Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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:
C#
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);
}

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!
Posted

1 solution

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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900