c# 使用api函数 ShowWindowAsync 控制窗体
2014-09-29 17:12
288 查看
1.需要匯入 System.Runtime.InteropServices 命名空間
2.宣告 ShowWindowAsync 函數
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(
IntPtr hWnd,
int nCmdShow
);
3.宣告 ShowWindow函數
[DllImport("user32.dll")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
4.宣告API常數定義
//API 常數定義
private const int SW_HIDE = 0;
private const int SW_NORMAL = 1;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
5.上述函數功能相同,都是用來設定視窗大小,不同的是宣告的型態不一樣需轉型。
ShowWindowAsync(this.Handle, SW_MINIMIZE);
ShowWindow((int)this.Handle, SW_MINIMIZE);
6.若是把int 改成IntPtr ,使用ShowWindow就不用轉型,所以在宣告時就可以考慮資料型態,必免轉型所耗的資源。
[DllImport("user32.dll")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
C#完整範例
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CS_WindowsResize
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//API 常數定義
private const int SW_HIDE = 0;
private const int SW_NORMAL = 1;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(
IntPtr hWnd,
int nCmdShow
);
[DllImport("user32.dll")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
private void button1_Click(object sender, EventArgs e)
{
//最小化
ShowWindowAsync(this.Handle, SW_MINIMIZE);
}
private void button2_Click(object sender, EventArgs e)
{
//最大化
ShowWindowAsync(this.Handle, SW_MAXIMIZE);
}
private void button3_Click(object sender, EventArgs e)
{
//還原
ShowWindowAsync(this.Handle, SW_RESTORE);
}
private void button4_Click(object sender, EventArgs e)
{
//最小化
ShowWindow((int)this.Handle, SW_MINIMIZE);
}
private void button5_Click(object sender, EventArgs e)
{
//最大化
ShowWindow((int)this.Handle, SW_MAXIMIZE);
}
private void button6_Click(object sender, EventArgs e)
{
//還原
ShowWindow((int)this.Handle, SW_RESTORE);
}
}
}
转载自:http://www.dotblogs.com.tw/yc421206/archive/2009/07/06/9140.aspx
2.宣告 ShowWindowAsync 函數
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(
IntPtr hWnd,
int nCmdShow
);
3.宣告 ShowWindow函數
[DllImport("user32.dll")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
4.宣告API常數定義
//API 常數定義
private const int SW_HIDE = 0;
private const int SW_NORMAL = 1;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
5.上述函數功能相同,都是用來設定視窗大小,不同的是宣告的型態不一樣需轉型。
ShowWindowAsync(this.Handle, SW_MINIMIZE);
ShowWindow((int)this.Handle, SW_MINIMIZE);
6.若是把int 改成IntPtr ,使用ShowWindow就不用轉型,所以在宣告時就可以考慮資料型態,必免轉型所耗的資源。
[DllImport("user32.dll")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
C#完整範例
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CS_WindowsResize
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//API 常數定義
private const int SW_HIDE = 0;
private const int SW_NORMAL = 1;
private const int SW_MAXIMIZE = 3;
private const int SW_SHOWNOACTIVATE = 4;
private const int SW_SHOW = 5;
private const int SW_MINIMIZE = 6;
private const int SW_RESTORE = 9;
private const int SW_SHOWDEFAULT = 10;
[DllImport("user32.dll")]
private static extern bool ShowWindowAsync(
IntPtr hWnd,
int nCmdShow
);
[DllImport("user32.dll")]
public static extern int ShowWindow(
int hwnd,
int nCmdShow
);
private void button1_Click(object sender, EventArgs e)
{
//最小化
ShowWindowAsync(this.Handle, SW_MINIMIZE);
}
private void button2_Click(object sender, EventArgs e)
{
//最大化
ShowWindowAsync(this.Handle, SW_MAXIMIZE);
}
private void button3_Click(object sender, EventArgs e)
{
//還原
ShowWindowAsync(this.Handle, SW_RESTORE);
}
private void button4_Click(object sender, EventArgs e)
{
//最小化
ShowWindow((int)this.Handle, SW_MINIMIZE);
}
private void button5_Click(object sender, EventArgs e)
{
//最大化
ShowWindow((int)this.Handle, SW_MAXIMIZE);
}
private void button6_Click(object sender, EventArgs e)
{
//還原
ShowWindow((int)this.Handle, SW_RESTORE);
}
}
}
转载自:http://www.dotblogs.com.tw/yc421206/archive/2009/07/06/9140.aspx
相关文章推荐
- c# 使用api函数 ShowWindowAsync 控制窗体
- c#中跨线程调用windows窗体控件 .我们在做winform应用的时候,大部分情况下都会碰到使用多线程控制界面上控件信息的问题。然而我们并不能用传统方法来做这个问题,下面我将详细的介绍。
- C#中使用API函数FINDWINDOW查找窗体
- 在C#中使用COM+实现事务控制
- 使用C#控制远程计算机的服务
- C# 使用重载消息处理函数的方式,实现没有标题栏的窗体的拖动。
- 在C#中使用COM+实现事务控制
- 使用 Override 和 New 关键字进行版本控制(C# 编程指南)
- 使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- 在C#中使用COM+实现事务控制
- C#窗体控制源代码
- zt在C#中使用COM+实现事务控制
- c#使用线程下载文件的控制技巧和缺陷
- 在C#中使用.NET SDK创建控制
- 使用C#控制远程计算机的服务
- 使用API函数播放MP3等音频文件的类设计(C#)
- 使用C#控制远程计算机的服务
- 如何使用 C# .NET 在 ASP.NET 应用程序中实现基于窗体的身份验证
- 使用C#控制远程计算机的服务
- 在C#中使用COM+实现事务控制(2)