vb在窗体中,按钮可以拖动,放下鼠标按钮就放在那里,怎么实现?

vb在窗体中,按钮可以拖动,放下鼠标按钮就放在那里,怎么实现?,第1张

版本一左键单击后可以移动,再次单击停止移动:
Dim
bMove
As
Boolean
'判断
可不可以
移动
Dim
oldX!,
oldY!
'!是Single的缩写
Private
Sub
Command1_
MouseMove
(Button
As
Integer,
Shift
As
Integer,
X
As
Single,
Y
As
Single)
If
bMove
Then
Command1Left
=
Command1Left
-
oldX
+
X
Command1Top
=
Command1Top
-
oldY
+
Y
End
If
End
Sub
Private
Sub
Command1_MouseUp(Button
As
Integer,
Shift
As
Integer,
X
As
Single,
Y
As
Single)
If
Button
=
1
Then
'1就是鼠标左键
bMove
=
Not
bMove
oldX
=
X
oldY
=
Y
End
If
End
Sub
版本二左键按下时可以拖动:
Dim
oldX!,
oldY!
'!是Single的缩写
Private
Sub
Command1_
MouseDown
(Button
As
Integer,
Shift
As
Integer,
X
As
Single,
Y
As
Single)
If
Button
=
1
Then
'1就是鼠标左键
oldX
=
X
oldY
=
Y
End
If
End
Sub
Private
Sub
Command1_MouseMove(Button
As
Integer,
Shift
As
Integer,
X
As
Single,
Y
As
Single)
If
Button
=
1
Then
Command1Left
=
Command1Left
-
oldX
+
X
Command1Top
=
Command1Top
-
oldY
+
Y
End
If
End
Sub

谁说不能拖动控件来移动窗体的。

这不就做出来了嘛:

using System;

using SystemDrawing;

using SystemWindowsForms;

namespace 可以拖动的工具箱 {

  public partial class 工具箱窗体 : Form {

      private Point 鼠标位置1, 鼠标位置2, 窗体位置1, 窗体位置2;

      private bool 按下鼠标 = false;

      public static bool 允许拖动 =true;

      private void 工具箱窗体_MouseDown(object sender, MouseEventArgs e) {

          if (允许拖动) {

              if (eButton == MouseButtonsLeft) {

                  按下鼠标 = true;

                  // Save window position and mouse position

                  鼠标位置1 = ControlMousePosition;

                  窗体位置1 = Location;

              }

          }

      }

      private void 工具箱窗体_MouseMove(object sender, MouseEventArgs e) {

          if (允许拖动 && 按下鼠标) {

              //Get the current position of the mouse in the screen

              鼠标位置2 = ControlMousePosition;

              //Set window position

              窗体位置2X = 鼠标位置2X - 鼠标位置1X + 窗体位置1X;

              窗体位置2Y = 鼠标位置2Y - 鼠标位置1Y + 窗体位置1Y;

              //Save window position

              Location = 窗体位置2;

              窗体位置1 = 窗体位置2;

              //Save mouse position

              鼠标位置1 = 鼠标位置2;

              坐标显示Visible = true;

              var p = thisPointToScreen(thisLocation);

              var rect = thisRectangleToScreen(Form1停靠面板Bounds);

              if (rectContains(p)) {

                  坐标显示Location = new Point(eX, eY - 坐标显示Height);

                  坐标显示BackColor = ColorDodgerBlue;

                  坐标显示Text = stringFormat("停靠");

              } else {

                  坐标显示Location = new Point(eX, eY - 坐标显示Height);

                  坐标显示BackColor = ColorLightSeaGreen;

                  坐标显示Text = stringFormat("{0:0},{1:0}", pX, pY);

              }

           

          }

      }

      private void 工具箱窗体_MouseUp(object sender, MouseEventArgs e) {

          if (允许拖动) {

              if (eButton == MouseButtonsLeft)

                  //Return back signal

                  按下鼠标 = false;

              //判断是否位于停靠区域

              var p = thisPointToScreen(thisLocation);

              var rect = thisRectangleToScreen(Form1停靠面板Bounds);

              if (rectContains(p)) {

                  允许拖动 = false;

                  thisHide();

                  Location = rectLocation;

                  Form1停靠面板Visible = true;

              }

              坐标显示Visible = false;

          }

      }

      public 工具箱窗体() {

          InitializeComponent();

      }

      private void 工具箱窗体_Load(object sender, EventArgs e) {

          FormBorderStyle = FormBorderStyleFixed3D;

          thisControlBox = false;

          thisShowIcon = false;

          thisShowInTaskbar = false;

          坐标显示Visible = false;

      }

  }

}

调用格式:[窗体名]Move 左边距离[,上边距离[,宽度[,高度]]]
例如:MeMove 100,200 (窗体移动到屏幕坐标100,200处,而窗体大小不变)
MeMove 100,200,2000,2200(窗体移动到屏幕坐标100,200处,而窗体大小改变,宽度为2000,高度为2200)

先定义类全局变量:
private int _x0;
private int _y0;
在窗体的MouseDown事件里写:
if (eButton == MouseButtonsLeft)
{
_x0 = eX;
_y0 = eY;
thisCapture = true;
}
在窗体的MouseMove事件里写:
if (eButton == MouseButtonsLeft && thisCapture)
{
int dx = eX - _x0;
int dy = eY - _y0;
thisLocation = new Point(thisLeft + dx, thisTop + dy);
}
在窗体的MouseUp事件里写:
if (eButton == MouseButtonsLeft)
{
thisCapture = false;
}
完整代码:
private int _x0;
private int _y0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (eButton == MouseButtonsLeft)
{
_x0 = eX;
_y0 = eY;
thisCapture = true;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (eButton == MouseButtonsLeft && thisCapture)
{
int dx = eX - _x0;
int dy = eY - _y0;
thisLocation = new Point(thisLeft + dx, thisTop + dy);
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (eButton == MouseButtonsLeft)
{
thisCapture = false;
}
}
//补充:
你说的那个“标题栏拖动有个虚线框框”,那个可以在windows里设置,在桌面上点右键选属性,打开显示 属性对话框,然后选择外观选项卡,点击效果,打开效果对话框,然后勾选拖动时显示窗口内容,确定,然后拖动窗体,你会发现没有虚线框了,

import javaawteventMouseAdapter;
import javaawteventMouseEvent;
import javaawteventMouseMotionAdapter;

import javaxswingJFrame;
public class test extends JFrame{
private int xx, yy;
private boolean isDraging = false;
public test(){
setUndecorated(true); // 没有标题栏
setSize(200, 200);
setVisible(true);
thisaddMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {

isDraging = true;

xx = egetX();

yy = egetY();
}

public void mouseReleased(MouseEvent e) {

isDraging = false;
}
});
thisaddMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {

if (isDraging) {

int left = getLocation()x;

int top = getLocation()y;

setLocation(left + egetX() - xx, top + egetY() - yy);

}
}
});
}
public static void main(String[] args) {
test t =new test();
tsetDefaultCloseOperation(3);
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/10540460.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-09
下一篇 2023-05-09

发表评论

登录后才能评论

评论列表(0条)

保存