admin管理员组

文章数量:1516870

实现步骤

  1. 为了能够控制Windows任务栏,我们需要利用Windows API提供的功能。具体来说,我们会使用到 user32.dll 中的两个函数: FindWindow ShowWindow 。这两个函数可以帮助我们找到任务栏窗口,并对其执行显示或隐藏的操作

  2. 引入命名空间 :首先,我们在项目中引入 System.Runtime.InteropServices 命名空间,以便能够调用非托管代码(即Windows API)。

  3. 声明API函数 :接着,我们需要声明将要使用的API函数。

模块代码:

using System.Runtime.InteropServices;
 [DllImport("user32.dll")]
 private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 [DllImport("user32.dll")]
 private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 // 定义常量
 private const int SW_HIDE = 0;
 private const int SW_SHOW = 5;
 /// <summary>
 /// 隐藏任务栏
 /// </summary>
 public void HideTaskbar()
 {
     var handle = FindWindow("Shell_TrayWnd", null);
     if (handle != IntPtr.Zero)
     {
         ShowWindow(handle, SW_HIDE); // 隐藏任务栏
     }
 }
 /// <summary>
 /// 显示任务栏
 /// </summary>
 public void ShowTaskbar()
 {
     var handle = FindWindow("Shell_TrayWnd", null);
     if (handle != IntPtr.Zero)
     {
         ShowWindow(handle, SW_SHOW); // 显示任务栏
     }
 }

调用方法

private void button1_Click(object sender, EventArgs e)
 {
     HideTaskbar();
 }
 private void button2_Click(object sender, EventArgs e)
 {
     ShowTaskbar();
 }

参考连接

特此记录

anlog

2024年10月29日

本文标签: 函数系统编程