windows 我如何找到任务栏的高度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3644531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:11:35  来源:igfitidea点击:

How would I find the height of the task bar?

c++windowsapiheighttaskbar

提问by Alexander Rafferty

In my windows application, I am trying to find the height of the task bar. While I can hard program this into my program, I would like to find it programmatically to support past, present (win7) and future windows versions.

在我的 Windows 应用程序中,我试图找到任务栏的高度。虽然我可以将其硬编程到我的程序中,但我希望以编程方式找到它以支持过去、现在 (win7) 和未来的 Windows 版本。

So, how would I do this?

那么,我该怎么做呢?

采纳答案by Hans Passant

You get it from GetMonitorInfo(), MONITORINFOEX.rcWork member.

你可以从GetMonitorInfo()MONITORINFOEX.rcWork 成员那里得到它。

Get the HMONITOR that you need to call this function from, say, MonitorFromRect(), passing your window rectangle. Or MonitorFromPoint() or EnumDisplayMonitors(), depends where you want to display your window. (0,0) is always the upper left corner of the primary monitor.

获取调用此函数所需的 HMONITOR,例如,MonitorFromRect(),传递您的窗口矩形。或 MonitorFromPoint() 或 EnumDisplayMonitors(),取决于您想在何处显示窗口。(0,0) 始终是主监视器的左上角。

回答by Mateen Ulhaq

By searching Google for "height of taskbar c++", I got the following result:

通过在 Google 上搜索"height of taskbar c++",我得到了以下结果:

Here's how to get the height of the Windows task bar using the windows functions FindWindowand GetWindowRect.

int MyClass::getTaskBarHeight()
{
    RECT rect;
    HWND taskBar = FindWindow(L"Shell_traywnd", NULL);
    if(taskBar && GetWindowRect(taskBar, &rect)) {
        return rect.bottom - rect.top;
    }
}

Getting the width (should the task bar be on the left or right of the screen) can be done using:

rect-right - rect.left

以下是如何使用 windows 函数FindWindowGetWindowRect.

int MyClass::getTaskBarHeight()
{
    RECT rect;
    HWND taskBar = FindWindow(L"Shell_traywnd", NULL);
    if(taskBar && GetWindowRect(taskBar, &rect)) {
        return rect.bottom - rect.top;
    }
}

可以使用以下方法获取宽度(任务栏应该在屏幕的左侧还是右侧):

rect-right - rect.left

You may want to check if the width is greater than the height. If the width is greater, this means the bar is at the top or bottom. Otherwise, it is on the left/right side of the screen.

您可能需要检查宽度是否大于高度。如果宽度更大,这意味着条在顶部或底部。否则,它位于屏幕的左侧/右侧。

回答by Franci Penov

Ask Windows about it using the ABM_GETTASKBARmessage and specifying the hwnd for the taskbar.

使用ABM_GETTASKBAR消息并指定任务栏的 hwnd询问 Windows 。

回答by Alexander Kosenkov

Probably, you want not only Taksbar, but all other 'bars' on the screen?

可能,您不仅想要 Taksbar,还想要屏幕上的所有其他“栏”?

All you actually need is SystemParametersInfo(SPI_GETWORKAREA)

你真正需要的是 SystemParametersInfo(SPI_GETWORKAREA)

SystemParametersInfo, passing SPI_GETWORKAREA as a parameter

SystemParametersInfo,将 SPI_GETWORKAREA 作为参数传递

Retrieves the size of the work area on the primary display monitor. The work area is the portion of the screen not obscured by the system taskbar or by application desktop toolbars. The pvParam parameter must point to a RECT structure that receives the coordinates of the work area, expressed in virtual screen coordinates.

检索主显示监视器上工作区的大小。工作区是屏幕上未被系统任务栏或应用程序桌面工具栏遮挡的部分。pvParam 参数必须指向接收工作区坐标的 RECT 结构,以虚拟屏幕坐标表示。

回答by Built on Sin

There are numerous methods depending on your needs. I used EnumDisplayMonitors() as I needed to test every display to see if it had a taskbar. A method of doing this is:

根据您的需要,有多种方法。我使用了 EnumDisplayMonitors(),因为我需要测试每个显示器以查看它是否有任务栏。这样做的方法是:

Use EnumDisplayMonitors()to get a list of all the monitors.

使用EnumDisplayMonitors()获取所有监视器的列表。

MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)

Inside the callback will give you a handle to a display. Warning this function will enumerate virtual displays as well:Using the handle to the display, use GetMonitorInfo()with the handle to the display.

在回调内部会给你一个显示的句柄。 警告此函数也将枚举虚拟显示:使用显示句柄,将GetMonitorInfo()与显示句柄一起使用。

This will return the name of the display along with two RECT structures one of the display position and resolution, the other RECT will be the work-area. You will need to do two checks (One for the X, one for the Y) to see if there is a taskbar on the monitor and the height or width of the taskbar.

这将返回显示名称以及两个 RECT 结构,一个是显示位置和分辨率,另一个是工作区。您需要进行两项检查(一项用于 X,一项用于 Y)以查看显示器上是否有任务栏以及任务栏的高度或宽度。

For example first we check the Y axis:

例如首先我们检查 Y 轴:

if(monitor->rcMonitor.top == monitor->rcWork.top &&
monitor->rcMonitor.bottom == monitor->rcWork.bottom)
{
std::cout << "There is no taskbar on the Y axis" << std::endl;
}
else
{
std::cout << "There is a taskbar on the Y axis" << std::endl;
int height = monitor->rcMonitor.bottom - monitor->rcMonitor.top;
int hieghtOfTaskbar = height - (monitor.rcWork.bottom - monitor.rcWork.top);
std::cout << "The height of the taskbar is: " << heightOfTaskbar << std::endl;
}

Then we check the X axis:

然后我们检查X轴:

if(monitor->rcMonitor.right == monitor->rcWork.right &&
monitor->rcMonitor.left == monitor->rcWork.left )
{
std::cout << "There is no taskbar on the X axis" << std::endl;
}
else
{
std::cout << "There is a taskbar on the X axis" << std::endl;
int width = monitor->rcMonitor.left  - monitor->rcMonitor.right;
int widthOfTaskbar = height - (monitor.rcWork.left - monitor.rcWork.right);
std::cout << "The width of the taskbar is: " << heightOfTaskbar << std::endl;
}

The height or width, depending on position, of the taskbar will usually be the height or the width of the monitor respectively, though this may not always be the case.

任务栏的高度或宽度(取决于位置)通常分别是显示器的高度或宽度,尽管情况并非总是如此。