Introduction
This article shows how to keep a diaglog box on the taskbar.
Background
In windows 7 you can change the display to 100, 125, or 150 percent which changes the height of the taskbar. This demo shows how to keep the dialog box sitting on the taskbar.
Using the code
You must first get the height of the taskbar, then the current logpixel size to see what the current display percentage is set to. The program now handles all settings of percent of display including custom DPI settings.
CWnd *pMainWnd;
CWnd *pWnd;
CRect rc;
CRect rect;
pMainWnd = AfxGetMainWnd();
pMainWnd->GetWindowRect(&rc);
pWnd = GetDesktopWindow();
pWnd->GetWindowRect(&rect);
double dbPercent, dbTbarHeight;
int x = ((rect.right / 2) - (rc.right / 2));
int y = rect.bottom - rc.bottom; int nTbarHeight = GetTaskBarHeight();
int nLogPixel = GetLogPixels();
dbPercent = nLogPixel / 96.0; dbTbarHeight = (double)nTbarHeight;
dbTbarHeight *= dbPercent;
nTbarHeight = (int)dbTbarHeight;
y -= nTbarHeight;
pMainWnd->SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE);
return TRUE;
}
These are the 2 function I added
int CBenchedDlg::GetTaskBarHeight()
{
RECT rect;
CWnd* pTaskBar = NULL;
pTaskBar = FindWindow("Shell_traywnd", NULL);
if(pTaskBar)
{
pTaskBar->GetWindowRect(&rect);
return rect.bottom - rect.top;
}
return 0;
}
DWORD CBenchedDlg::GetLogPixels()
{
DWORD dwLogPixel = 0;
DWORD dwType = REG_DWORD;
DWORD dwLength = sizeof(DWORD);
LONG lReg;
LONG lRet;
HKEY hRegKey = NULL;
lReg = RegOpenKeyEx(HKEY_CURRENT_USER,
"Control Panel\\Desktop",
0,KEY_ALL_ACCESS,
&hRegKey);
if(lReg == ERROR_SUCCESS)
{
lRet = RegQueryValueEx(hRegKey,"LogPixels",NULL,&dwType,(BYTE*)&dwLogPixel,&dwLength);
RegCloseKey(hRegKey);
if((lRet == ERROR_SUCCESS) || (lRet == ERROR_TIMEOUT) || (lRet == ERROR_MORE_DATA))
{
lReg = RegOpenKeyEx(HKEY_CURRENT_USER,
"Control Panel\\Desktop",
0,KEY_ALL_ACCESS,
&hRegKey);
lRet = RegGetValue(hRegKey,NULL,"LogPixels",RRF_RT_REG_SZ,&dwType,(BYTE*)&dwLogPixel,&dwLength);
RegCloseKey(hRegKey);
}
}
return dwLogPixel;
}
Points of Interest
I initally wrote this code for a game score program a friend wanted just for something to do.
History
2014-12-18 Version 1
2014-12-25 Added custom DPI