导言和相关资料:
我要在主窗口的WM_PAINT处理程序中实现一个复杂的绘图。
我提交了下面的图片来说明这一点:

主窗口有静态控件,而不是具有样式SS_NOTIFY的按钮。
当用户单击它们时,程序中会发生某些操作,这些操作目前是不相关的。
下图显示主窗口中的静态控件为:

橙色面板上的地图是一个EMF文件,左上角和右边的标志是PNG文件,其他图片是bitmap的。
Visual Styles通过#pragma指令启用。我还使用GDI+和GDI。
项目是作为空项目创建的,我已经从“从头开始”编码了所有内容。
为了实现这一任务,我决定在WM_PAINT,中绘制的整个图片,并将透明的 static control**s放在对应它们的图片**上。
为了保持代码的整洁和简单,我已经编写了实现上述功能的函数,因此我的WM_PAINT处理程序可以尽可能小。
更新#1( 2013年12月17日更新):
为了实现从成员arx获得的建议,我发布了一个可以编译并可以重现问题的单一源代码:
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <gdiplus.h>
#pragma comment( linker, "/manifestdependency:\"type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' \
language='*'\"" )
#pragma comment( lib, "comctl32.lib")
#pragma comment( lib, "Msimg32.lib" )
#pragma comment( lib, "Gdiplus.lib" )
using namespace Gdiplus;
// variable for storing the instance
static HINSTANCE hInst;
// variables for painting the window
static HBRUSH hbPozadina, // gray background brush for grid on the top
BlueFrame, // needed to frame blue static controls
hbr; // orange brush for orange panel
/********* helper functions for WM_PAINT **********/
// Fills triangle with gradient brush
void GradientTriangle( HDC MemDC,
LONG x1, LONG y1,
LONG x2, LONG y2,
LONG x3, LONG y3,
COLORREF top, COLORREF bottom )
{
TRIVERTEX vertex[3];
vertex[0].x = x1;
vertex[0].y = y1;
vertex[0].Red = GetRValue(bottom) << 8;
vertex[0].Green = GetGValue(bottom) << 8;
vertex[0].Blue = GetBValue(bottom) << 8;
vertex[0].Alpha = 0x0000;
vertex[1].x = x2;
vertex[1].y = y2;
vertex[1].Red = GetRValue(top) << 8;
vertex[1].Green = GetGValue(top) << 8;
vertex[1].Blue = GetBValue(top) << 8;
vertex[1].Alpha = 0x0000;
vertex[2].x = x3;
vertex[2].y = y3;
vertex[2].Red = GetRValue(bottom) << 8;
vertex[2].Green = GetGValue(bottom) << 8;
vertex[2].Blue = GetBValue(bottom) << 8;
vertex[2].Alpha = 0x0000;
// Create a GRADIENT_TRIANGLE structure that
// references the TRIVERTEX vertices.
GRADIENT_TRIANGLE gTriangle;
gTriangle.Vertex1 = 0;
gTriangle.Vertex2 = 1;
gTriangle.Vertex3 = 2;
// Draw a shaded triangle.
GradientFill( MemDC, vertex, 3, &gTriangle, 1, GRADIENT_FILL_TRIANGLE);
}
// draws the background for the part of the window between header and footer
void drawBackground( HDC MemDC, RECT r )
{
/******* main window's gradient background ********/
GradientTriangle( MemDC, r.right, r.bottom - r.top - 30,
r.left, r.bottom - r.top - 30,
r.left, r.top + 120,
RGB( 0x95, 0xB3, 0xD7 ),
RGB( 0xDB, 0xE5, 0xF1 ) );
GradientTriangle( MemDC, r.right, r.bottom - r.top - 30,
r.right, r.top + 120,
r.left, r.top + 120,
RGB( 0x95, 0xB3, 0xD7 ),
RGB( 0xDB, 0xE5, 0xF1 ) );
}
// draws the header of the main window
void drawHeader( HDC MemDC, RECT rect, HBRUSH hbPozadina )
{
FillRect( MemDC, &rect, hbPozadina );
}
// fills rectangle with gradient brush
void GradientRectangle( HDC MemDC,
LONG x1, LONG y1,
LONG x2, LONG y2,
COLORREF top,
COLORREF bottom )
{
// vertexes for static's gradient color
TRIVERTEX vertexS[2];
vertexS[0].x = x1;
vertexS[0].y = y1;
vertexS[0].Red = GetRValue(top) << 8;
vertexS[0].Green = GetGValue(top) << 8;
vertexS[0].Blue = GetBValue(top) << 8;
vertexS[0].Alpha = 0x0000;
vertexS[1].x = x2;
vertexS[1].y = y2;
vertexS[1].Red = GetRValue(bottom) << 8;
vertexS[1].Green = GetGValue(bottom) << 8;
vertexS[1].Blue = GetBValue(bottom) << 8;
vertexS[1].Alpha = 0x0000;
// Create a GRADIENT_RECT structure that
// references the TRIVERTEX vertices.
GRADIENT_RECT gRect;
gRect.UpperLeft = 0;
gRect.LowerRight = 1;
// Draw a shaded rectangle.
GradientFill( MemDC, vertexS, 2, &gRect, 1, GRADIENT_FILL_RECT_V );
}
// fills the "button" with blue gradient and frames it with blue brush
void FillButton( HDC MemDC, RECT rect, HBRUSH BlueFrame )
{
// fill upper half of the rectangle
GradientRectangle( MemDC,
rect.left, rect.top,
rect.right, rect.top + ( rect.bottom - rect.top ) / 2,
RGB( 0x95, 0xB3, 0xD7 ),
RGB( 0x4F, 0x8B, 0xBD ) );
// fill bottom half of the rectangle
GradientRectangle( MemDC,
rect.left, rect.top + ( rect.bottom - rect.top ) / 2,
rect.right, rect.bottom,
RGB( 0x4F, 0x8B, 0xBD ),
RGB( 0x95, 0xB3, 0xD7 ) );
FrameRect( MemDC, &rect, BlueFrame );
}
// draws the "status bar" at the bottom of the main window
void drawFooter( HDC MemDC, RECT r, COLORREF top, COLORREF bottom )
{
// down triangle
GradientTriangle( MemDC,
r.right, r.bottom,
( r.right - r.left ) / 2,
r.bottom - r.top - 15,
r.left, r.bottom,
top, bottom );
// upper triangle
GradientTriangle( MemDC,
r.right, r.bottom - r.top - 30,
( r.right - r.left ) / 2, r.bottom - r.top - 15,
r.left, r.bottom - r.top - 30,
top, bottom );
// left triangle
GradientTriangle( MemDC,
r.left, r.bottom,
( r.right - r.left ) / 2, r.bottom - r.top - 15,
r.left, r.bottom - r.top - 30,
top, bottom );
// right triangle
GradientTriangle( MemDC,
r.right, r.bottom - r.top - 30,
( r.right - r.left ) / 2, r.bottom - r.top - 15,
r.right, r.bottom,
top, bottom );
}
// draw orange panel on which map and 3 static controls will be drawn
void drawOrangePanel( HDC MemDC, RECT r, COLORREF top, COLORREF bottom )
{
// down triangle
GradientTriangle( MemDC,
r.right, r.bottom,
r.left + ( r.right - r.left ) / 2,
r.top + ( r.bottom - r.top ) / 2,
r.left, r.bottom,
top, bottom );
// upper triangle
GradientTriangle( MemDC,
r.right, r.top,
r.left + ( r.right - r.left ) / 2,
r.top + ( r.bottom - r.top ) / 2,
r.left, r.top,
top, bottom );
// left triangle
GradientTriangle( MemDC,
r.left, r.bottom,
r.left + ( r.right - r.left ) / 2,
r.top + ( r.bottom - r.top ) / 2,
r.left, r.top,
top, bottom );
// right triangle
GradientTriangle( MemDC,
r.right, r.top,
r.left + ( r.right - r.left ) / 2,
r.top + ( r.bottom - r.top ) / 2,
r.right, r.bottom,
top, bottom );
}
void onPaint( HWND hwnd, WPARAM wParam, LPARAM lParam )
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint( hwnd, &ps);
RECT r; // rectangle for main window's client area
GetClientRect( hwnd, &r);
HDC MemDC = CreateCompatibleDC(hdc); // back buffer
// compatible bitmap for MemDC
HBITMAP bmp = CreateCompatibleBitmap( hdc, r.right - r.left, r.bottom - r.top ),
oldBmp = (HBITMAP)SelectObject( MemDC, bmp ); // needed for cleanup
// draw background for middle part of the window
drawBackground( MemDC, r );
// draw header with grid lines
RECT rect; // position it properly at the top
rect.left = r.left;
rect.top = r.top;
rect.right = r.right;
rect.bottom = 120;
drawHeader( MemDC, rect, hbPozadina );
// draw "status bar"
drawFooter( MemDC, r, RGB( 0x48, 0xAC, 0xC6), RGB( 0x31, 0x83, 0x99 ) );
/******* draw static control's background ****/
//======== top left static control ======//
//position it properly
rect.left = ( 3 * ( r.right - r.left ) / 4 - 340 ) / 3;
rect.top = 120 + ( r.bottom - r.top - 450 ) / 3;
rect.right = 150 + ( 3 * ( r.right - r.left ) / 4 - 340 ) / 3;
rect.bottom = 270 + ( r.bottom - r.top - 450 ) / 3;
// draw gradient button
FillButton( MemDC, rect, BlueFrame );
//======================== draw orange panel =================//
//position it properly
rect.left = 3 * ( r.right - r.left ) / 4 - 40;
rect.top = r.top + 140;
rect.right = rect.left + ( r.right - r.left ) / 4;
rect.bottom = rect.top + ( r.bottom - r.top - 190 );
drawOrangePanel( MemDC, rect, RGB( 0xFF, 0xC8, 0xAA ), RGB( 0xFF, 0x96, 0x48 ) );
/****** draw back buffer on the screen DC *******/
BitBlt( hdc, 0, 0, r.right - r.left, r.bottom - r.top, MemDC, 0, 0, SRCCOPY );
/************** cleanup *******************/
SelectObject( MemDC, oldBmp );
DeleteObject(bmp); // compatible bitmap for MemDC
DeleteDC(MemDC);
EndPaint( hwnd, &ps);
}
// WinMain's procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
//******** brushes ***********//
// load gray background brush for the top banner
hbPozadina = CreateSolidBrush( RGB( 230, 230, 230 ) );
// brush for orange panel that holds 3 static controls and a map
hbr = CreateSolidBrush( RGB( 255, 163, 94 ) );
// blue frame for blue static controls
BlueFrame = CreateSolidBrush( RGB(79, 129, 189) );
/*******************************************/
}
return (LRESULT)0;
case WM_ERASEBKGND:
return (LRESULT)1; // so we avoid flicker ( all painting is in WM_PAINT )
case WM_PAINT:
{
// paint the picture
onPaint( hwnd, wParam, lParam );
}
return (LRESULT)0;
case WM_SIZE:
InvalidateRect( hwnd, NULL, FALSE );
return (LRESULT)0;
case WM_CLOSE:
// destroy brushes
DeleteObject(hbPozadina);
DeleteObject(hbr);
DeleteObject(BlueFrame);
DestroyWindow(hwnd);
return (LRESULT)0;
case WM_DESTROY:
PostQuitMessage(0);
return (LRESULT)0;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow)
{
// store hInstance in global variable for later use
hInst = hInstance;
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
/**** variables for GDI+ initialization ******/
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
/********* Initialize GDI+. ********/
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
/**** finished GDI+ initialisation *****/
// initialize common controls
INITCOMMONCONTROLSEX iccex;
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_STANDARD_CLASSES ;
InitCommonControlsEx(&iccex);
// register main window class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInst;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.hbrBackground = NULL;//(HBRUSH)GetStockObject( WHITE_BRUSH );
wc.lpszMenuName = NULL;
wc.lpszClassName = L"Main_Window";
wc.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox( NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK );
return 0;
}
// create main window
hwnd = CreateWindowEx( 0, // WS_EX_COMPOSITED "improved" drawing of the edges
L"Main_Window",
L"Геотермист",
WS_OVERLAPPEDWINDOW,
( GetSystemMetrics(SM_CXMAXIMIZED) - 1020 ) / 2,
( GetSystemMetrics(SM_CYMAXIMIZED) - 600 ) / 2,
1020, 600, NULL, NULL, hInstance, 0 );
if(hwnd == NULL)
{
MessageBox( NULL, L"Window creation failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK );
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
// shutdownd GDI+
GdiplusShutdown(gdiplusToken);
return Msg.wParam;
}我在Windows XP上工作,使用MS Visual Studio C++ 2008 Express Edition和pure Win32 API。
一个注意:自从VS 的速成版没有资源编辑器以来,就没有资源编辑器,资源文件和资源头是使用ResEdit从这里创建的:http://www.resedit.net/。
问题:
为了避免闪烁,我使用了双缓冲,这是我从*Paul *关于CodeProject的文章、Charles的编程Windows第5版和Forger的WIN32教程中学到的。
从理论上讲,一切正常,我的代码编译时没有任何错误。
我在这里粘贴了代码:http://pastebin.com/zSYT1i8L
我的英语不够好,无法准确地描述我面临的问题(我所能说的就是主窗口的边缘和静态控件的重绘“缓慢”和闪烁),所以我创建了一个演示应用程序:http://www.filedropper.com/geotermistgrafika
我为解决问题所做的努力:
我已经处理了WM_ERASEBKGND (返回的(LRESULT)1 ),并且从窗口类中排除了样式CS_VREDRAW和CS_HREDRAW,因此不应该因此导致闪烁。
我的窗口没有WS_CLIPCHILDREN样式,因为桌面图片的一部分是静态控件所在的位置。
在我的WM_SIZE处理程序中有:
SetWindowPos(...) API重新定位静态控件,并通过添加SWP_NOCOPYBITS标志减少闪烁。InvalidateRect( hWnd, NULL, FALSE )使整个窗口失效,所以这个API在无效时不会发送WM_ERASEBKGND (第三个参数是FALSE ),但是即使我尝试使用TRUE,效果也是一样的。我为WM_PAINT处理程序实现了双缓冲,如上述书籍/文章/教程中的示例所示(通过在内存DC中执行所有操作,并在屏幕DC上执行BitBlt(...)以避免闪烁)。
我没有处理WM_SIZING、WM_WINDOWPOSCHANGING或WM_MOVING消息。
我使用了工具GDIView ( handles.html )来跟踪GDI leaks。
每次我调整窗口大小/最大化窗口时,GDIView都会在区域列中显示+4,这意味着我会泄漏区域,但我不知道这是怎么可能的,因为我没有使用对区域进行操作的API,并且对所有内容都进行了双重检查。
在我看来,一切都应该是好的,也许这是不相关的,但我只是想提一下,也许这很重要。
如果我将WS_EX_COMPOSITED样式添加到主窗口,则性能不会提高。
我试图找到在线的例子,以帮助我解决我的问题,但所有的教程都很简单,不涵盖这类复杂的图片。
重要注意事项:
在我的WM_PAINT处理程序为空并使用onPaint API获得的设备上下文调用WM_ERASEBKGND中的onPaint函数之后,闪烁消失了,但是在调整大小时,窗口的重绘很“尖峰”,主窗口边缘的问题也没有得到解决。
不过,这比原来的代码要好得多..。
问题:
如何解决我在上面提供的演示应用程序中演示的问题?
我在此感谢所有投入时间和精力来帮助我的人。
诚挚的问候。
发布于 2013-12-17 21:49:33
我在Windows7上编译并运行了代码。使用标准主题(使用DWM),它在调整大小时看起来很好。
我切换到Windows经典主题(它禁用DWM),当窗口调整大小时,按钮的边缘有很多撕裂现象。我怀疑这就是你所看到的问题。
当绘制与屏幕的物理更新不同步时,会发生撕裂。这将导致部分屏幕显示旧图像,部分屏幕显示新图像。在垂直直线水平移动时,效果尤其明显。
您还在使用Windows吗?
据我所知,防止XP撕裂的唯一方法是在画图中使用DirectX,并显式地与VSYNC同步。不过,您可能可以使用现有的绘图代码,只需使用DirectX绘制最后的位图。或者还有其他的同步机制。我不知道。
然而,由于问题本身修复了Windows的后期版本,所以我不会做任何事情。
https://stackoverflow.com/questions/20641566
复制相似问题