如何检测并突出鼠标悬停矩形 [英] How to detect and highlight rectangle on mouse hover

查看:144
本文介绍了如何检测并突出鼠标悬停矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了C#.NET Windows应用程序的控制,显示在图形模式中的某些对象(事物)。 所以,我创建了一个矩形取决于我的清单得到了项目的数量,并通过控制OnPaint事件绘制它了控制。

I have created a windows application control in C#.net to show some objects(things) in graphical mode. So I created a rectangles depend on number of items I got in list and plot it over control by using Control OnPaint event.

现在我想强调的是矩形,如果鼠标悬停就可以了。

Now I want to highlight that rectangle if mouse hovering on it.

请检查连接的图像更加清晰和放大器;建议我怎样才能实现这一目标。

Please check attached image for more clarity & suggest me how can I achieve it .

推荐答案

你检查的经典DRAWCLI例子吗?它显示了一个基本的应用程序应该如何管理对象和工具。

Did you check the classical DrawCli example? It shows how a basic application should manage objects and tools.

总之,你应该重新枚举的MouseMove 事件中的名单,获得该项目的矩形,并设置其 IsFocused 属性为true,如果鼠标指针是RECT内。然后重绘,如果事情发生了转变。你甚至可以做到这一点在你的的OnPaint (查看当前鼠标的位置),但你必须要始终重绘里面的一切的MouseMove (这是一个非常糟糕的主意)。

In short you should re-enumerate your list inside MouseMove event, get the item's rect and set its IsFocused property to true if mouse pointer is inside that rect. Then redraw if something changed. You may even do that inside your OnPaint (check current mouse position) but then you have to always redraw everything inside MouseMove (and it's a very bad idea).

类的伪code解释一下我的意思是:

Kind of pseudo-code to explain what I mean:

protected override void OnPaint(PaintEventArgs e)
{
   foreach (GraphicalObject obj in Objects)
   {
      if (!obj.IsVisible)
            continue;

      Rectangle rect = obj.GetBounds(e.Graphics);
      if (!rect.Intersects(e.ClipRectangle))
         continue;

      obj.Draw(e.Graphics);
   }
}

GraphicalObject 是你可以把屏幕上的所有对象的基本类型。 对象是包含其中的集合( GraphicalObjectCollection ,例如)的属性。现在,code可能是这样的(注意,这是远远跳投距离真正的code,这是一个普遍的方法,只是一个例子):

GraphicalObject is the base type for all objects you can put on the screen. Objects is a property that contains the collection of them (GraphicalObjectCollection, for example). Now you code may be like this (note that this is far aways from true code, it's just an example of a general technique):

protected override OnMouseMove(MouseMoveEventArgs e)
{
   bool needToRedraw = false;

   using (Graphics g = CreateGraphics())
   {
      foreach (GraphicalObject obj in Objects)
      {
         if (!obj.IsVisible)
               continue;

         Rectangle rect = obj.GetBounds(e.Graphics);
         if (rect.Contains(e.Location))
         {
            if (!obj.IsFocused)
            {
               obj.IsFocused = true;
               needToRedraw = true;
            }
         }
         else
         {
            if (obj.IsFocused)
            {
               obj.IsFocused = false;
               needToRedraw = true;
            }
         }

         obj.Draw(e.Graphics);
      }
   }

   if (needToRedraw)
      Invalidate();
}

这篇关于如何检测并突出鼠标悬停矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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