日期:2014-05-18  浏览次数:20776 次

winform中关于GD+的问题!!
我现在吸想实现这样一个功能(winform上)
 根据数据我绘制一条路线,然后绘制一些车在这条线上,然后我的鼠标要是移动到这些车上,就会跳出一个小提示框(就像web中的alt一样的东西)来说明车的详细资料, 现在我用GDI+绘制出了线和车,关键是我的提示框怎么作??? 
  大家能部能给我一个解决方案啊!! 谢谢

------解决方案--------------------
这个提示框实现起来确实不是很方便
因为提示框不知道什么时候出现,你绘制的车根本就不能识别

我提出一种方法仅供参考,期待更好的方法:
把车所在的区域保存,然后再mousemove中判断,如果在某一个区域中了,就把该区域中的信息显示,显示的提示框可用对话框(None的),然后这个提示框什么时候消失你可以自己写
------解决方案--------------------
车绘制在一个Rectangle里,MouseMove的检测Point是不是在Rectangle里面,如果是则显示提示。
------解决方案--------------------
怕麻烦的话,也可以用Tooltip:

代码可直接编译
C# code

using System;
using System.Drawing;
using System.Windows.Forms;

public class Form1 : Form
{
    ToolTip toolTip1 = new ToolTip();
    int lastHit = -1;

    Rectangle[] cars = new Rectangle[]{
        new Rectangle(100,50, 32,32),
        new Rectangle(200,50, 32,32),
        new Rectangle(200,100,32,32),
        new Rectangle(100,180,32,32),
        new Rectangle(10,50,32,32),
    };

    protected override void OnMouseMove(MouseEventArgs e)
    {
        int hit = -1;
        for(int i=0; i<cars.Length; i++)
        {
            if (cars[i].Contains(e.Location))
            {
                hit = i;
                break;
            }
        }

        this.Cursor = (hit >= 0) ? Cursors.Help : Cursors.Default;
        if (hit>=0 && hit != lastHit)
        {
            toolTip1.Show( "car " + hit, this, e.Location, 2000);
        }
        lastHit = hit;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        using (Graphics g = e.Graphics)
        {
            foreach (Rectangle rect in cars)
            {
                g.FillRectangle(Brushes.LightCoral, rect);
            }
        }
    }

    [STAThread]
    static void Main()
    {
        Application.Run(new Form1());
    }
}

------解决方案--------------------
1、你可以用Cursor的静态属性Cursor.Position来得到当前鼠标的屏幕位置,不过你得把它转化
成picturebox1中的坐标才好比较:
Point p = picturebox1.PointToClient( Cursor.Position );

2、你也可以处理MouseMove事件,而不用MouseHover事件。在MouseMove事件中,就有鼠标的location了。