当前位置:编程学习 > C#/ASP.NET >>

最近在做一个拖动按钮的简单程序

这里问一些小问题
下面我给大家看一下我的代码。(或许不够专业)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WinForm_Shift_Button
{
    public partial class StartShift : Form
    {
        Point StartThePoint = new Point();
        bool YNShift = false;
        public StartShift()
        {
            InitializeComponent();
            BtnShift.MouseDown += new MouseEventHandler(BtnShift_MouseDown);
            BtnShift.MouseMove += new MouseEventHandler(BtnShift_MouseMove);
            BtnShift.MouseUp += new MouseEventHandler(BtnShift_MouseUp);
        }

        void BtnShift_MouseDown(object sender, MouseEventArgs e)//当按下按钮时
        {
            StartThePoint = Control.MousePosition;
            YNShift = true;
        }

        void BtnShift_MouseMove(object sender, MouseEventArgs e)//当移动鼠标时
        {

        }

        void BtnShift_MouseUp(object sender, MouseEventArgs e)//当放开按钮时
        {
            Point MousePointTemp = new Point();
            if (YNShift)
            {
                MousePointTemp.X = BtnShift.Location.X - (StartThePoint.X - Control.MousePosition.X);
                MousePointTemp.Y = BtnShift.Location.Y - (StartThePoint.Y - Control.MousePosition.Y);
                BtnShift.Location = MousePointTemp;
            }
            YNShift = false;
        }
    }
}
这么写是没有问题,可是在移动的时候无法实时看到移动的效果。
我当时想到了另一个办法,将按钮放开的方法(除了最后一句YNShift = false)写在移动鼠标的事件当中。
但是这么写,正常拖动的时候,从按下鼠标那一刻开始,按钮就不知道在干什么了。这个时候,按钮就会全球流,满屏幕飞,然后我只能表示压力很大啊,很大。
因而求教众大神,大神们是用什么办法,让按钮拖动,而且能够实时看到按钮的状态的? --------------------编程问答-------------------- All in button's events ↓

MouseDown:
 flag=true
 oldpoint=e.Location    (relative to left-top corner of button)

MouseMove:
 if flag is true then
  button.Location=new Point(
   button.Location.X+e.Location.X-oldpoint.X,
   button.Location.Y+e.Location.Y-oldpoint.Y
  );
 end

MouseUp:
 flag=false



==============
试试 --------------------编程问答--------------------
引用 1 楼  的回复:
All in button's events ↓

MouseDown:
 flag=true
 oldpoint=e.Location    (relative to left-top corner of button)

MouseMove:
 if flag is true then
  button.Location=new Point(
   button.Locat……

啊,大神,你的VB我表示好要命,表示,我先看看吧,总之还是感谢您。 --------------------编程问答-------------------- 其实不是VB。。
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,