鳕鱼天空

This is Mr Wang's Tech Blog.

Winform实现无边框拖动的两种方式

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事件,即可。

C#获得本机的计算机名及IP地址

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;

 

C#知识库 外链列表存档

码农改变世界lyf 的 C#基础知识

C#基础知识-数据类型(一)

C#基础知识-编写第一个程序(二)

C#基础知识-基本的流程控制语句(三)

C#基础知识-流程控制的应用(四)

C#基础知识-函数的定义和调用(五)

C#基础知识-引用类型和值类型的区别(六)

C#基础知识-编程思想之封装(七)

C#基础知识-面向对象思想之继承(八)

C#基础知识-数组_ArrayList_List(九)

C#基础知识-XML介绍及基本操作(十)

C#基础知识-使用XML完成一个小程序(十一)

----------------------------------------------我是分割线-----------------------------

c# 检测操作系统版本

使用C#自动注册自定义文件类型

[转]C#中如何获取其他进程的命令行参数 ( How to get other processes's command line argument )

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。

  • WMI的C++例子:
    http://msdn.microsoft.com/zh-cn/aa394558

    http://msdn.microsoft.com/zh-cn/aa389762

 

None 模式Winform鼠标改变窗口大小

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;
    }
}