判断鼠标的位置【利用这种方法可以写圆形按钮】

来源:互联网 发布:js 让隐藏的按钮显示 编辑:程序博客网 时间:2024/04/24 07:33

鼠标按下前:   鼠标按下后:


    public partial class JudgmentFrm : Form    {        private GraphicsPath m_Gpath;        private Rectangle m_Rect;        private bool m_Flag;        private bool m_IsDown;        public JudgmentFrm()        {            InitializeComponent();        }        private void JudgmentFrm_Paint(object sender, PaintEventArgs e)        {            Pen pen = new Pen(Color.Green);            e.Graphics.DrawEllipse(pen, m_Rect);            //使用普通画刷            //SolidBrush brush = new SolidBrush(Color.Red);             // 使用渐变画刷            if (m_Flag && m_IsDown)            {                LinearGradientBrush brush = new LinearGradientBrush(m_Rect, Color.Yellow, Color.Red, LinearGradientMode.Horizontal);                e.Graphics.FillEllipse(brush, m_Rect);            }            else            {                LinearGradientBrush brush = new LinearGradientBrush(m_Rect, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);                e.Graphics.FillEllipse(brush, m_Rect);            }        }        private void JudgmentFrm_Load(object sender, EventArgs e)        {            m_Gpath = new GraphicsPath();            m_Rect = new Rectangle(50, 50, 100, 100);            m_Gpath.AddEllipse(m_Rect);        }        private void JudgmentFrm_MouseDown(object sender, MouseEventArgs e)        {            m_Flag = m_Gpath.IsVisible(e.X, e.Y);            m_IsDown = true;            if (m_Flag)            {                Invalidate();            }        }        private void JudgmentFrm_MouseUp(object sender, MouseEventArgs e)        {            m_Flag = m_Gpath.IsVisible(e.X, e.Y);            m_IsDown = false;            if (m_Flag)            {                Invalidate();            }        }    }

有兴趣的朋友可以试一下。

原创粉丝点击