日期:2018-04-30  浏览次数:2706 次

引入命名空间:using System.Runtime.InteropServices;

 

 导入库:
// 查找窗口句柄
[DllImport("user32.dll")]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 
 // 显示/隐藏窗口
[DllImport("user32.dll", EntryPoint = "ShowWindow")]
static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
 
 // 获取窗口信息
[DllImport("user32.dll", EntryPoint = "GetWindowLong")]
public static extern int GetWindowLong(IntPtr hwnd, int nIndex);
 
 // 设置窗口属性
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong(IntPtr hMenu, int nIndex, int dwNewLong);
 

添加最小化/最大化按钮:

注意:FindWindow()函数第2个参数表示程序运行后显示在标题栏(windows任务栏)的文字

此处我填“SRO_Client”(一款网络游戏,有认识的吗?o(╯□╰)o)

另:如果是网络游戏的话一般不需要添加最大化按钮,因为即使将游戏窗口最大化后画面是强制拉伸的。

//  添加最小化按钮处理事件

private void btnMinBox_Click(object sender, EventArgs e)
{
    // 查找程序窗口句柄
    IntPtr handle = FindWindow(null, “SRO_Client”);//SRO_Client是窗体的TEXT的名字,不是NAME名字
    if (handle == IntPtr.Zero)
    {
        MessageBox.Show("该任务不存在");
    }
    else
    {
        int GWL_STYLE = -16; // 该常量值表示窗口样式(通过查询API得到)
        //int WS_MAXIMIZEBOX = 0x00010000; // 窗口有最大化按钮
        int WS_MINIMIZEBOX = 0x00020000; // 窗口有最小化按钮
        
        int nStyle = GetWindowLong(handle, GWL_STYLE);
 
        //nStyle |= WS_MAXIMIZEBOX; 
        nStyle |= WS_MINIMIZEBOX;   
 
        SetWindowLong(handle, GWL_STYLE, nStyle);
 
    }
}