65.9K
CodeProject is changing. Read more.
Home

Bench a Dialog on the Taskbar

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.53/5 (9 votes)

Dec 18, 2014

CPOL
viewsIcon

23353

downloadIcon

320

A project to place a Dialog Box on the Taskbar

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.

// This is in OnInitDialog()

	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;	// height desktop minus height of dialog
	int nTbarHeight = GetTaskBarHeight();
	int nLogPixel = GetLogPixels();
	dbPercent = nLogPixel / 96.0;	// 96 is 100%
	dbTbarHeight = (double)nTbarHeight;
	dbTbarHeight *= dbPercent;
	nTbarHeight = (int)dbTbarHeight;
	y -= nTbarHeight;

	pMainWnd->SetWindowPos(NULL, x, y, 0, 0, SWP_NOSIZE);

	return TRUE;  // return TRUE  unless you set the focus to a control 


} 

These are the 2 function I added

// This function gets the height of the taskbar
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;
}

// This function gets the logpixel value from the registry
DWORD CBenchedDlg::GetLogPixels()
{
    DWORD dwLogPixel = 0;
    DWORD dwType = REG_DWORD;
    DWORD dwLength = sizeof(DWORD);
    LONG lReg;
    LONG lRet;
    HKEY hRegKey = NULL;
    // Read the registry key which says about desktop pixel size
    // In HKEY_CURRENT_USER\\Control Panel\\Desktop"
    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