c# 在运行后form窗体随鼠标移动

yixiliuyun 2007-11-08 10:56:20
就是窗体随鼠标拖动 怎么实现
大侠帮忙~~~~~~
...全文
518 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
shinaterry 2007-11-14
  • 打赏
  • 举报
回复
用钓子比较好...

平民百姓 的做法比较另类, 值得一用...

^o^
love_cutezhou 2007-11-14
  • 打赏
  • 举报
回复
学习
北京的雾霾天 2007-11-14
  • 打赏
  • 举报
回复
在窗体里添加如下的代码:

internal static int WM_NCHITTEST = 0x84; //移动鼠标,按住或释放鼠标时发生的系统消息
internal static int WM_NCACTIVATE = 0x86;//窗体的激活状态发生改变的消息

internal static IntPtr HTCLIENT = (IntPtr)0x1;//工作区
internal static IntPtr HTSYSMENU = (IntPtr)3;//系统菜单
internal static IntPtr HTCAPTION = (IntPtr)0x2; //标题栏

internal static IntPtr HTLEFT = (IntPtr)10;//向左
internal static IntPtr HTRIGHT = (IntPtr)11;//向右
internal static IntPtr HTTOP = (IntPtr)12;//向上
internal static IntPtr HTTOPLEFT = (IntPtr)13;//向左上
internal static IntPtr HTTOPRIGHT = (IntPtr)14;//向右上
internal static IntPtr HTBOTTOM = (IntPtr)15;//向下
internal static IntPtr HTBOTTOMLEFT = (IntPtr)16;//向左下
internal static IntPtr HTBOTTOMRIGHT = (IntPtr)17;//向右下

private int m_BorderWidth = 4;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if (DesignMode)
{
return;
}
if (m.Result == HTCLIENT)
{
m.HWnd = this.Handle;

System.Drawing.Rectangle rect = this.RectangleToScreen(this.ClientRectangle);
Point C_Pos = Cursor.Position;
if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
m.Result = HTTOPLEFT;//左上
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y <= rect.Top + m_BorderWidth))
m.Result = HTTOPRIGHT;//右上
else if ((C_Pos.X <= rect.Left + m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOMLEFT;//左下
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth) && (C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOMRIGHT;//右下
else if ((C_Pos.X <= rect.Left + m_BorderWidth - 1))
m.Result = HTLEFT;//左
else if ((C_Pos.X >= rect.Left + rect.Width - m_BorderWidth))
m.Result = HTRIGHT;//右
else if ((C_Pos.Y <= rect.Top + m_BorderWidth - 1))
m.Result = HTTOP;//上
else if ((C_Pos.Y >= rect.Top + rect.Height - m_BorderWidth))
m.Result = HTBOTTOM;//下
else
{
m.Result = HTCAPTION;//模拟标题栏,移动或双击可以最大或最小化窗体
}
}
return;
}
base.WndProc(ref m);
}
yixiliuyun 2007-11-14
  • 打赏
  • 举报
回复
对不起了 啊 刚刚看到贴子

新蛋小飞侠 的方法试过了
我感觉也应该是这样子的
但不知道为什么 在拖动form时 还是拖动不了的

在调试的时候 这个事件是触发不了的

????
shrinerain 2007-11-14
  • 打赏
  • 举报
回复
你需要用到两个函数

1.获取鼠标位置GetCursorPos
2.移动窗体SetWindowPos
yixiliuyun 2007-11-14
  • 打赏
  • 举报
回复
平民百姓

这个我要慢慢看看

看不懂了 ^_^
John_Yang 2007-11-09
  • 打赏
  • 举报
回复

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

namespace forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
this.Location = new Point(Control.MousePosition.X - 100, Control.MousePosition.Y - 50);
}
}
}
LPGG 2007-11-09
  • 打赏
  • 举报
回复
我想LZ说的不是一个程序的事情,而是如何根据鼠标位置将自己的FORM强制跟随的问题。

如果程序是启动时运行,但运行后并不一定处于激活状态,那么写在程序里的程序可能就不会被执行,因此也就不能跟随鼠标了。

应该是这样,先想办法在系统最高权限级别拥有入口,比如写个高级别的WINDOWS SERVICE,HOOK到鼠标移动API,然后再调动FORM到鼠标处。

但这样会有个问题,如果LZ的FORM本身就有800X600或1024X768,那么在这样的分辨率下还让不让人活了?呵呵······

另外,如何在需要对该FORM内执行鼠标动作时不再让FORM跟随鼠标移动,也是个需要解决的问题。

浅见~请指正······嘻嘻······
  • 打赏
  • 举报
回复
好像不行,如果鼠标不在这Form里根本捕捉不到这个事件。关注
applethink 2007-11-09
  • 打赏
  • 举报
回复
楼上应该可以。
John_Yang 2007-11-08
  • 打赏
  • 举报
回复
捕获鼠标位置,然后设置窗口的位置为鼠标的位置.
自定义窗体的最大化、最小化和关闭按钮, C#移动无标题栏窗体的三种代码: C#移动无标题栏窗体的三种代码:第一种采用,需注意窗体上的控件是否把窗体覆盖了。。。MouseDown、MouseMove、MouseUp事件应该是鼠标所处位置最顶层的控件的事件 在窗体的类中声明两个变量 private Point mouseOffset; //记录鼠标指针的坐标 private bool isMouseDown = false; //记录鼠标按键是否按下 创建该窗体 MouseDown、MouseMove、MouseUp事件的相应处理程序 private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { int xOffset; int yOffset; if (e.Button == MouseButtons.Left) { xOffset = -e.X ; yOffset = -e.Y ; mouseOffset = new Point(xOffset, yOffset); isMouseDown = true; } } private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e) { if (isMouseDown) { Point mousePos = Control.MousePosition; mousePos.Offset(mouseOffset.X, mouseOffset.Y); Location = mousePos; } } private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { // 修改鼠标状态isMouseDown的值 // 确保只有鼠标左键按下并移动时,才移动窗体 if (e.Button == MouseButtons.Left) { isMouseDown = false; } } 第二种调用API 未验证 using System.Runtime.InteropServices; [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); public const int WM_SYSCOMMAND = 0x0112; public const int SC_MOVE = 0xF010; public const int HTCAPTION = 0x0002; private void Form1_MouseDown(object sender, MouseEventArgs e) { ReleaseCapture(); SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0); } 第三种未验证 private bool isMouseDown = false; private Point FormLocation; //form的location private Point mouseOffset; //鼠标的按下位置 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); [DllImport("user32.dll")] public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息 private const int SC_MOVE = 0xF010;//移动信息 private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息 private const int WM_NCHITTEST = 0x84;//鼠标窗体客户区(除了标题栏和边框以外的部分)时发送的消息 private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息 private const int SC_MAXIMIZE = 0xF030;//最大化信息 private const int SC_MINIMIZE = 0xF020;//最小化信息 protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_SYSCOMMAND: if (m.WParam == (IntPtr)SC_MAXIMIZE) { m.WParam = (IntPtr)SC_MINIMIZE; } break; case WM_NCHITTEST: //如果鼠标移动或单击 base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息 if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT { m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION return;//直接返回退出方法 } break; } base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理 } private void Form1_Load(object sender, EventArgs e) { } ------------------------------- 如何在窗体标题栏左边的控制菜单加入自己的菜单啊? 我们一般在窗口标题栏点右键 或 按Alt+空格 可以弹出那个菜单。 ------解决方案-------------------- using System.Runtime.InteropServices; [DllImport( "user32.dll ")] public static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport( "user32.dll ")] public static extern bool InsertMenu(IntPtr hMenu, uint uPosition, uint uFlags, uint uIDNewItem, string lpNewItem); public const int MF_BYCOMMAND = 0; public const int MF_STRING = 0; public const int MF_BYPOSITION = 0x400; public const int MF_SEPARATOR = 0x800; private const uint SC_ABOUT = 0x0001; public const int WM_SYSCOMMAND = 0x0112; private void Form1_Load(object sender, EventArgs e) { IntPtr vMenuHandle = GetSystemMenu(Handle, false); InsertMenu(vMenuHandle, 255, MF_STRING, SC_ABOUT, "About... "); } protected override void WndProc(ref Message m) { switch (m.Msg) { case WM_SYSCOMMAND: if ((uint)m.WParam == SC_ABOUT) { MessageBox.Show( "Zswang 路过! "); } break; } base.WndProc(ref m); }

110,539

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧