win32-封装BeginPaint

Graphics* StartPaint(HWND win, HDC* hdc, PAINTSTRUCT* ps)
{
    *hdc = BeginPaint(win, ps);
    return new Graphics(*hdc);
}


 case WM_PAINT:
        {
            HDC hdc;
            PAINTSTRUCT ps;
            Graphics* g = StartPaint(hWnd, &hdc, &ps);           
            SolidBrush solidBrush(Color(255, 255, 0, 0));           
            Point point1 = Point(0, 0);
            Point point2 = Point(0, 10);
            Point point3 = Point(10, 10);
            Point point4 = Point(10, 0);
            Point points[4] = {point1,point2,point3,point4};
            g->FillPolygon(&solidBrush, points, 4);
            delete g;
            EndPaint(hWnd, &ps);
        }
        break;

更好的方法:

std::unique_ptr<Graphics> StartPaint(    ...) 
{ 
    ... 
    return std::make_unique<Graphics>(*hdc); 
}

auto g = StartPaint(...);

 

posted @ 2020-05-08 10:42  strive-sun  阅读(290)  评论(0编辑  收藏  举报