Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

location of modal dialog boxes

Status
Not open for further replies.

foxnet

Programmer
Mar 24, 2005
21
US
Is there a way to place a modal dialog box of the screen by giving an x, y offset. I use the command

DialogBox(g_hInst, MAKEINTRESOURCE(IDD_OPTIONS), hwnd, (DLGPROC)

to place it on the screen, but is always goes to the top left corner.

 
It does a little bit more than you need, but this function handles WM_INITDIALOG to, among other things, locate it on a specific position on the screen:

Code:
LRESULT CDialogbox::InitDialog(HWND hwndDlg, HWND hwndControl, LPARAM lParam)
//Handles the WM_INITDIALOG message.
{
	LOCK(&m_csVars);

	HWND hwndOwner=NULL;
	HICON hIcon=NULL;
	RECT rcOwner={0}, rcDlg={0};
	VARIANT_BOOL bResult=VARIANT_TRUE;

	m_hWnd = hwndDlg;			//This one we must save for future messages.
	m_sWndMap[hwndDlg]=this;	//Store in the window map, so we can find the object in future messages as well.

	//Set title:
	if(m_lcTitle.Length()>0) 
		SetWindowText(m_hWnd, m_lcTitle);

	else {
		//If a title id was not specified, load the title text into the member variable:
		m_lcTitle.Reserve(GetWindowTextLength(m_hWnd)+1);
		
		GetWindowText(m_hWnd, m_lcTitle.GetBuffer(), m_lcTitle.BufferSize());
	}

	//Set small icon:
	if(m_nSmallIcon>0 && (hIcon=m_pRes->LoadResIcon(m_nSmallIcon, lsdIcon_Small))!=NULL) SendMessage(m_hWnd, WM_SETICON, static_cast<WPARAM>(ICON_SMALL), reinterpret_cast<LPARAM>(hIcon));

	//Set large icon:
	if(m_nLargeIcon>0 && (hIcon=m_pRes->LoadResIcon(m_nLargeIcon, lsdIcon_Big))!=NULL) SendMessage(m_hWnd, WM_SETICON, static_cast<WPARAM>(ICON_BIG), reinterpret_cast<LPARAM>(hIcon));

	//Add extra windowstyles:
	if(m_nStyles>0) SetWindowLong(m_hWnd, GWL_STYLE, GetWindowLong(m_hWnd, GWL_STYLE)|m_nStyles);
	if(m_nExtraStyles>0) SetWindowLong(m_hWnd, GWL_EXSTYLE, GetWindowLong(m_hWnd, GWL_EXSTYLE)|m_nExtraStyles);

	//Fire the event:
	Fire_Initialize(m_enmPosition, m_nXPos, m_nYPos, &bResult);

	//If necessary, position the dialogbox:
	switch(m_enmPosition) {
		case lsdResDlg_CenterOwner:
			if((hwndOwner=GetParent(m_hWnd))==NULL) hwndOwner=GetDesktopWindow();

		case lsdResDlg_CenterScreen:
			if(hwndOwner==NULL) hwndOwner=GetDesktopWindow();
			GetWindowRect(hwndOwner, &rcOwner);
			GetWindowRect(m_hWnd, &rcDlg);

			SetWindowPos(m_hWnd, HWND_TOP, rcOwner.left+(((rcOwner.right-rcOwner.left)-(rcDlg.right-rcDlg.left))/2), rcOwner.top+(((rcOwner.bottom-rcOwner.top)-(rcDlg.bottom-rcDlg.top))/2), 0, 0, SWP_NOSIZE);
			break;

		case lsdResDlg_Specific:
			SetWindowPos(m_hWnd, HWND_TOP, m_nXPos, m_nYPos, 0, 0, SWP_NOSIZE);
			break;
	}

	//And to be absolutely sure it will ALWAYS be entirely visible:
	SendMessage(m_hWnd, DM_REPOSITION, NULL, NULL);

	UNLOCK(&m_csVars);

	return bResult==VARIANT_TRUE;
}

Greetings,
Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top