窗体标题栏外的拖动操作

类别:.NET开发 点击:0 评论:0 推荐:

窗体标题栏外的拖动操作

(作者:张均洪)    2004-9-3

我们知道对窗体的拖动只需要点住标题栏,拖动鼠标就可以了.但有些时候我们想在窗体的标题栏外的区域实行拖动窗体的操作.这时就要需要我们自已写些代码了,下面是我的做法,供大家参观.

新建一个窗体FORM1,并放入两个RADIOBUTTON控件,第一个是确定是否窗体拖动,第三个是确定是否指定某一区域进行窗体拖动.

以下是窗体代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.IO;

 

namespace WindowsApplication1
{
    partial class Form1 : Form
    {
        Point _StartXY;        //鼠标按下的位置
        bool _MoveDown=false;            //鼠标是不是按下了
        //指定一个区域,好写字在ONPAINT中
        Rectangle _rec = new Rectangle(50, 50, 70, 70);
        public Form1()
        {
            InitializeComponent();
        }

             protected override void OnMouseDown(MouseEventArgs e)
        {
            //要移动吗?由按下鼠标(onMouseDown)确定哈
                bool OktoMove=false;        
                base.OnMouseDown(e);
           
                if(this.radioButton1.Checked)
                {
                    OktoMove=true;
                }
                else
                {
                    //如果按下鼠标的点坐标在_rec指定的区域内
                    //那么我们同意移动窗体操作哈。
                    if(this._rec.Contains(this.PointToClient(MousePosition)))
                    {
                        OktoMove = true;

                    }                     
                 }
            //如果同意移动,就把按下鼠标一瞬的坐标给_StartXY
            //并同意按着鼠标移动。
                if(OktoMove==true)
                {
                   //把当前的鼠标位置赋给变量
                    _StartXY = MousePosition;
                    _MoveDown = true;
                }
            }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            //如果同意按着鼠标移动,就把移动的量给变量
            //以便窗体按你的量移动。
            if (_MoveDown)
            {
                int Xdiff,Ydiff;
                Xdiff = MousePosition.X - _StartXY.X;
                Ydiff = MousePosition.Y - _StartXY.Y;
                //改变窗体位置
                this.Left = this.Left + Xdiff;
                this.Top = this.Top + Ydiff;
                //把新位置给_StartXY变量了哈
                _StartXY = MousePosition;
            }          
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            _MoveDown = false;

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //要一支新画笔,向NET要哈
            SolidBrush b = new SolidBrush(Color.Red);
            if (radioButton2.Checked)
            {
                //填充_rec的区域,笔的颜色是红的哈
                e.Graphics.FillRectangle(b, _rec);
                //改变笔的颜色
                b.Color = Color.White;
                //重新定义一个区域,这个区域其实就是_rec变量的区域
                RectangleF recF = new RectangleF(_rec.X,
                                               _rec.Y,
                                               _rec.Width,
                                               _rec.Height);
                //在这个区域写几个字呢
                e.Graphics.DrawString("click on me drag", Font, b, recF);
            }
            else
            {
                //如果不同意用区域改变窗体位置,就把背景设为窗体的
                //颜色,免得影响视觉。
                b.Color = BackColor;
                //把这个区域涂了
                e.Graphics.FillRectangle(b, _rec);
            }
            b.Dispose();//把笔丢了,免得占我地方
        }


              private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            this.Invalidate(this._rec);//使_rec指定的地方无效了
        }
       
    }
}

 

好啦,试试看!!

本文地址:http://com.8s8s.com/it/it44253.htm