简体   繁体   English

提前 InvalidateRect

[英]InvalidateRect ahead of time

.h: 。H:

struct RRECT
{
    int x;
    int y;
    int width;
    int height;
};

Bitmap* bitmap;
RRECT position;
void Button::SetImage(HDC g)
{
    Graphics graphics(g);
    DrawImage(bitmap,0,0,width,height);
}
void Button::CreateImage(HDC hdc)
{
    invalidateButton(hwnd);
    SetImage(hdc);
}
void Button::invalidateButton(HWND hWnd)
{
    RECT rect = Helper::getAbsoluteRect(position);
    InvalidateRect(hWnd, &rect, true);

}

WM_PAINT: WM_PAINT:

PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hwnd, &ps);
    childCloseButton = new Button(new Bitmap(L"close2.png"), new Bitmap(L"close.png"), new Bitmap(L"close2.png"), hdc);
        childCloseButton->position.x = p.x - 35;
        childCloseButton->position.y = 10;
        childCloseButton->position.height = bitmap->GetHeight();
        childCloseButton->position.width = bitmap->GetWidth();
    childCloseButton->CreateImage(gt);
    EndPaint(hwnd, &ps);

I am trying to update the region before drawing a new image, but it updates after I have drawn the image, how do I make the region update first, and then the picture is drawn if I remove InvalidateRect from the invalidateButton() method, and add it to WM_PAINT :我试图在绘制新图像之前更新该区域,但它在我绘制图像后更新,我如何首先更新区域,然后如果我从invalidateButton()方法中删除InvalidateRect则绘制图片,并且将其添加到WM_PAINT

PAINTSTRUCT ps;

HDC hdc = BeginPaint(hwnd, &ps);
childCloseButton = new Button(new Bitmap(L"close2.png"), new Bitmap(L"close.png"), new Bitmap(L"close2.png"), hdc);
childCloseButton->position.x = p.x - 35;
childCloseButton->position.y = 10;
childCloseButton->position.height = bitmap->GetHeight();
childCloseButton->position.width = bitmap->GetWidth();
childCloseButton->CreateImage(gt);
childCloseButton->invalidateButton(hwnd);
childCloseButton->position.x = p.x - 35;
childCloseButton->position.y = 10;
childCloseButton->CreateImage(gt);
EndPaint(hwnd, &ps);

its working good它运作良好

EDIT编辑

LoadFils(){
childCloseButton = new Button(new Bitmap(L"close2.png"), new Bitmap(L"close.png"), new Bitmap(L"close2.png"), hdc);
    childCloseButton->position.x = p.x - 35;
    childCloseButton->position.y = 10;
    childCloseButton->position.height = bitmap->GetHeight();
    childCloseButton->position.width = bitmap->GetWidth();

}

WM_CREATE:
{
    LoadFils();
}

WM_PAINT:{

    childCloseButton->CreateImage(gt);
    childCloseButton->position.x = 55;
    childCloseButton->position.y = 25;
    childCloseButton->CreateImage(gt);

}

The purpose of WM_PAINT is to only DRAW A WINDOW'S CONTENT , do nothing else in there. WM_PAINT的目的是只绘制一个 WINDOW'S CONTENT ,在那里什么都不做。 All of your image preparation needs to be done outside of the WM_PAINT handler.所有图像准备工作都需要在WM_PAINT处理程序之外完成。 Have WM_PAINT simply draw the current image as-is.WM_PAINT简单地按原样绘制当前图像。 You can InvalidateRect() the window whenever the conditions of the drawing change.只要绘图的条件发生变化,您就可以InvalidateRect() window。

Try something more like this:尝试更像这样的东西:

struct RRECT
{
    int x;
    int y;
    int width;
    int height;
};

class Button
{
public:
    Bitmap* bitmap;
    RRECT position;
    HWND parentWnd;
    ...
    Button(Bitmap*, Bitmap*, Bitmap*);
    void Draw(HDC);
    void Invalidate();
};

Button::Button(Bitmap*, Bitmap*, Bitmap*)
{
    ...
}

void Button::Draw(HDC g)
{
    Graphics graphics(g);
    graphics.DrawImage(bitmap, Point(position.x, position.y));
}

void Button::Invalidate()
{
    InvalidateRect(parentWnd, &position, TRUE);
}
Button* childCloseButton;

void LoadFils(HWND hwnd)
{
    childCloseButton = new Button(new Bitmap(L"close2.png"), new Bitmap(L"close.png"), new Bitmap(L"close2.png"));
    childCloseButton->parentWnd = hwnd;
    childCloseButton->position.x = ...;
    childCloseButton->position.y = ...;
    childCloseButton->position.height = bitmap->GetHeight();
    childCloseButton->position.width = bitmap->GetWidth();
    childCloseButton->Invalidate();
}

...

case WM_CREATE: {
    LoadFils(hwnd);
    break;
}

case WM_PAINT: {
    PAINTSTRUCT ps;
    HDC hdc = BeginPaint(hwnd, &ps);
    childCloseButton->Draw(hdc);
    EndPaint(hwnd, &ps);
    break;
}

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

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