请问:如何使用Common Control

wdxp119 wdxp119 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
如题。代码如下:
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib, "comctl32.lib")

HWND hWnd;
HINSTANCE hInst;

LRESULT CALLBACK wndproc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
);

int WINAPI WinMain(
HINSTANCE hInstance, // handle to current instance
HINSTANCE hPrevInstance, // handle to previous instance
LPSTR lpCmdLine, // pointer to command line
int nCmdShow // show state of window
)
{
WNDCLASS wndclass;

wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wndclass.hCursor = LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpfnWndProc = wndproc;
wndclass.lpszClassName = "ControlTest";
wndclass.lpszMenuName = NULL;
wndclass.style = CS_HREDRAW | CS_VREDRAW;

if(!RegisterClass(&wndclass))
{
MessageBox(NULL,"注册窗口类失败!","ControlTest",MB_OK);
return 0;
}

hInst=hInstance;
hWnd=CreateWindow("ControlTest",
"ControlTest",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInst,
NULL);

ShowWindow(hWnd,nCmdShow);
UpdateWindow(hWnd);

MSG msg;
while(GetMessage(&msg,hWnd,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

LRESULT CALLBACK wndproc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam // second message parameter
)
{
switch(uMsg)
{
case WM_CREATE:
{
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
if(!InitCommonControlsEx(&icex))
{
MessageBox(NULL,"Fail to init common controls!","Test",MB_OK);
return 0;
}
HWND hWndListView;
hWndListView = CreateWindowEx(0,WC_LISTVIEW, //WC_LISTVIEW不需要加引号
TEXT(""),
WS_CHILD | WS_VISIBLE|WS_BORDER | LVS_ICON | LVS_EDITLABELS | WS_EX_CLIENTEDGE ,
10,
10,
100,
100,
hWnd,
(HMENU)1200, //控件ID
hInst, //实例句柄
NULL);
if(hWndListView == NULL)
MessageBox(NULL,"Fail to create window!","Test",0);

return 0;
}
case WM_CLOSE:
PostQuitMessage(0);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc (hwnd, uMsg, wParam, lParam) ;
}

代码编译没有问题。但在创建控件时失败!
...全文
评论 1
收藏 已收藏
分享
返回顶部
wdxp119
VC/MFC 16,550 成员 421,606 内容