Introduction
Yesterday when I wrote a simple Win32 dialog application, I missed one handy feature that is implemented in .NET forms; anchors and docking for controls. I took a look at the CodeProject, but I did not find exactly what I was looking for, so I wrote a simple solution for myself. I put it here with hope that it will be useful for some other programmers. This solution has two advantages. First is that, you can use this simple class in MFC and non-MFC applications too. Second is that you can use add/remove for dynamically created controls during running application.
Using the code
First of all, if you want to use this class in your application, you must include this header file:
#include "anchor.h" CDlgAnchor dlgAnchor; CDlgMan dlgMan;
In WM_INITDIALOG
handler routine or OnInitDialog
(MFC applications), you must initialize this class:
static BOOL CALLBACK DialogFunc(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_INITDIALOG: // load pos/sizes of dialog window and all child windows dlgMan.Load(hwndDlg, "Software\\CodeProject\\AnchorExample"); dlgAnchor.Init(hwndDlg); ...
Now you can set anchors and/or docking for the controls you want from the list in the header file. You can use add/remove controls during the life of the dialog window as you will need, e.g. you can use Add
function for dynamically created controls.
dlgAnchor.Add(IDOK, ANCHOR_BOTTOM); dlgAnchor.Add(IDCANCEL, ANCHOR_BOTTOM); dlgAnchor.Add(IDC_LIST1, ANCHOR_TOPLEFT | ANCHOR_BOTTOM); dlgAnchor.Add(IDC_LIST2, ANCHOR_BOTTOMLEFT | ANCHOR_RIGHT); dlgAnchor.Add(IDC_EDIT1, ANCHOR_ALL); ...
Lastly you will need to add to the WM_SIZE
handler routine, code that will execute the OnSize
class routine, and add to the WM_DESTROY
handler routine, code that will save window position and size.
case WM_SIZE:
dlgAnchor.OnSize();
...
case WM_DESTROY:
dlgMan.Save();
...
That's all. I hope, you find this class useful.
History
- 19 September 2003
- Added
Update
andUpdateAll
functions that you may use if some other dialog handler (like Splitter) will change size of controls. - Added helper class
CDlgMan
(saving/restoring window position and position of all child windows).
- Added
- 11 September 2003
- First release.
License
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here
'C & C++ > MFC 컨트롤' 카테고리의 다른 글
[Dialog] 윈도우 그리기 lock 하기 (0) | 2011.12.14 |
---|---|
[Dialog] 캡션이나 아이콘을 깜박이게 하는 FlashWindow (0) | 2011.12.14 |
리스트(List) 색상 변경 - 리스트 (0) | 2011.12.13 |
[Static] Static 글자 색상, 크기 조절 (0) | 2011.11.24 |
다이얼로그 안으로 밖으로 (0) | 2011.11.21 |
댓글