- windows 窗口结构体 原型,10个字段
- typedef struct _WNDCLASS {
- UINT style;
- WNDPROC lpfnWndProc;
- int cbClsExtra;
- int cbWndExtra;
- HINSTANCE hInstance;
- HICON hIcon;
- HCURSOR hCursor;
- HBRUSH hbrBackground;
- LPCTSTR lpszMenuName;
- LPCTSTR lpszClassName;
- } WNDCLASS, *PWNDCLASS;
- BOOL GetMessage(
- LPMSG lpMsg, // message information
- HWND hWnd, // handle to window
- UINT wMsgFilterMin, // first message
- UINT wMsgFilterMax // last message
- );
这10个字段中最重要的是第二个 lpfnWnProc和倒数第一个lpszClassName;
一般用法如下:
WNDCLASS wc={0}; // 先都搞成0再说
wc.lpfnWndProc = WndProc;
wc.lpszClassName = L"mywindows";
- //原型
- HWND CreateWindow(
- LPCTSTR lpClassName,
- LPCTSTR lpWindowName,
- DWORD dwStyle,
- int x,
- int y,
- int nWidth,
- int nHeight,
- HWND hWndParent,
- HMENU hMenu,
- HANDLE hInstance,
- PVOID lpParam );
一般用法:
- hwnd = CreateWindowW( wc.lpszClassName, // 窗口名称
- L"Window的标题",
- WS_OVERLAPPEDWINDOW | WS_VISIBLE,
- 100, //左上角x
- 100, //......y
- 350, //宽
- 250, //高
- NULL, // 父窗口句柄,没有则用NULL
- NULL, // 菜单
- hInstance, //本窗口句柄
- NULL);
或者:
- hwnd = CreateWindow (szAppName, TEXT ("Random Rectangles"),
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT,
- CW_USEDEFAULT, CW_USEDEFAULT,
- NULL, NULL, hInstance, NULL) ;
- #include <windows.h>
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- LRESULT CALLBACK PanelProc(HWND, UINT, WPARAM, LPARAM);
- void RegisterRedPanelClass(void);
- void RegisterBluePanelClass(void);
- int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR lpCmdLine,int nShowCmd)
- {
- MSG msg;
- WNDCLASSW ws ={0}; // ws 共有10个字段.... 填充结构体为0先....然后下面把需要特别赋值的一一赋值.
- ws.lpszClassName = L"mycolorwindows";
- ws.hInstance = hInstance;
- ws.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
- ws.lpfnWndProc = WndProc;
- ws.hCursor = LoadCursor(0,IDC_HAND);//第一个参数为NULL 0,表示用windows自带的ICON,那么第二个参数用自带光标.一般是箭头IDC_ARROW
- // 窗口结构体ws 填充后,注册窗口
- RegisterClassW(&ws);
- // 然后创建窗口
- HWND hwnd;
- hwnd = CreateWindowW(ws.lpszClassName,L" color windows ",
- WS_OVERLAPPEDWINDOW|WS_VISIBLE,
- 100,100,250,180,
- NULL,
- 0,
- hInstance,
- 0);
- // 消息循环
- while(GetMessage(&msg,NULL,0,0)){ // 获取消息,保存到msg结构体中
- TranslateMessage(&msg); //翻译键盘消息
- DispatchMessage(&msg); //派发消息
- }
- return (int)msg.wParam; //?? msg结构没有wParam这个成员函数,wParam是怎么来的
- }
- LRESULT CALLBACK WndProc( HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
- {
- switch(msg)
- {
- case WM_CREATE:
- RegisterRedPanelClass();
- CreateWindowW(L"RedPanelClass", NULL,
- WS_CHILD | WS_VISIBLE,
- 20, 20, 80, 80,
- hwnd, (HMENU) 1, NULL, NULL);
- RegisterBluePanelClass();
- CreateWindowW(L"BluePanelClass", NULL,
- WS_CHILD | WS_VISIBLE,
- 120, 20, 80, 80, // 120 = 20 +80 + 20
- hwnd, (HMENU) 2, NULL, NULL);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- LRESULT CALLBACK PanelProc( HWND hwnd, UINT msg,
- WPARAM wParam, LPARAM lParam )
- {
- switch(msg)
- {
- case WM_LBUTTONUP:
- Beep(50, 40);
- break;
- }
- return DefWindowProcW(hwnd, msg, wParam, lParam);
- }
- void RegisterRedPanelClass(void)
- {
- HBRUSH hbrush = CreateSolidBrush(RGB(255, 0, 0));
- WNDCLASSW rwc = {0};
- rwc.lpszClassName = L"RedPanelClass";
- rwc.hbrBackground = hbrush;
- rwc.lpfnWndProc = PanelProc;
- rwc.hCursor = LoadCursor(0, IDC_ARROW);
- RegisterClassW(&rwc);
- }
- void RegisterBluePanelClass(void)
- {
- HBRUSH hbrush = CreateSolidBrush(RGB(0, 0, 255));
- WNDCLASSW rwc = {0};
- rwc.lpszClassName = L"BluePanelClass";
- rwc.hbrBackground = hbrush;
- rwc.lpfnWndProc = PanelProc;
- rwc.hCursor = LoadCursor(0, IDC_ARROW);
- RegisterClassW(&rwc);
- }