2 dandan rensheng dandan_rensheng 于 2015.07.12 08:52 提问

如何通过单击鼠标左键来移动panel的位置

我在窗体中创建了两个panel(ScreenBox和SScreenBox),其中SScreenBox在ScreenBox内。
我想实现的功能是在ScreenBox中单击鼠标左键,可以将SScreenBox移动到鼠标的位置。
怎么怎么实现啊,是要生成ScreenBox的MouseClick事件吗?要详细代码!谢谢!!!急!!!!!

5个回答

tongyi55555
tongyi55555   2015.07.12 09:08

是Java的JPanel么?是的话就是监听ScreenBox的鼠标事件。

dandan_rensheng
dandan_rensheng 不是,我用的是C#
大约 3 年之前 回复
suiyunonghen
suiyunonghen   2015.07.12 09:27

MouseDown,然后获得鼠标位置,然后设置位置就行了

dandan_rensheng
dandan_rensheng   2015.07.12 09:50

@不得闲 我用的是MouseClick
//点击鼠标时,选择框移动到指定位置
private void ScreenBox_MouseClick(object sender, MouseEventArgs e)
{
Point MousePoint=Control.MousePosition;
MousePoint.Offset(this.Location.X,this.Location.Y);
if(e.Button==MouseButtons.Left)
{
if ((MousePoint.X + SScreenBox.Width / 2) <= ScreenBox.Right)
SScreenBox.Left = MousePoint.X - SScreenBox.Width / 2;
else
SScreenBox.Left = ScreenBox.Right - SScreenBox.Width;
if ((MousePoint.Y + SScreenBox.Height / 2) <= ScreenBox.Bottom)
SScreenBox.Top = MousePoint.Y - SScreenBox.Height / 2;
else
SScreenBox.Top = ScreenBox.Bottom - SScreenBox.Height;
}

但是不行,只响应一次,错误的而且移动的位置

caozhy
caozhy   Ds   Rxr 2015.07.12 15:16
CSDNXIAOD
CSDNXIAOD   2015.07.16 15:23

模拟鼠标移动和左键单击
C# 鼠标移动到指定位置,并且单击
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
C#在窗体中点击任意位置鼠标左键获得该点坐标
private void Form1_MouseClick(object sender, MouseEventArgs e) { Point p = e.Location; MessageBox.Show(p.ToString()); }
QT 实现按住鼠标左键点击对话框空白处,拖动对话框
定义头文件     QPoint move_point; //移动的距离     bool mouse_press; //按下鼠标左键 protected:     void mousePressEvent(QMouseEvent *event);     void mouseReleaseEvent(QMouseEvent *event);     void mouseMoveEve
vb6.0 api 实现鼠标指定位置 按下鼠标左键并抬起
例:单击Command1时鼠标会到(500,500)这个点作鼠标左键单击动作。 Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As L
selenium在制定元素进行鼠标单击左键和释放的操作
1.html源码 function mouseDownFun() { document.getElementById('div1').innerHTML += '鼠标左键被按下'; } function mouseUpFun() { document.getElementById('div1').innerHTML += '已经被按下的鼠标左键被释放抬起' } fun
delphi7模拟鼠标移动和单击功能
oldPoint,newPoint:TPOINT       GetCursorPos(oldPoint); //保存当前鼠标位置。       newPoint.x = oldPoint.x+40;       newPoint.y = oldPoint.y+10;       SetCursorPos(newPoint.x,newPoint.y); //设置目的地位置。      
Unity3d 实现鼠标左键点击地形使角色移动到指定地点[脚本]
其中涉及,移动速度,主角对象,目标点,射线碰撞等!~   var moveSpeed:int=5;//player移动速度 var player:Transform;//定义一个人物的Transform private var endposition : Vector3; function Start() { endposition = player.transform.pos
编写一个windows应用程序,要求在窗口的用户区中绘制一个圆 ,当单击左键时,该圆放大;单击右键时,该圆缩小;按下ctrl键时的同时鼠标移动,则该圆会随鼠标移动而移动
编写一个windows应用程序,要求在窗口的用户区中绘制一个圆 ,当单击左键时,该圆放大;单击右键时,该圆缩小;按下ctrl键时的同时鼠标移动,则该圆会随鼠标移动而移动。 1.建立一个SDI,名字为MFCexp7_9 2.在文档类中添加一个成员     CRect m_Ell; 3.在文档类的构造函数中对成员初始化 CMy7_9Doc::CMy7_9Doc() :
selenium_webdriver(python)鼠标操作详解
#coding=utf-8 from selenium import webdriver from selenium.webdriver.common.keys import Keys #要使用鼠标操作,首先需要引入ActionChains包 from selenium.webdriver.common.action_chains import ActionChains import t
【VS2010 C# 代码】实时监控鼠标位置和左键点击时的位置
Microsoft Visual Studio 2010做的C#实时监控鼠标位置和左键点击时的位置实例,主要代码: public class MouseHook { private Point point; private Point Point { get { return point; } set { if (point != value) { point = value; if (MouseMoveEvent != null) { var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0); MouseMoveEvent(this, e); } } } } private int hHook; private const int WM_LBUTTONDOWN = 0x201; public const int WH_MOUSE_LL = 14; public Win32Api.HookProc hProc; public MouseHook() { this.Point = new Point(); } public int SetHook() { hProc = new Win32Api.HookProc(MouseHookProc); hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return hHook; } public void UnHook() { Win32Api.UnhookWindowsHookEx(hHook); } private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct)); if (nCode < 0) { return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } else { if (MouseClickEvent != null) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch ((Int32)wParam) { case WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; } var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0); MouseClickEvent(this, e); } this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y); return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } } public delegate void MouseMoveHandler(object sender, MouseEventArgs e); public event MouseMoveHandler MouseMoveEvent; public delegate void MouseClickHandler(object sender, MouseEventArgs e); public event MouseClickHandler MouseClickEvent; }
让小球向鼠标单击的方向前进
通过获取用户单击鼠标左键的位置信息,来引导小球的前进方向,右键单击则停止。