我们来比较winform窗体的this方式和win32api handle方式实现窗体的最大化、默认窗体、半透明、不透明的区别
1、窗体界面设计
this方式按钮: btnMaxWindow、 btnNormalWindow、btnOpaWindow 、btnOpacityWindow
handle方式按钮: btnMaxWindow1、 btnNormalWindow1、btnOpaWindow1 、btnOpacityWindow1
2、使用句柄(handle)
首先在项目中引入所需win32api的函数方法
/// <summary> /// windowapi 找到指定窗体的句柄函数 /// </summary> /// <param name="lpClassName">窗体类名</param> /// <param name="lpWindowName">窗体标题名</param> /// <returns>返回窗体句柄(IntPtr)</returns> [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// windowapi 通过句柄显示或隐藏窗体函数 /// </summary> /// <param name="hWnd">窗体句柄</param> /// <param name="cmdShow">显示类型(0:隐藏窗体,1:默认大小窗体,2:最小化窗体,3:最大化窗体)</param> /// <returns>返回成功或失败</returns> [DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)] public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// <summary> /// 设置窗体透明度属性 /// </summary> /// <param name="hwnd">窗体句柄</param> /// <param name="crKey">掩码颜色</param> /// <param name="bAlpha">不透明度</param> /// <param name="dwFlags">透明方式</param> /// <returns></returns> [DllImport("user32.dll", SetLastError = true)] static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
3、比较this和handle的区别
1) 最大化窗体
#region 设置窗体最大化(this、handle) /// <summary> /// this方式 设置窗体最大化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMaxWindow_Click(object sender, EventArgs e) { //设置窗体状态为最大化 this.WindowState = FormWindowState.Maximized; //不显示在任务栏 this.ShowInTaskbar = false; //设置窗体为不可见 // this.Visible = true; } /// <summary> /// handle方式 通过句柄设置窗体最大化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMaxWindow1_Click(object sender, EventArgs e) { //获取窗体句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,最大化窗体 ShowWindowAsync(handle, 3); } } #endregion
2)默认窗体
#region 设置窗体默认大小(this、handle) /// <summary> /// this方式 设置窗体默认大小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNormalWindow_Click(object sender, EventArgs e) { //设置窗体状态为默认大小 this.WindowState = FormWindowState.Normal; //不显示在任务栏 this.ShowInTaskbar = false; //设置窗体为不可见 //this.Visible = false; } /// <summary> /// handle方法 设置窗体默认大小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNormalWindow1_Click(object sender, EventArgs e) { //获取窗体句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,弹出窗体默认大小 ShowWindowAsync(handle, 1); } } #endregion
3)半透明窗体
#region 设置窗体为半透明(this、handle) /// <summary> /// this方式 设置窗体透明度为半透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpaWindow_Click(object sender, EventArgs e) { //设置当前窗体为不透明度为0.5 this.Opacity = 0.5; } /// <summary> /// handle方式 设置窗体透明度为半透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpaWindow1_Click(object sender, EventArgs e) { //通过win32FindWindow()函数,获取窗体的句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,设置窗体透明度为半透明(第一个参数:窗体句柄;第二参数:掩码颜色;第三个参数:透明度,范围0-255) SetLayeredWindowAttributes(handle, 0, 128, 0x00000002); } } #endregion
4)不透明窗体
#region 设置窗体为不透明(this、handle) /// <summary> /// this方式 设置窗体为不透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpacityWindow_Click(object sender, EventArgs e) { //设置窗体为不透明(0~1) this.Opacity = 1; } /// <summary> /// handle方式 设置窗体为不透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpacityWindow1_Click(object sender, EventArgs e) { //通过win32FindWindow()函数,获取窗体的句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,设置窗体透明度为不透明(第一个参数:窗体句柄;第二参数:掩码颜色;第三个参数:透明度,范围0-255) SetLayeredWindowAttributes(handle, 0, 255, 0x00000002); } } #endregion
4、完整源码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsForms { public partial class Form1 : Form { /// <summary> /// windowapi 找到指定窗体的句柄函数 /// </summary> /// <param name="lpClassName">窗体类名</param> /// <param name="lpWindowName">窗体标题名</param> /// <returns>返回窗体句柄(IntPtr)</returns> [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); /// <summary> /// windowapi 通过句柄显示或隐藏窗体函数 /// </summary> /// <param name="hWnd">窗体句柄</param> /// <param name="cmdShow">显示类型(0:隐藏窗体,1:默认大小窗体,2:最小化窗体,3:最大化窗体)</param> /// <returns>返回成功或失败</returns> [DllImport("user32.dll", EntryPoint = "ShowWindowAsync", SetLastError = true)] public static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow); /// <summary> /// 设置窗体透明度属性 /// </summary> /// <param name="hwnd">窗体句柄</param> /// <param name="crKey">掩码颜色</param> /// <param name="bAlpha">不透明度</param> /// <param name="dwFlags">透明方式</param> /// <returns></returns> [DllImport("user32.dll", SetLastError = true)] static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); public Form1() { InitializeComponent(); //第一次运行弹出显示在屏幕中间 this.StartPosition = FormStartPosition.CenterScreen; } #region 设置窗体最大化(this、handle) /// <summary> /// this方式 设置窗体最大化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMaxWindow_Click(object sender, EventArgs e) { //设置窗体状态为最大化 this.WindowState = FormWindowState.Maximized; //不显示在任务栏 this.ShowInTaskbar = true; //设置窗体为不可见 // this.Visible = true; } /// <summary> /// handle方式 通过句柄设置窗体最大化 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnMaxWindow1_Click(object sender, EventArgs e) { //获取窗体句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,最大化窗体 ShowWindowAsync(handle, 3); } } #endregion #region 设置窗体默认大小(this、handle) /// <summary> /// this方式 设置窗体默认大小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNormalWindow_Click(object sender, EventArgs e) { //设置窗体状态为默认大小 this.WindowState = FormWindowState.Normal; //不显示在任务栏 this.ShowInTaskbar = false; //设置窗体为不可见 //this.Visible = false; } /// <summary> /// handle方法 设置窗体默认大小 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnNormalWindow1_Click(object sender, EventArgs e) { //获取窗体句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,弹出窗体默认大小 ShowWindowAsync(handle, 1); } } #endregion #region 设置窗体为半透明(this、handle) /// <summary> /// this方式 设置窗体透明度为半透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpaWindow_Click(object sender, EventArgs e) { //设置当前窗体为不透明度为0.5 this.Opacity = 0.5; } /// <summary> /// handle方式 设置窗体透明度为半透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpaWindow1_Click(object sender, EventArgs e) { //通过win32FindWindow()函数,获取窗体的句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,设置窗体透明度为半透明(第一个参数:窗体句柄;第二参数:掩码颜色;第三个参数:透明度,范围0-255) SetLayeredWindowAttributes(handle, 0, 128, 0x00000002); } } #endregion #region 设置窗体为不透明(this、handle) /// <summary> /// this方式 设置窗体为不透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpacityWindow_Click(object sender, EventArgs e) { //设置窗体为不透明(0~1) this.Opacity = 1; } /// <summary> /// handle方式 设置窗体为不透明 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOpacityWindow1_Click(object sender, EventArgs e) { //通过win32FindWindow()函数,获取窗体的句柄 IntPtr handle = FindWindow(null, "测试窗体"); if (handle != IntPtr.Zero) { //通过句柄,设置窗体透明度为不透明(第一个参数:窗体句柄;第二参数:掩码颜色;第三个参数:透明度,范围0-255) SetLayeredWindowAttributes(handle, 0, 255, 0x00000002); } } #endregion } }
5、演示窗体顺序
(1)先按this方式的最大化窗体,handle方式恢复默认1
(2)先按handle方式的最大化窗体1,this方式恢复默认
(3)先按this方式的半透明窗体,handle方式不透明窗体1
(4)先按handle方式的半透明窗体1,this方式不透明窗体
演示的效图
按了this方式的半透明按钮
按钮handle方式不透明按钮1