GDI +如何擦除旧图像并为基于图像的按钮绘制新图像? [英] GDI+ how to erase the old image and to draw new image for the image-based button?

查看:269
本文介绍了GDI +如何擦除旧图像并为基于图像的按钮绘制新图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据图像绘制一个按钮。按钮应该有3张图片:

- 1.png - 正常状态,

- 2.png - 鼠标悬停状态,

- 3 .png - 鼠标按下状态。

同样在WM_MOUSEMOVE的处理程序中我设置了_TrackMouseEvent。



好​​的。为此,我在窗口过程中编写以下代码:

- 对于正常的按钮状态:

I'm trying to draw a button based on the images. The button should have 3 pictures:
- 1.png - normal state,
- 2.png - mouse hover state,
- 3.png - mouse down state.
Also in the handler for WM_MOUSEMOVE I set _TrackMouseEvent.

Ok. To do that I write the following code in the window procedure:
- For the normal button state:

case WM_PAINT:
{
    PAINTSTRUCT ps;
    BeginPaint(hwnd, &ps);
    Gdiplus::Graphics  *normal_state = new Gdiplus::Graphics(ps.hdc);
    Image *img = new Image(L"d:/images/1.png",false);
    normal_state->DrawImage(img,5,5);
    delete img;
    delete normal_state;
    EndPaint(hwnd, &ps);
}



完美无缺。

现在我正在努力实现鼠标悬停状态:


It works perfectly.
And now I'm trying to to make realization for mouse hover state:

case WM_MOUSEHOVER:
{
    PAINTSTRUCT ps;
    BeginPaint(hwnd,&ps);
    Gdiplus::Graphics *mousehover_state = new Gdiplus::Graphics(ps.hdc);
    Image *img = new Image(L"d:/images/2.png",false);
    mousehover_state->DrawImage(img,5,5);
    delete img;
    delete mousehover_state;
    EndPaint(hwnd,&ps);
}



但是在最终结果中WM_MOUSEHOVER被解雇了,但是没有绘制新图片 - 这是一个问题编号1.

问题2 - 如何在新按钮状态的绘画图像之前删除旧按钮状态的图像?

谢谢!


But in the final result WM_MOUSEHOVER fired, but the new picture is not drawn - this is a problem number 1.
Problem number 2 - how to erase the image of the old button state before the painting image for new button state?
Thanks!

推荐答案

通过 WM_PAINT 更改渲染中使用的数据中的图像,并在适当大小和位置的矩形上调用 InvalidateRect http://msdn.microsoft.com/en- us / library / windows / desktop / dd145002%28v = vs.85%29.aspx [ ^ ]或http://msdn.microsoft.com/en-us/library/2f3csed3%28v=vs.80%29.aspx [ ^ ]。



-SA
绘制图像的唯一位置是WM_PAINT;选择要在其他情况下绘制的图像。
Change the image in your data used in rendering via WM_PAINT and call InvalidateRect on the rectangle of proper size and position: http://msdn.microsoft.com/en-us/library/windows/desktop/dd145002%28v=vs.85%29.aspx[^] or http://msdn.microsoft.com/en-us/library/2f3csed3%28v=vs.80%29.aspx[^].

—SA
The only place for drawing the image is WM_PAINT; select the image to draw in the other cases.


这篇关于GDI +如何擦除旧图像并为基于图像的按钮绘制新图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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