在鼠标上方更改矩形区域的颜色。 [英] Change the color of a rectangular area on mouse over.
问题描述
我有一个在WM_PAINT消息中绘制的矩形。现在我想像按钮一样模拟这个矩形部分。当我将鼠标光标移到矩形区域时,它的背景颜色应该改变,当我把光标从它上面取出时它应该是原来的颜色。
如何我可以使用Win32 API捕获此鼠标悬停事件吗?我怀疑必须使用许多鼠标消息来克服这个问题。但是我在捕获鼠标效果方面取得了如下成功:
I have a rectangle which is drawn in the WM_PAINT message. Now I want to simulate this rectangular portion like a button. When I bring the mouse cursor into the rectangular area its background color should be changed and when i bring the cursor out of it it should be in its original color.
How can I catch this mouse-over event using Win32 API ? I suspect many mouse messages must be used to overcome this. However i am succesful in catching the mouse-in effect as follows:
//The rectangle is just a structure with 4-values(x1, y1, x2, y2).
case WM_MOUSEMOVE:
int x = LOWORD(lParam);
int y = HIWORD(lParam);
if(X_and_Y_are_inside_the_rectangle)
{
//get_a_dc
//paint_the_rectangle_with_another_color
//release_dc
}
else
{
//if i uncomment the following line even the mouse-in effect
//won't work
//InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_PAINT:
//get_a_dc
//paint_the_rectangle_with_original_color
//release_dc
return 0;
问题简介:
如何在Win32 API中捕获特定矩形区域的鼠标输入和鼠标移出事件?
仅限问题在于捕获这两个消息之王而不是像GDI库那样的任何东西。
推荐答案
#define INSIDE_RECT 1
#define OUTSIDE_RECT -1
static int iHoverRect;
case WM_CREATE:
iHoverRect = OUTSIDE_RECT;
return 0;
case WM_MOUSEMOVE:
int x = LOWORD(lParam);
int y = HIWORD(lParam);
if(iHoverRect == OUTSIDE_RECT)
{
if(is_points_in_rect)
{
iHoverRect = INSIDE_RECT;
//mouse-in event here
}
}
else
{
if(!is_points_in_rect)
{
iHoverRect = OUTSIDE_RECT;
//mouse-out event here
}
}
return 0;
您应该只在WM_PAINT消息处理程序内部绘制。例如(伪代码):
You should draw only inside WM_PAINT message handler. For instance (pseudo-code):
case WM_MOUSEMOVE:
int x = LOWORD(lParam);
int y = HIWORD(lParam);
if(X_and_Y_are_inside_the_rectangle)
{
if ( colorRect != colorInside)
{
colorRect = colorInside;
InvalidateRect
UpdateWindow
}
}
else
{
if ( colorRect != colorOutside)
{
colorRect = colorOutside;
InvalidateRect
UpdateWindow
}
}
return 0;
case WM_PAINT:
get_a_dc
paint the rectangle with colorRect
release_dc
return 0;
这篇关于在鼠标上方更改矩形区域的颜色。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!