简体   繁体   English

如何使用GDI +在WM_PAINT中正确绘制

[英]How to properly paint in WM_PAINT with GDI+

I'm new to using GDI+ and I was wondering how to fix the mess of creating and disposing objects that I have. 我是使用GDI +的新手,我想知道如何解决创建和处理我拥有的对象的麻烦。 Right now all the program needs to do is handle repeated WM_PAINT messages and update a DrawPie with increased degrees each time. 现在,程序所需要做的就是处理重复的WM_PAINT消息,并DrawPie以更高的度数更新DrawPie

I have called GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL ); 我叫GdiplusStartup( &gdiplusToken, &gdiplusStartupInput, NULL ); when the window starts up, and GdiplusShutdown( gdiplusToken ); 当窗口启动时, GdiplusShutdown( gdiplusToken ); on WM_DESTROY . WM_DESTROY

Degrees global variable defined near the top: 度全局变量在顶部附近定义:

volatile double degrees = 0;

Here is my WM_PAINT : 这是我的WM_PAINT

case WM_PAINT: {
    hdc = BeginPaint( hWnd, &ps );

    Graphics g( hdc );
    Pen p( Color::Green );
    if ( degrees > 0 ) {
        if ( degrees == 360 ) {
            g.DrawEllipse( &p, 0, 0, 100, 100 );
        } else {
            g.DrawPie( &p, 0, 0, 100, 100, -90, degrees );
        }
    }

    EndPaint( hWnd, &ps );
    break;
}

And here is the function that updates the degrees and updates the window (It's in a separate thread): 这是更新度数和更新窗口的函数(在单独的线程中):

void UpdateDegrees() {
    for ( ;; ) {
        if ( globalHWND != NULL ) {
            degrees += 0.1;
            InvalidateRect( globalHWND, NULL, TRUE );
            UpdateWindow( globalHWND );
        }
    }
}

If I run it I get a "solid" pie shape like this , which I assume is it just redrawing itself at every angle. 如果运行它,我将得到一个像这样的“实心”饼形,我认为它只是在各个角度重绘自身。 It needs to look like this , in other words, clear the graphic before it redraws itself each time. 它需要看起来像这样 ,换句话说,在每次重新绘制自身之前都要清除图形。 (Sorry, I guess I need 10 rep to post inline images) (对不起,我想我需要10名代表才能发布内嵌图片)

I know that I didn't initialize and/or dispose of my graphics correctly, but I honestly have no idea how to do that. 我知道我没有正确初始化和/或处置图形,但是老实说我不知道​​该怎么做。 Any help would be greatly appreciated! 任何帮助将不胜感激! Thanks. 谢谢。

There are lots of ways you can do this, the standard one being to handle the WM_ERASEBKGND message. 有很多方法可以执行此操作,标准方法是处理WM_ERASEBKGND消息。

For simplicity, you can just fill a white rectangle across the clip rect (which is specified in ps), which will clear the background being painted. 为简单起见,您可以仅在片段rect上填充一个白色矩形(以ps为单位指定),以清除要绘制的背景。

SolidBrush backGroundBrush(Color(255,255,255));
Rect clipRect(ps->rcPaint.left, ps.rcPaint.top, ps->rcPaint.right - ps->rcPaint.left, ps->rcPaint.bottom - ps.rcPaint.top);
g.FillRectangle(&backgroundBrush, Rect(ps->rcPaint.left, ps.rcPaint));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM