首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

SendMessage会跳过不执行.解决方案

2012-03-19 
SendMessage会跳过不执行....C/C++ code#include Windows.h#define CellSize 15#define Width 20#define

SendMessage会跳过不执行....

C/C++ code
#include <Windows.h>#define CellSize 15#define Width 20#define Height 30LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR szCmdLine, int iCmdShow){    static TCHAR szAppName[] = TEXT("Tetris");    WNDCLASS wndclass;    HWND hwnd;    MSG msg;    wndclass.cbClsExtra = 0;    wndclass.cbWndExtra = 0;    wndclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);    wndclass.hCursor = LoadCursor(hInstance, IDC_ARROW);    wndclass.hIcon = LoadIcon(hInstance, IDI_APPLICATION);    wndclass.hInstance = hInstance;    wndclass.lpfnWndProc = WndProc;    wndclass.lpszClassName = szAppName;    wndclass.lpszMenuName = NULL;    wndclass.style = CS_HREDRAW | CS_VREDRAW;        if(!RegisterClass(&wndclass))    {        MessageBox(NULL, TEXT("The programe requires Windows NT!"), szAppName, MB_ICONERROR);        return 0;    }    hwnd = CreateWindow(szAppName, TEXT("Tetris Game"), WS_OVERLAPPEDWINDOW,        (GetSystemMetrics(SM_CXSCREEN) - Width * CellSize) / 2,        (GetSystemMetrics(SM_CYSCREEN) - Height * CellSize) / 2,         Width * CellSize, Height * CellSize,        NULL, NULL, hInstance, NULL);        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;    static int i;    //HPEN hPen;    switch(message)    {    case WM_CREATE:             i = 0;        return 0;    case WM_KEYDOWN:        switch(wParam)        {        case VK_RIGHT:            i++;            break;        }        SendMessage(hwnd, WM_PAINT, 0, 0);    //SendMessage会跳过不执行        return 0;    case WM_PAINT:        hdc = BeginPaint(hwnd, &ps);        GetClientRect(hwnd, &rect);        TextOut(hdc, i, 0 , TEXT("abc"), lstrlen(TEXT("abc")));        EndPaint(hwnd, &ps);        return 0;    case WM_DESTROY:        PostQuitMessage(0);        return 0;    }    return DefWindowProc(hwnd, message, wParam, lParam);}


[解决办法]
SendMessage(hwnd, WM_PAINT, 0, 0); //SendMessage会跳过不执行
-->
InvalidateRect(hwnd, NULL, FALSE);
[解决办法]
The WM_PAINT message is generated by the system and should not be sent by an application. To force a window to draw into a specific device context, use the WM_PRINT or WM_PRINTCLIENT message. Note that this requires the target window to support the WM_PRINTCLIENT message. Most common controls support the WM_PRINTCLIENT message.
[解决办法]
2楼说得很清楚了,WM_PAINT消息由系统产生而非应用程序,要强制使窗口重绘可调用InvalidateRect函数使用窗口无效,系统自动重绘窗口
[解决办法]
SendMessage(hwnd, WM_PAINT, 0, 0); //SendMessage会跳过不执行
是没效果吧。
WM_PAINT要有无效区!

热点排行