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

WM_PAINT 有关问题

2012-09-02 
WM_PAINT 问题请问为什么下面的程序会出现一些区域 没有重绘 的现象(灰色的区域)?重现步骤:运行下面程序,

WM_PAINT 问题
请问为什么下面的程序会出现一些区域 没有重绘 的现象(灰色的区域)?

重现步骤:
运行下面程序, 最大化
运行另外一个程序,比如计算器, 移动计算器窗口 
就会出现 灰色区域 (最好快速拖动计算器窗口,比较容易重现)



C/C++ code
// vcwin32.cpp : Defines the entry point for the application.//#include "stdafx.h"#include "vcwin32.h"//---------------------------------------#include <windows.h>#include <winuser.h>#include <commctrl.h>#include <tchar.h>#include <assert.h>HINSTANCE ghInstance;// Global Instance// Prototyp WindowFunctionLRESULT CALLBACK WndProcedure (HWND hwnd, UINT msg,WPARAM w, LPARAM l);//****************************************************************************************HWND mainWin;int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*lpCmdLine*/, int nCmdShow){    //InitCommonControls();    static TCHAR szAppName[]= __TEXT("Test_WmPaint");    static TCHAR szTitleBar[]= __TEXT("Title Bar");    WNDCLASSEX    wndclass;    MSG         msg;    // Register Class    ghInstance = hInstance;    wndclass.cbSize         = sizeof(WNDCLASSEX);    wndclass.style        = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS ;    wndclass.lpfnWndProc  = WndProcedure;    wndclass.cbClsExtra   = 0;    wndclass.cbWndExtra   = 0;    wndclass.hInstance    = hInstance;    wndclass.hIcon        = NULL;//LoadIcon (ghInstance, IDI_APPLICATION);    wndclass.hCursor      = LoadCursor (NULL, IDC_ARROW);    //wndclass.hbrBackground = Null;    //wndclass.hbrBackground = CreateSolidBrush(RGB(200,0,0));    wndclass.hbrBackground = (HBRUSH)(COLOR_GRAYTEXT+1);//GetStockObject (WHITE_BRUSH);    wndclass.lpszMenuName = NULL;    wndclass.lpszClassName = szAppName;    wndclass.hIconSm         = NULL;//LoadIcon(NULL, IDI_APPLICATION);    if (!RegisterClassEx (&wndclass)) {        MessageBox(NULL, __TEXT("Window Registration Failed!"), __TEXT("Error!"),    MB_ICONEXCLAMATION | MB_OK);        return -1;    }    // Create Window and show Window    mainWin = CreateWindow (szAppName,        szTitleBar,        WS_OVERLAPPEDWINDOW, //WS_POPUP, //!WS_CAPTION ,        200,                   // x position for this window        200,                   // y position for this window        300,                 // window width        300,                 // window height        NULL,                // parent        NULL,                // menu        hInstance,        NULL);               // user data    if (mainWin == NULL) {        MessageBox(NULL, __TEXT("Window Creation Failed!"), __TEXT("Error!"),MB_ICONEXCLAMATION | MB_OK);        return 0;    }    ShowWindow   (mainWin, nCmdShow);    UpdateWindow (mainWin);    // Dispatch Message or exit program    while (GetMessage (&msg, NULL, 0, 0)) {        //if(!IsDialogMessage(hwnd, &msg)){        TranslateMessage (&msg);        DispatchMessage  (&msg);        //}    }    return (msg.wParam);}//**************************************************************************HBRUSH brushes[] = {    CreateSolidBrush(RGB(100,0,0)),    CreateSolidBrush(RGB(0,160,0)),    CreateSolidBrush(RGB(0,0,255)),};LRESULT CALLBACK WndProcedure (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam){    static int x=0;    assert(hWnd!=NULL);    int ret=0;    HDC hdc=NULL;    //HPEN pen=NULL;    PAINTSTRUCT ps;    RECT rt;    SetRect(&rt,0,0,0,0); // init    switch (wMsg) {        case WM_CREATE:            break;        case WM_LBUTTONDOWN:            return 0;            /*            hdc = GetDC(hWnd);            GetClientRect(hWnd, &rt);            FillRect(hdc, &rt,  brushes[0]);            ReleaseDC(hWnd, hdc);            break;            */        case WM_PAINT:{            x++;            hdc = BeginPaint(hWnd, &ps);            rt = ps.rcPaint;            SetRect(&rt, 0,0,2000,2000);            //use this line can easy see which region is painted.            ret=FillRect(hdc, &rt/*&ps.rcPaint*/, brushes[x%2]);            //ret=FillRect(hdc, &ps.rcPaint, brushes[0]);            if(ret==0) {                MessageBox(mainWin, __TEXT("fill rect Error "), __TEXT("Error"), MB_OK);            }            //RedrawWindow(hWnd, &ps.rcPaint, NULL, RDW_NOERASE);            EndPaint(hWnd, &ps);            //GdiFlush ();            return 0;        }        case WM_ERASEBKGND:            hdc = (HDC)wParam;            rt.left = 0 ; rt.top = 0;            rt.right = 300; rt.bottom = 300;            ret=FillRect(hdc, &rt, brushes[2] );            return 1;        case WM_DESTROY:            PostQuitMessage (0);            return (0);    }    return DefWindowProc (hWnd, wMsg, wParam, lParam);} 









[解决办法]
1 背景填充没有包含所有客户区
C/C++ code
case WM_ERASEBKGND:            hdc = (HDC)wParam;            GetClientRect(hWnd, &rt);            //rt.left = 0 ; rt.top = 0;            //rt.right = 300; rt.bottom = 300;            ret=FillRect(hdc, &rt, brushes[2] );            return 1;
[解决办法]
你选择一片区域重绘试试,就知道哪出问题了。
[解决办法]
探讨

你选择一片区域重绘试试,就知道哪出问题了。

[解决办法]
在vista和win7上运行是一种颜色,出不来你说的效果。
默认的aero主题不会像你想的那样去重绘。
win7下把界面主题改成Windows经典就可以测试你的程序了。
我在win7下测试了一下,没有出现你说的没有重绘的问题。

热点排行