C++ C++;,WinApi,月历控件
我需要一个日历应用程序从用户那里获取开始和结束日期(2个日期)。我不知道如何以最佳方式实施它。所以,我开始看这个。它只有一个日历,但我需要两个月的日历 但是,我面临的第一个问题是我犯了这个错误C++ C++;,WinApi,月历控件,c++,winapi,C++,Winapi,我需要一个日历应用程序从用户那里获取开始和结束日期(2个日期)。我不知道如何以最佳方式实施它。所以,我开始看这个。它只有一个日历,但我需要两个月的日历 但是,我面临的第一个问题是我犯了这个错误 Error LNK2019 unresolved external symbol __imp_InitCommonControlsEx referenced in function "void __cdecl CreateControls(struct HWND__ *)" (?C
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);
}
#包括
#包括
#包括
#包括
LRESULT回调WndProc(HWND、UINT、WPARAM、LPARAM);
无效控制(HWND);
无效GetSelectedDate(HWND,HWND);
HWND-hStat;
HWND hMonthCal;
int WINAPI WinMain(HINSTANCE HINSTANCE、HINSTANCE HPPreInstance、,
LPSTR lpCmdLine,int nCmdShow){
HWND-HWND;
味精;
WNDCLASSW wc={0};
wc.lpszClassName=L“月历”;
wc.hInstance=hInstance;
wc.hbrBackground=GetSysColorBrush(颜色面);
wc.lpfnWndProc=WndProc;
wc.hCursor=加载光标(0,IDC_箭头);
注册分类(&wc);
hwnd=CreateWindowW(wc.lpszClassName,L“月历”,
WS|U重叠窗口| WS|U可见,
100、100、250、300、0、0、0);
while(GetMessage(&msg,NULL,0,0)){
发送消息(&msg);
}
返回(int)msg.wParam;
}
LRESULT回调WndProc(HWND HWND,UINT msg,
WPARAM WPARAM,LPARAM LPARAM){
LPNMHDR LPNMHDR;
开关(msg){
案例WM_创建:
创建控件(hwnd);
打破
案件通知:
lpNmHdr=(lpNmHdr)LPRAM;
如果(lpNmHdr->代码==MCN\U选择){
GetSelectedDate(hMonthCal,hStat);
}
打破
案例WM_销毁:
PostQuitMessage(0);
打破
}
返回DefWindowProcW(hwnd、msg、wParam、lParam);
}
void CreateControls(HWND-HWND){
hStat=CreateWindowW(WC_STATICW,L“”,
WS|u CHILD | WS|u VISIBLE,80240,8030,
hwnd,(HMENU)1,空,空);
第一性icex;
icex.dwSize=sizeof(icex);
icex.dwICC=ICC\u日期\u类别;
InitCommonControlsEx(&icex);
hMonthCal=CreateWindowW(MONTHCAL_CLASSW,L“”,
WS|u BORDER | WS|u CHILD | WS|u VISIBLE | MCS|u notodaycle,
20,20,200,200,hwnd,(HMENU)2,空,空);
}
无效GetSelectedDate(HWND hMonthCal,HWND hStat){
系统时间;
常数int dsize=20;
wchar_t buf[dsize];
零内存(&time,sizeof(SYSTEMTIME));
SendMessage(hmonhcal、MCM_GETCURSEL、0、(lpram)和时间);
大小cbDest=dsize*sizeof(wchar\t);
StringCbPrintfW(buf,CbTest,L“%d-%d-%d”,
time.wYear,time.wMonth,time.wDay);
SetWindowTextW(hStat,buf);
}
您需要链接Comctl32.lib
:
#pragma comment(lib,"Comctl32.lib")
它说,
InitCommonControlsEx
没有定义。是否确实包含了Comctl32.lib
?或者,将Comctrl32.lib
添加到项目配置中。