使用WindowsAPI,创建MonthCalendar控件:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")
#define ID_CALENDAR 101
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE: {
// 创建 MonthCalendar 控件
HWND hwndCalendar = CreateWindowEx(0, MONTHCAL_CLASS, NULL,
WS_CHILD | WS_VISIBLE | WS_BORDER,
10, 10, 200, 200, // x, y, width, height
hwnd, (HMENU)ID_CALENDAR, NULL, NULL);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 初始化 Common Controls
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_DATE_CLASSES; // 初始化日期控件类
InitCommonControlsEx(&icex);
// 创建窗口类
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = L"MonthCalendarExample";
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
RegisterClass(&wc);
// 创建窗口
HWND hwnd = CreateWindowEx(
0, L"MonthCalendarExample", L"MonthCalendar Control Example",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 250,
NULL, NULL, hInstance, NULL);
// 消息循环
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
效果图:
没有回复内容