C#移动无边框的窗体怎么写。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#移动无边框的窗体怎么写。相关的知识,希望对你有一定的参考价值。

我想做一个类似QQ聊天窗口的东西,我把原来的标题栏弄掉了。窗口的最大化,最小化,还有窗口拖动都没有了。我想自己写一个拖动窗口怎么写?

下面是一个公用类,只用把你想作为无标题栏的窗体作为参数传递进来就可以了。你可以测试下。使用有问题,请追问/*
* (C) wyj_slsoft
* Author: SLsoft
* Description:拥有统一属性的登录窗体(或需要实现以后功能的窗体),包括无标题栏移动窗体,以及进入、退出特效,验证错误窗体抖动、窗体透明等
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing;namespace SLsoft

/// <summary>
/// 登陆窗体
/// </summary>
public class LoginForm

#region 全局变量
private int mouseX = 0;
private int mouseY = 0;
private Form login;
public Timer timer_shake;
int times = 0;
const int AW_HOR_POSITIVE = 0x0001;
const int AW_HOR_NEGATIVE = 0x0002;
const int AW_VER_POSITIVE = 0X0004;
const int AW_VER_NEGATIVE = 0x0008;
const int AW_CENTER = 0x0010;
const int AW_HIDE = 0x10000;
const int AW_ACTIVATE = 0x20000;
const int AW_SLIDE = 0x40000;
const int AW_BLEND = 0x80000;
#endregion [DllImport("user32")]
private static extern bool AnimateWindow(IntPtr hwnd, int dwTime, int dwFlags); [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
private static extern int GetWindowLong(HandleRef hWnd, int nIndex); [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
private static extern IntPtr SetWindowLong(HandleRef hWnd, int nIndex, int dwNewLong); /// <summary>
/// 登陆窗体构造函数
/// </summary>
/// <param name="form">需要进行处理的窗体</param>
public LoginForm(Form form)

login = form;
login.Load+=new EventHandler(login_Load);
login.FormClosing += new FormClosingEventHandler(Login_FormClosing);
login.MouseDown += new MouseEventHandler(Login_MouseDown);
login.MouseMove += new MouseEventHandler(Login_MouseMove);
login.BackColor = Color.Blue;
login.TransparencyKey = Color.Blue;
login.FormBorderStyle = FormBorderStyle.None; timer_shake = new Timer();
timer_shake.Interval = 100;
timer_shake.Tick+=new EventHandler(timer_shake_Tick);
private void Login_MouseDown(object sender, MouseEventArgs e)

mouseX = e.X;
mouseY = e.Y;
private void Login_MouseMove(object sender, MouseEventArgs e)

if (e.Button == MouseButtons.Left)

login.Location = new Point(Control.MousePosition.X - mouseX, Control.MousePosition.Y - mouseY);

private void login_Load(object sender, EventArgs e)

AnimateWindow(login.Handle, 550, AW_CENTER | AW_ACTIVATE);
int WS_SYSMENU = 0x00080000; int windowLong = (GetWindowLong(new HandleRef(login, login.Handle), -16));
SetWindowLong(new HandleRef(login, login.Handle), -16, windowLong | WS_SYSMENU);
SetWindowLong(new HandleRef(login, login.Handle), -16, WS_SYSMENU | WS_SYSMENU);//屏蔽最大化
// SetWindowLong(new HandleRef(this, this.Handle), -16, windowLong | WS_SYSMENU); //屏蔽最小化
private void Login_FormClosing(object sender, FormClosingEventArgs e)

AnimateWindow(login.Handle, 1000, AW_SLIDE | AW_HIDE | AW_VER_NEGATIVE);
Application.Exit();
private void timer_shake_Tick(object sender, EventArgs e)

times++;
if (times < 8)

for (int i = 0; i < 10; i++)

login.Location = new Point(login.Location.X + 1, login.Location.Y);

for (int i = 0; i < 10; i++)

login.Location = new Point(login.Location.X - 1, login.Location.Y);


else

times = 0;
timer_shake.Stop();


参考技术A 以鼠标左键单击窗体移动为例: private bool isMouseDown = false;//标记
private Point FormLocation; //form的location
private Point mouseOffset; //鼠标的按下位置 private int oldHeight = 0; private void Form1_MouseDown(object sender, MouseEventArgs e)

if (e.Button == MouseButtons.Left)

isMouseDown = true;
FormLocation = this.Location;
mouseOffset = Control.MousePosition;

private void Form1_MouseUp(object sender, MouseEventArgs e)

isMouseDown = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)

int _x = 0;
int _y = 0;
if (isMouseDown)

Point pt = Control.MousePosition;
_x = mouseOffset.X - pt.X;
_y = mouseOffset.Y - pt.Y; this.Location = new Point(FormLocation.X - _x, FormLocation.Y - _y);


protected override void WndProc(ref Message m)

const int WM_NCLBUTTONDBLCLK = 0x00A3;
switch (m.Msg)

case WM_NCLBUTTONDBLCLK:
if (oldHeight <= 0)

oldHeight = ClientSize.Height;
ClientSize = new Size(ClientSize.Width, 0);

else

ClientSize = new Size(ClientSize.Width, oldHeight);
oldHeight = 0;

return;

base.WndProc(ref m);
参考技术B bool IsLeftPressd = false;
int MouseLeft, MouseRight, oldX, oldY;//分别保存鼠标当前点击的x,y坐标,和当前窗体的x,y
private void Form1_MouseDown(object sender, MouseEventArgs e)

IsLeftPressd = true;//当鼠标左键按下设置为真,松开为假
oldX =this.Location .X ;//这些都是保存当前窗体位置的X
oldY = this.Location.Y;//这些都是保存当前窗体位置的X
MouseLeft = MousePosition;//这些都是保存当前鼠标位置的X
MouseRight = MousePosition .Y ;//这些都是保存当前鼠标位置的Y

private void Form1_MouseUp(object sender, MouseEventArgs e)

IsLeftPressd = false;//松开鼠标后设置为假
private void Form1_MouseMove(object sender, MouseEventArgs e)

if (IsLeftPressd)//判断是否按下了鼠标左键,且没有松开

this.Location = new Point(oldX + MousePosition.X - MouseLeft, oldY + MousePosition.Y - MouseRight);//将以前的窗体X,Y分别加上鼠标的偏移量!

如果还有疑问可以问我!祝你成功!

C#WinForm无边框窗体移动----模仿鼠标单击标题栏移动窗体位置

C#WinForm无边框窗体移动方法、模仿鼠标单击标题栏移动窗体位置

这里介绍俩种办法

方法一:直接通过修改窗体位置从而达到移动窗体的效果

方法二:直接伪装发送单击任务栏消息,让应用程序误以为单击任务栏从而移动窗体

新建窗体用于测试

技术图片

方法一

1.定义一个位置信息Point用于存储鼠标位置

1         private Point mPoint;

2.给窗体等控件增加MouseDown和MouseMove事件

技术图片
 1         /// <summary>
 2         /// 鼠标按下
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void panel1_MouseDown(object sender, MouseEventArgs e)
 7         
 8             mPoint = new Point(e.X, e.Y);
 9         
10 
11         /// <summary>
12         /// 鼠标移动
13         /// </summary>
14         /// <param name="sender"></param>
15         /// <param name="e"></param>
16         private void panel1_MouseMove(object sender, MouseEventArgs e)
17         
18             if (e.Button == MouseButtons.Left)
19             
20                 this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
21             
22         
技术图片

方法二:

1.引入下面代码 前提需要引入命名空间using System.Runtime.InteropServices

技术图片
1         [DllImport("user32.dll")]
2         public static extern bool ReleaseCapture();
3 
4         [DllImport("user32.dll")]
5         public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
6 
7         private const int VM_NCLBUTTONDOWN = 0XA1;//定义鼠标左键按下
8         private const int HTCAPTION = 2;
技术图片

2.增加鼠标按下事件发送消息,让系统误以为按下是标题栏

技术图片
 1         /// <summary>
 2         /// 鼠标按下
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void panel1_MouseDown(object sender, MouseEventArgs e)
 7         
 8             //为当前应用程序释放鼠标捕获
 9             ReleaseCapture();
10             //发送消息 让系统误以为在标题栏上按下鼠标
11             SendMessage((IntPtr)this.Handle, VM_NCLBUTTONDOWN, HTCAPTION, 0);
12         
技术图片

测试效果

 技术图片

动态图演示

技术图片

 

 源代码工程文件下载

 

出处:https://www.cnblogs.com/JiYF/p/9053596.html

以上是关于C#移动无边框的窗体怎么写。的主要内容,如果未能解决你的问题,请参考以下文章

C#无边框窗体移动的三种方法

C#中去掉winform边框如何拖动

C# winform鼠标移动到窗口给窗口加边框并获得句柄

WinForm 无边框窗体改变尺寸及移动窗体

无边框窗体和用户控件以及权限

form 无边框窗体移动