c++ - C++、WinApi、月历控件
问题描述
我需要一个日历应用程序来从用户那里获取开始和结束日期(2 个日期)。我不知道如何以最好的方式实现它。所以,我开始查看这个月历控件示例。它只有一个日历,但我需要 2 个月的日历。
但是,我面临的第一个问题是我收到了这个错误
Error LNK2019 unresolved external symbol __imp_InitCommonControlsEx referenced in function "void __cdecl CreateControls(struct HWND__ *)" (?CreateControls@@YAXPEAUHWND__@@@Z)
我无法弄清楚解决方案。
网站上给出的完整代码:
#include <windows.h>
#include <commctrl.h>
#include <wchar.h>
#include <strsafe.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateControls(HWND);
void GetSelectedDate(HWND, HWND);
HWND hStat;
HWND hMonthCal;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
HWND hwnd;
MSG msg;
WNDCLASSW wc = {0};
wc.lpszClassName = L"Month Calendar";
wc.hInstance = hInstance ;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc ;
wc.hCursor = LoadCursor(0, IDC_ARROW);
RegisterClassW(&wc);
hwnd = CreateWindowW(wc.lpszClassName, L"Month Calendar",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 250, 300, 0, 0, hInstance, 0);
while (GetMessage(&msg, NULL, 0, 0)) {
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg,
WPARAM wParam, LPARAM lParam) {
LPNMHDR lpNmHdr;
switch(msg) {
case WM_CREATE:
CreateControls(hwnd);
break;
case WM_NOTIFY:
lpNmHdr = (LPNMHDR) lParam;
if (lpNmHdr->code == MCN_SELECT) {
GetSelectedDate(hMonthCal, hStat);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProcW(hwnd, msg, wParam, lParam);
}
void CreateControls(HWND hwnd) {
hStat = CreateWindowW(WC_STATICW, L"",
WS_CHILD | WS_VISIBLE, 80, 240, 80, 30,
hwnd, (HMENU)1, NULL, NULL);
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(icex);
icex.dwICC = ICC_DATE_CLASSES;
InitCommonControlsEx(&icex);
hMonthCal = CreateWindowW(MONTHCAL_CLASSW, L"",
WS_BORDER | WS_CHILD | WS_VISIBLE | MCS_NOTODAYCIRCLE,
20, 20, 200, 200, hwnd, (HMENU)2, NULL, NULL);
}
void GetSelectedDate(HWND hMonthCal, HWND hStat) {
SYSTEMTIME time;
const int dsize = 20;
wchar_t buf[dsize];
ZeroMemory(&time, sizeof(SYSTEMTIME));
SendMessage(hMonthCal, MCM_GETCURSEL, 0, (LPARAM) &time);
size_t cbDest = dsize * sizeof(wchar_t);
StringCbPrintfW(buf, cbDest, L"%d-%d-%d",
time.wYear, time.wMonth, time.wDay);
SetWindowTextW(hStat, buf);
}
解决方案
您需要链接Comctl32.lib
:
#pragma comment(lib,"Comctl32.lib")
推荐阅读
- java - 使用浏览器获取请求发送 JWT 令牌
- python - 循环数据帧
- c# - 从文本文件中查找行并使用 c# 将其复制到新的文本文件中
- hana - HANA:为什么时间戳列会截断毫秒并包含时区?
- jmeter - 我是 jmeter 新手,如何在 jmeter 中为 1000 个用户创建脚本
- python - 尝试通过按键输入更改视频帧
- python - 如何使用 .shp 文件生成与 tiff 文件大小相同的 RGB 图像?
- database - Sphinx RT 索引并按值在 json 中搜索
- reactjs - React 错误边界不能工作,只是打破
- django - 如何使用带有 django 的谷歌应用引擎安全地提供静态文件?