大家来帮忙看看以下程序运行时执行几次 MessageBox(hwnd,TEXT("WM_PAINT occurs"),NULL,MB_OK);
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "winnt.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
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 (BLACK_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
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
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:
hdc = BeginPaint (hwnd, &ps) ;
MessageBox(hwnd,TEXT("WM_PAINT occurs"),NULL,MB_OK);
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -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) ;
}
/*------------------------------------------------------------
HELLOWIN.C -- Displays "Hello, Windows 98!" in client area
(c) Charles Petzold, 1998
------------------------------------------------------------*/#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include "winnt.h"LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
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 (BLACK_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
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
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:
hdc = BeginPaint (hwnd, &ps) ;
MessageBox(hwnd,TEXT("WM_PAINT occurs"),NULL,MB_OK);
GetClientRect (hwnd, &rect) ;
DrawText (hdc, TEXT ("Hello, Windows 98!"), -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) ;
}
解决方案 »
- OpenGL焦点拾取问题
- VS2005 VS 2008 都没有 ——建立类向导这个功能
- vs2005对话框编辑器的问题
- 关于ZLIB里面解压缩返回Z_BUF_ERROR错误,跟帖有分!!
- 在dll里怎么用GetPrivateProfileString读取ini的内容?
- 我用MFC在一个对话框上显示一个图片,我怎么把他移值为ACTIVE控件???
- 消息映射
- 在WINDOWNS平台下使用临界区和原子操作的讨论
- 我的sock = (int)socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)怎么return -1
- 哪里有mfc activex的例子,谢谢
- 不好意思,问大家一个简单的问题
- 在托盘程序中,我用append函数来动态添加菜单,这时不知道菜单的ID,那我怎样能得到我当前正在点击的菜单中的一项的text????????
好奇怪,有的时候一次有的时候两次,我怀疑鼠标的操作可能造成WM_PAINT被响应
应该把MessageBox(hwnd,TEXT("WM_PAINT occurs"),NULL,MB_OK); 放到case WM_CREATE:下面
up :)
if(i)
{
新年快乐!
}死循环
如果你把这几句话
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
改成
0, // initial x position
0, // initial y position
100, // initial x size
100, // initial y size这样窗口比较下,只会触发一次(切换窗口触发那就是另外一回事了)
.
.
.
.
come out
可经实验结果证实,两次弹出对话框(我指的是程序初始化运行时)。。
不知为何。
不过我有一问:ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
////////////////////////////////
以上两行代码会触发几次WM_PAINT消息?
CreateWindow()如果建出来的窗口属性中有WS_VISIBLE,那么此函数将在发完WM_CREATE中发WM_PAINT(第一次).
ShowWindow(),此时窗口已绘,楼主iCmdShow又是SW_SHOW,窗口并没有发生变化,所以不再发WM_PAINT消息.
UpdateWindow()强行再发一次WM_PAINT(第二次)建议楼主不要在WM_PAINT处理函数中用messagebox(),如要统计个数,写到文件中即可,这种可能更改显示情况的函数不适合在此使用.
做几个试验:
1.启动程序,对话框弹出。按确定,对话框不再弹出。这是因为对话框弹出并不能激发WM_PAINT,虽然它占用了一块空间。
2.方法同上,但不要直接按确定,用鼠标拖着对话框走一段,在确定。好,对话框又弹出来了。说明移动对话框会激发WM_PAINT。
3.将程序启动,对话框弹出,这是按下windows+D,(windows键就是左边ctrl旁边的画着windows logo的键)将窗口最小化,再将窗口恢复,会看到一大堆的对话框重叠着弹出来。因为是叠着的,所以这时关闭对话框时也会激发WM_PAINT。
不知为何。。
不过我很同意/ Zark(金陵五月) 的意见,我取消了messagebox(),用一个static变量i统计该消息次数。。每次i++;
////////////////////
建议楼主不要在WM_PAINT处理函数中用messagebox(),如要统计个数,写到文件中即可,这种可能更改显示情况的函数不适合在此使用.
ok,but you must wait until this problem is resolved....