|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionRecently I've been reading up on WTL, and I came across a rather interesting class that I hadn't seen mentioned
anywhere, Using CDialogResizeThe basicsAs with many other WTL features, you begin by adding class CMainDlg : public CAxDialogImpl<CMainDlg>, public CDialogResize<CMainDlg>
The next step is to initialize the LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Init the CDialogResize code
DlgResize_Init();
...
}
Next, add an entry in the dialog's message map that passes sizing messages to BEGIN_MSG_MAP(CMainDlg)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
...
CHAIN_MSG_MAP(CDialogResize<CMainDlg>)
END_MSG_MAP()
Finally, add a new map to the dialog class that lists which controls in the dialog will be resized: class CMainDlg : public CAxDialogImpl<CMainDlg>, public CDialogResize<CMainDlg> { ... public: BEGIN_DLGRESIZE_MAP(CMainDlg) END_DLGRESIZE_MAP() ... }; I'll describe how to fill in this map in the "Setting up the resize map" section. Initializing CDialogResize
void CDialogResize::DlgResize_Init ( bool bAddGripper = true, bool bUseMinTrackSize = true, DWORD dwForceStyle = WS_THICKFRAME | WS_CLIPCHILDREN ); The parameters are:
Setting up the resize mapThe dialog resize map tells DLGRESIZE_CONTROL(ControlID, Flags)
Note that you cannot move and size a control in the same dimension. If you specify, say You can also group controls together so that they move and size proportionally to each other. I will cover this subject later. Sample ProjectThe sample project included with this article is a simple dialog-based app that functions as a browser (using the WebBrowser ActiveX control). The control IDs are listed below; you should refer back to this diagram later when I discuss moving and resizing these controls.
The controls will move and size according to these rules:
The browser's LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/) { // Init the CDialogResize code DlgResize_Init(); ... } This uses the default parameters to
Here is the resize map, which lists the moving and sizing behavior for the controls. Note the new macros -
BEGIN_DLGRESIZE_MAP(CMainDlg)
// Location edit box
DLGRESIZE_CONTROL(IDC_URL, DLSZ_SIZE_X)
// Go, Exit, About buttons
DLGRESIZE_CONTROL(IDC_GO, DLSZ_MOVE_X)
DLGRESIZE_CONTROL(IDC_EXIT, DLSZ_MOVE_X)
DLGRESIZE_CONTROL(ID_APP_ABOUT, DLSZ_MOVE_X)
// IE control buttons
BEGIN_DLGRESIZE_GROUP()
DLGRESIZE_CONTROL(IDC_BACK, DLSZ_SIZE_X)
DLGRESIZE_CONTROL(IDC_FORWARD, DLSZ_SIZE_X)
DLGRESIZE_CONTROL(IDC_STOP, DLSZ_SIZE_X)
DLGRESIZE_CONTROL(IDC_REFRESH, DLSZ_SIZE_X)
END_DLGRESIZE_GROUP()
// WebBrowser control
DLGRESIZE_CONTROL(IDC_BROWSER, DLSZ_SIZE_X|DLSZ_SIZE_Y)
END_DLGRESIZE_MAP()
Here is the dialog after being resized:
Notice how the edit box is wider, and the browser control is wider and taller. The behavior of the four grouped
buttons is a bit tough to put into words, and the WTL code offers little guidance since there are few comments.
But the basic idea is: imagine a bounding rectangle that surrounds all four buttons. That rectangle resizes like
any other control, and all the buttons are sized proportionally so they remain within the bounding rectangle. If
the buttons were to be moved, instead of resized, they would be positioned to always be evenly spaced apart. Note
that all the controls in a group should have the same Bugs and Problems with CDialogResizeSo far, I've only seen two problems. One is that there seems to be an off-by-one bug somewhere, because the
first time you resize the dialog, some of the controls shift the wrong direction by one pixel. The other, more
serious problem, is a bug in the WTL AppWizard that is exposed when you add int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR lpstrCmdLine, int nCmdShow) { // ... CMainDlg dlgMain; int nRet = dlgMain.DoModal(); _Module.Term(); ::CoUninitialize(); return nRet; } The trouble with that is the int nRet;
{
CMainDlg dlgMain;
nRet = dlgMain.DoModal();
}
|
||||||||||||||||||||||