Tips for Dialog based applicationsAuthor : V.Girish Environment: Compiled using VC6.0 Sp3 and tested using Win95/98 WinNT4.0 and Win 2000 Here are some tips for developing dialog based applications :- In the Header file of the Dialog box,add a CBrush variable. For example, CBrush m_brush; 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. Creating a View inside a dialog :-This should not be done usually. But in rare cases, when we really need a view within a dialog, we can do this.We can use any view here, but for demonstration purposes, i am going to use a CScrollView.So, what we do is :- Derive a class from CScrollView. For example, u've named it CDlgScrollView In your dialog's header add a variable for that class. For example, CDlgScrollView *m_pScrollView; Then , in the OnInitDialog function of your dialog, add this code. CRuntimeClass *pClass = RUNTIME_CLASS(CDlgScrollView); m_pScrollView = (CDlgScrollView*)pClass->CreateObject(); // For the Create function's final parameter, i've used 10001. // This can be any value. Its just an id for your view. m_pScrollView->Create(NULL, NULL, WS_CHILD | WS_VISIBLE, CRect(0,0,200,200), this, 10001); m_pScrollView->ModifyStyleEx(0, WS_EX_CLIENTEDGE); Thats it. Compile it and u've got a Scroll View inside your dialog. |