www.xbdev.net
xbdev - software development
Tuesday April 23, 2024
home | contact | Support | Tutorials - Tips - Secrets - for C and C++.. It does all the hard work for us..
>>

Dialog Box Tips/Tricks

Here are some tips for developing dialog based applications :-

To fill the dialog box with a user specified colour :-
	In the Header file of the Dialog box,add a CBrush variable. For example, CBrush m_brush; 
In the OnInitDialog function of your dialog, add this code. m_brush.CreateSolidBrush(RGB(150,50,100)); Handle the WM_CTLCOLOR message in your dialog and in the OnCtlColor function, add this code. m_brush.CreateSolidBrush(RGB(150,50,100)); Or else, you can simply call SetDialogBkColor function in the InitInstance of the application if you want the dialog to have one standard colour.
To maximize a dialog by default :-
	Put this setence in the OnInitDialog function before the return statement.

	SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, NULL);
To create a modal dialog box :-
	CYourDialog *Dlg;
	Dlg = new CYourDialog;
	Dlg->DoModal();
To create a modeless dialog box :-
	CYourDialog *Dlg;
	Dlg = new CYourDialog;

	// IDD_MAIN_DIALOG is the ID of your dialog
	Dlg->Create(IDD_MAINDIALOG,this);
	Dlg->CenterWindow();
	Dlg->ShowWindow(SW_SHOWNORMAL);
To put a toolbar in your dialog :-
	First of all, create a toolbar in the resource.I have assumed that you have named it IDR_TOOLBAR1 

	Then,add a member variable in your dialog's header file for the toolbar. 

	CToolBar m_ctrlToolBar;

	Next,add this code in the OnInitDialog function of your dialog

	BOOL CYourDlg::OnInitDialog()
	{
	  CDialog::OnInitDialog();

	  ToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS 
			| CBRS_FLYBY | CBRS_BORDER_BOTTOM);

	  // Here IDR_TOOLBAR1 is the ID of the toolbar which you have created.
	  m_ctrlToolBar.LoadToolBar(IDR_TOOLBAR1);
  
	  CRect rcClientStart,rcClientNow;
	  GetClientRect(rcClientStart);

	  RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST,0, reposQuery, rcClientNow);


	  CPoint ptOffset(rcClientNow.left - rcClientStart.left,rcClientNow.top - rcClientStart.top);

	  CRect rcChild;

	  CWnd* pwndChild = GetWindow(GW_CHILD);

	  while (pwndChild)
	  {
	    pwndChild->GetWindowRect(rcChild);
	    ScreenToClient(rcChild);

	    rcChild.OffsetRect(ptOffset);

	    pwndChild->MoveWindow(rcChild, FALSE);
	    pwndChild = pwndChild->GetNextWindow();
	  }

	  CRect rcWindow;
	  GetWindowRect(rcWindow);

	  rcWindow.right += rcClientStart.Width() - rcClientNow.Width();
	  rcWindow.bottom += rcClientStart.Height() - rcClientNow.Height();
	  MoveWindow(rcWindow, FALSE);	

	  // And position the control bars
	  RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
  
  	  return TRUE;
	}
Using a CFindReplaceDialog :-

	1) In your header file, add a variable for the CFindReplaceDialog. 

	   CFindReplaceDialog *m_pFindReplaceDlg;

	2) When you want to call it, use this code.

	   m_pFindReplaceDlg = new CFindReplaceDialog();   
	   m_pFindReplaceDlg->Create(FALSE,"Hai","Hai Everybody",FR_DOWN,this);
To prevent resizing :-
	Assuming that you have given the user a flexibility to resize the dialog.
	But you dont want him/her to resize after a particular size. To do just that,
	handle the WM_SIZING message like this.

	void CYourDialog::OnSizing(UINT fwSide, LPRECT pRect) 
	{
		if(pRect->right - pRect->left <=200)
			pRect->right = pRect->left + 200;
	
		if(pRect->bottom - pRect->top <=200)
			pRect->bottom = pRect->top + 200;

		CDialog::OnSizing(fwSide, pRect);
	}
Moving a captionless dialog :-
	Sometimes, you would have the necessity of removing the Caption bar from the dialog.
	In that case, you wont be able to move your dialog as you cant click on the caption
	bar and drag it. In order to make your dialog move whenever you click on the dialog
	and move it without releasing the Left mouse button, handle WM_LBUTTONDOWN and the
	WM_NCHITTEST messages like this

	// Handler for WM_LBUTTONDOWN message

	void CYourDialog::OnLButtonDown(UINT nFlags, CPoint point)
	{
	    CDialog::OnLButtonDown(nFlags, point);
	    PostMessage( WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM( point.x, point.y));
	}

	// Handler for WM_NCHITTEST message

	LONG CYourDialog::OnNcHitTest( UINT uParam, LONG lParam )
	{  
	   int xPos = LOWORD(lParam);
	   int yPos = HIWORD(lParam);

	   UINT nHitTest = CDialog::OnNcHitTest(CSize(xPos, yPos));

	   return (nHitTest == HTCLIENT) ? HTCAPTION : nHitTest;
	}

	and now, you are able to move the dialog by clicking into the client area and dragging.
Creating a round dialog :-
	When i had started learning MFC , i used to think that this was something that only 
	great programmers could do. It was until i found out that it was so simple. Take a look
	at this.

	First, you need to add a member variable of type CRgn in your dialog's header file.

	CRgn m_rgn;

	Then , in the OnInitDialog function of your dialog, add this code.
	
	CRect rcDialog;

	// Get the client rectangle
	GetClientRect(rcDialog);

	// Create an elliptic region with the size equal to that of the dialog.
	// You can set the sizes to any value

	m_rgn.CreateEllipticRgn(0, 0, rcDialog.Width(), rcDialogHeight());

	// Set the window region of the dialog to the CRgn created.
	SetWindowRgn(GetSafeHwnd(), (HRGN) m_rgn, TRUE);

	Thats all. You've created a round rectangle now which can be used in casual looking applications.
 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.