开发者

How would I find the height of the task bar?

In my windows application, I am trying to find the 开发者_如何学Goheight 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?


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.


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜