none
使用winform绘图,需要绘制一个跟随鼠标移动的直线,如下图中的绿线: RRS feed

答案

  • Hi,

    跟随鼠标移动的话只要处理mousemove然后计算下坐标 drawline就可以了 http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawline(v=vs.110).aspx

    以下的是MouseMove的示例代码:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace GlobalMouseEvents
    {
       public partial class Form1 : Form
       {
          public Form1()
          {
             GlobalMouseHandler gmh = new GlobalMouseHandler();
             gmh.TheMouseMoved += new MouseMovedEvent(gmh_TheMouseMoved);
             Application.AddMessageFilter(gmh);
    
             InitializeComponent();
          }
    
          void gmh_TheMouseMoved()
          {
             Point cur_pos = System.Windows.Forms.Cursor.Position;
             System.Console.WriteLine(cur_pos);
          }
       }
    
       public delegate void MouseMovedEvent();
    
       public class GlobalMouseHandler : IMessageFilter
       {
          private const int WM_MOUSEMOVE = 0x0200;
    
          public event MouseMovedEvent TheMouseMoved;
    
          #region IMessageFilter Members
    
          public bool PreFilterMessage(ref Message m)
          {
             if (m.Msg == WM_MOUSEMOVE)
             {
                if (TheMouseMoved != null)
                {
                   TheMouseMoved();
                }
             }
             // Always allow message to continue to the next filter control
             return false;
          }
    
          #endregion
       }
    }
    使用IMessageFilter class来监测鼠标消息然后画线,具体的设计需要你自己来根据自己的要求来做。


    Barry
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月7日 8:24
    版主