Csharp 如何获取电脑菜单栏像素高度的代码
时间: 2023-06-02 17:07:02 浏览: 45
以下是获取电脑菜单栏像素高度的C#代码:
``` csharp
int menuBarHeight = SystemInformation.MenuHeight;
```
这个代码使用 SystemInformation 类的 MenuHeight 属性来获取电脑菜单栏的像素高度。
相关问题
csharp 获取桌面任务栏像素高度的代码
以下是获取桌面任务栏高度的 C# 代码:
```csharp
using System;
using System.Runtime.InteropServices;
namespace DesktopTaskbarHeight
{
class Program
{
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern IntPtr GetWindow(IntPtr hWnd, int uCmd);
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);
private const int SM_CYSCREEN = 1;
private const int SM_CYCAPTION = 4;
private const int SM_CYSIZEFRAME = 33;
private const int SM_CYBORDER = 6;
private const int SM_CYMINSPACING = 48;
static void Main(string[] args)
{
IntPtr taskbarHandle = FindWindow("Shell_TrayWnd", null);
IntPtr taskbarSizeHandle = GetWindow(taskbarHandle, 5);
int height = GetSystemMetrics(SM_CYSCREEN) - GetSystemMetrics(SM_CYCAPTION) -
2 * GetSystemMetrics(SM_CYSIZEFRAME) - GetSystemMetrics(SM_CYBORDER) -
GetSystemMetrics(SM_CYMINSPACING) - taskbarSizeHandle.Height;
Console.WriteLine($"Taskbar height: {height}px");
}
}
}
```
这段代码通过 PInvoke 调用了 user32.dll 中的一些函数,其中 `FindWindow` 函数获取任务栏窗口句柄,`GetWindow` 函数获取任务栏按钮组窗口句柄,`GetSystemMetrics` 函数获取系统参数。最终计算出任务栏的高度,并输出到控制台。
Csharp 在winform最大化的时候如何不遮挡菜单栏的代码
可以在窗体的Resize事件中判断窗体状态是否为最大化,如果是则将菜单栏放到最前面:
```
private void Form1_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Maximized)
{
menuStrip1.BringToFront();
}
}
```
其中menuStrip1是菜单栏的控件名。