windows程序设计上说,同一个窗口类别可以建立多个窗口,这些窗口都是共用同一个窗口消息处理函数WndProc,WndProc的HWND参数用以识别具体的窗口,所以就稍微改了一下书中的例子,Create两个窗口,试验消息处理函数,然后在wndproc中,做不同的处理,可是执行起来,wndproc函数HWND参数好像始终是传入 的第一个窗口的whwnd,第二个窗口的消息没有传入或没有处理????
#include <windows.h>
#include "mmsystem.h"
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) ;
}
#include <windows.h>
#include "mmsystem.h"
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) ;
}
if(hwnd11=hwnd12)
MessageBox ( NULL, TEXT ("hwnd11=hwnd12"), TEXT ("tt!"), MB_ICONERROR) ;
ps:这里高手如云,这个问题应该很基础的,拜托高手帮我看看吧?
hwnd11 = hwnd; //-------------------------------》你添加的
hwnd12 = hwnd1; //-------------------------------》你添加的 这个赋值导致你把先前创建窗口的句柄(hwnd1)丢失了。
UpdateWindow (hwnd1) ;
为什么还可以运行成功呢
------------------
这句改为if(hwnd11==hwnd12) 那两句赋值对的
shunruo 说的是一个问题!至于你说的问题,如果你能在屏幕上看到两个窗口,并且它们都能很好的工作,那就没问题,肯定是你实验方法不对!试着在窗口过程入口加一个断点跟进去,不要闹一个判断,结果条件还写错了!