简体   繁体   中英

How would I find the height of the task bar?

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.

So, how would I do this?

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

Here's how to get the height of the Windows task bar using the windows functions FindWindow and 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 

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.

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

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.

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

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

All you actually need is SystemParametersInfo(SPI_GETWORKAREA)

SystemParametersInfo , passing SPI_GETWORKAREA as a parameter

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.

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:

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

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.

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.

For example first we check the Y axis:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM