Winform作为桌面应用程序的一个代表,可以说是C#初学者快速入门最好的一种方式,然而随着我们的开发能力的提升,我们对UI界面的要求变得越来越高,于是我们开始不断优化、升级我们的UI界面。
Winform无边框设计是我们常用的一种方式,无边框设计意味着没有了应用程序的标题栏,也就无法直接拖动,今天主要介绍两种实现无边框拖动的方式。
第一种方式:代码实现
Point mPoint;
private void Panel_MouseDown(object sender, MouseEventArgs e)
{
mPoint = new Point(e.X, e.Y);
}
private void Panel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Location = new Point(this.Location.X + e.X - mPoint.X, this.Location.Y + e.Y - mPoint.Y);
}
}
this.MouseDown += Form_MouseDown;
this.MouseUp += Form_MouseUp;
this.MouseMove += Form_MouseMove;
这种方式使用的是自己写代码实现,将这段代码复制到界面代码中,然后选择窗体中的一个控件,比如Panel或者Label,将它们的MouseDown事件绑定代码中的MouseDown事件,MouseMove事件绑定代码中的MouseMove事件,即可。
第二种方式:Windows API
[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 TopPanel_MouseDown(object sender, MouseEventArgs e)
{
ReleaseCapture();
SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
}
这种方式使用的是Windows底层的API函数,同样将这段代码复制到界面代码中,然后选择窗体中的一个控件,比如Panel或者Label,将它们的MouseDown事件绑定代码中的MouseDown事件,即可。
using System.Net;
String hostInfo =Dns.GetHostName();//获取本机的计算机名
this.ComputerTextBox.Text = hostInfo;
System.Net.IPAddress addr;
addr = new System.Net.IPAddress(Dns.GetHostByName(Dns.GetHostName()).AddressList[0].Address);
String IPAddress = addr.ToString();//获取本机的IP地址
this.ipTextBox.Text = IPAddress;
private static IEnumerable<string> GetCommandLines(string processName)
{
List<string> results = new List<string>();
string wmiQuery = string.Format("select CommandLine from Win32_Process where Name='{0}'", processName);
using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(wmiQuery))
{
using (ManagementObjectCollection retObjectCollection = searcher.Get())
{
foreach (ManagementObject retObject in retObjectCollection)
{
results.Add((string)retObject["CommandLine"]);
}
}
}
return results;
}
static void Main(string[] args)
{
var result = GetCommandLines("msvsmon.exe");
Console.Read();
}
任务管理器中实际的参数如下, 该程序或得到3个item的string。
const int WM_NCHITTEST = 0x0084;
const int HTLEFT = 10;
const int HTRIGHT = 11;
const int HTTOP = 12;
const int HTTOPLEFT = 13;
const int HTTOPRIGHT = 14;
const int HTBOTTOM = 15;
const int HTBOTTOMLEFT = 0x10;
const int HTBOTTOMRIGHT = 17;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCHITTEST:
Point vPoint = new Point((int)m.LParam & 0xFFFF,
(int)m.LParam >> 16 & 0xFFFF);
vPoint = PointToClient(vPoint);
if (vPoint.X <= 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)HTTOPLEFT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)HTBOTTOMLEFT;
else m.Result = (IntPtr)HTLEFT;
else if (vPoint.X >= ClientSize.Width - 5)
if (vPoint.Y <= 5)
m.Result = (IntPtr)HTTOPRIGHT;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)HTBOTTOMRIGHT;
else m.Result = (IntPtr)HTRIGHT;
else if (vPoint.Y <= 5)
m.Result = (IntPtr)HTTOP;
else if (vPoint.Y >= ClientSize.Height - 5)
m.Result = (IntPtr)HTBOTTOM;
break;
}
}