windwos api 实现 showintaskbar
时间: 2023-08-04 12:07:16 浏览: 58
在 Windows API 中,可以使用 SetWindowLongPtr 函数来设置窗口样式。要将窗口显示在任务栏上,可以将窗口样式设置为 WS_EX_APPWINDOW。设置窗口样式后,您需要使用 SetWindowPos 函数将窗口放置在正确的位置。
以下是一个示例代码:
```
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
const int GWLP_EXSTYLE = -20;
const int WS_EX_APPWINDOW = 0x00040000;
const uint SWP_NOSIZE = 0x0001;
const uint SWP_NOMOVE = 0x0002;
const uint SWP_NOZORDER = 0x0004;
const uint SWP_SHOWWINDOW = 0x0040;
private void ShowInTaskbar(IntPtr hWnd)
{
// 设置窗口样式为 WS_EX_APPWINDOW
IntPtr oldStyle = GetWindowLongPtr(hWnd, GWLP_EXSTYLE);
SetWindowLongPtr(hWnd, GWLP_EXSTYLE, oldStyle | (IntPtr)WS_EX_APPWINDOW);
// 将窗口放置在正确的位置
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW);
}
```
请注意,此代码仅适用于 Windows 窗口应用程序,而不适用于 WPF 应用程序。如果您使用的是 WPF 应用程序,则需要使用其他方法来向任务栏显示窗口。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)