windows程序设计上说,同一个窗口类别可以建立多个窗口,这些窗口都是共用同一个窗口消息处理函数WndProc,WndProc的HWND参数用以识别具体的窗口,所以就稍微改了一下书中的例子,Create两个窗口,试验消息处理函数,然后在wndproc中,做不同的处理。
#include <windows.h>
#include <mmsystem.h>
#pragma comment(lib,"winmm.lib")
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
HWND hwnd11; //------------------------------------》我添加的
HWND hwnd12; //------------------------------------》我添加的
HINSTANCE hInstance1;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
HWND hwnd1 ; //------------------------------------》我添加的
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName= szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox ( NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow( szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,// initial x position
CW_USEDEFAULT,// initial y position
CW_USEDEFAULT,// initial x size
CW_USEDEFAULT,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
hwnd1 = CreateWindow( szAppName, // window class name //--》我添加的
TEXT ("The Hello Program1111"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT,// initial x position
CW_USEDEFAULT,// initial y position
CW_USEDEFAULT,// initial x size
CW_USEDEFAULT,// initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters //--》我添加的
hwnd11 = hwnd; //-------------------------------》我添加的
hwnd12 = hwnd1; //-------------------------------》我添加的
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
ShowWindow (hwnd1, iCmdShow) ; //-------------------------------》我添加的
UpdateWindow (hwnd1) ; //-------------------------------》我添加的
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
switch (message)
{
case WM_CREATE:
PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
return 0 ;
case WM_PAINT:
if(hwnd12==hwnd) //-------------------------------》我添加的
{ //-------------------------------》我添加的
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
}
else //-------------------------------》我添加的
{ //-------------------------------》我添加的
hdc = BeginPaint (hwnd, &ps) ; //---------》我添加的
GetClientRect (hwnd, &rect) ; //---------》我添加的
DrawText (hdc, TEXT ("Hello, Windows xp!"), -1, &rect, //---------》我添加的
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ; //---------》我添加的
EndPaint (hwnd, &ps) ; //---------》我添加的
} //-------------------------------》我添加的
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
************************************
WNDCLASS wndclass;
wndclass.lpfnWndProc = ::DefWindowProc;
// 窗口函数的函数原形
LRESULT CALLBACK DefWindowProc(HWND, UINT, WPARAM, LPARAM)
{
...
}
为啥上面的窗口回调函数还会有HWND这个参数呢,这个参数不是肯定是自己了吗,不是多余吗,::DispachMessage的时候不是就是根据这个参数给调用各个窗口自己的DefWindowProc吗 为啥还要HWND
答:1)为了能让内部的其他api使用句柄,比如通常在 WM_CREATE 里创建子窗口
LRESULT wnd_proc(HWND hwnd, MSG msg, ...) {
switch(msg) {
case WM_CREATE:
CreateWindow("button", ..., hwnd, ...); //hwnd作为parent句柄
...
}
}
比如WM_PAINT
HDC hdc = GetDC(hWnd);
2)hwnd用于识别这个窗口过程函数是哪个窗口正在被调用的,因为多个windows窗口可以使用同一个窗口类,而且可以共用同一个窗口过程。