GDI:WM_PAINT问题 [英] GDI: WM_PAINT question

查看:91
本文介绍了GDI:WM_PAINT问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我遇到了一个问题:窗口的某个区域没有重新绘制。

Recently, I encoutered a problem is: some region of a window is not repainted.

要查看未绘制区域,请尝试:

to see un paint region, please try:

1。运行以下代码并最大化窗口,让我说,窗口A。

1. run following code and maximum the window, let me say, window A.

2。打开另一个窗口,例如记事本,拖动记事本并快速移动,

2. open another window, e.g notepad, drag notepad and move quickly,

3。然后在窗口A上显示重新显示区域(灰色)。

3. then unpaint region (Gray color) will be displayed on window A.

我不知道这些灰色区域来自何处,因为WM_ERASEBKGND消息也被处理,灰色区域不应显示.. 。

I have no idea where these Gray regions come from because the WM_ERASEBKGND msg is also processed, Gray region should not displayed...

并且不确定这是否与操作系统有关。我的操作系统是:XP,sp3。

and not sure if this is related to OS. my OS is:XP, sp3.

代码的预期运行结果是:

它只显示红色或绿色区域,不包括灰色区域

the expect running result of code is:
it will only display region in Red or Green, not include region in Gray

如果您无法复制,请告诉我,因为我听说Vista可能会得到不同的结果。

if you can not reproduce, please let me know because I heard that Vista perhaps got different result.

非常感谢!

// vcwin32.cpp : Defines the entry point for the application.
#include "stdafx.h"
//---------------------------------------------------------------------------
#ifndef STRICT
#define STRICT
#endif
#include <windows.h>
#include <winuser.h>
#include <commctrl.h>
#include <tchar.h>

#include <assert.h>

HINSTANCE ghInstance;// Global Instance

// Prototyp WindowFunction
LRESULT CALLBACK WndProcedure (HWND hwnd, UINT msg,WPARAM w, LPARAM l);
//****************************************************************************************

HWND mainWin;

HBRUSH brushes[] = {
	CreateSolidBrush(RGB(100,0,0)),
	CreateSolidBrush(RGB(0,160,0)),
	CreateSolidBrush(RGB(0,0,255)),
};

int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*lpCmdLine*/, int nCmdShow)
{
	//InitCommonControls();

	// check if the brush good
	for(int i=0;i<3;i++) {
		assert(brushes[i]!=NULL);
	}

	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);
}

//**************************************************************************

LRESULT CALLBACK WndProcedure (HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
	static int x=0;
	assert(hWnd!=NULL);
	int ret=0;

	HDC hdc=NULL;
	PAINTSTRUCT ps;
	RECT rt;
	SetRect(&rt,0,0,0,0); // init

	switch (wMsg) {
		case WM_CREATE:
			break;

		case WM_LBUTTONDOWN:
			/*
			hdc = GetDC(hWnd);

			GetClientRect(hWnd, &rt);
			FillRect(hdc, &rt,  brushes[0]);
			ReleaseDC(hWnd, hdc);
			*/
			return 0;

		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, &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;

			GetClientRect(hWnd, &rt);
			//SetRect(&rt, 0, 0, 3000, 3000);
			ret=FillRect(hdc, &rt, brushes[2] );
			if(ret==0) {
				MessageBox(mainWin, __TEXT("fill rect Error "), __TEXT("Error"), MB_OK);
            }
			return 1;

		case WM_DESTROY:
			PostQuitMessage (0);
			return (0);
	}
	return DefWindowProc (hWnd, wMsg, wParam, lParam);
}

推荐答案

最后,这个问题被认为与此有关主题设置。

在Xp中为
,如果主题设置设为"经典Windows主题",则会显示不亮区域。

if主题设置设置为"XP主题",它不会显示重写区域。



谢谢你
at last, this problem is believed to be related to theme settings.

in Xp, if theme setting is set to "Classic Windows theme", it will display unpaint region.
if theme setting is set to "XP theme", it will not diplay unpaint region.

Thanks


这篇关于GDI:WM_PAINT问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆