Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / C++
Tip/Trick

Bench a Dialog on the Taskbar

Rate me:
Please Sign up or sign in to vote.
4.53/5 (9 votes)
26 Dec 2014CPOL 22.8K   320   8   6
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.

C++
// 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

C++
// 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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat will happen when taskbar being resized? Pin
VaKa24-Dec-14 4:29
VaKa24-Dec-14 4:29 
AnswerRe: What will happen when taskbar being resized? Pin
Roger6524-Dec-14 5:47
Roger6524-Dec-14 5:47 
GeneralRe: What will happen when taskbar being resized? Pin
VaKa24-Dec-14 23:03
VaKa24-Dec-14 23:03 
GeneralRe: What will happen when taskbar being resized? Pin
Roger6525-Dec-14 0:23
Roger6525-Dec-14 0:23 
GeneralInteresting Pin
ToothRobber22-Dec-14 5:55
professionalToothRobber22-Dec-14 5:55 
GeneralMy vote of 5 Pin
Matth Moestl18-Dec-14 21:21
professionalMatth Moestl18-Dec-14 21:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.