This article is a spanzhang original, its blog address is: http://blog.9cbs.net/spanzhang. Please indicate the source, thank you! ! When writing a Windows program with C #, we can notice that there is a very interesting and useful Dongdong "Anchor". It can be designated as a combination of "TOP, BOTTOM, LEFT, RIGHT". This thing can simplify a lot of work on our interface, and you can write code for layout. The following is an analog solution I have given in VC6: #pragma Warning (Disable: 4786) #include
Void Basedlg :: Registeranchorbyid (uint itemid, uint anchor / * = ar_none * /) {anchorsbyid [itemid] = anchor;
void baseDlg :: unRegisterAnchorById (UINT itemId) {anchorMapById :: iterator iter = anchorsById.find (itemId); if (! iter = anchorsById.end ()) anchorsById.erase (iter);} void baseDlg :: OnSize (UINT nType , int CX, int CY) {cdialog :: Onsize (NTYPE, CX, CY);
If (! iswindowvisible ()) return; crect RTClientnew; getClientRect (& RTCLIENTNEW); int dx = rtclientnew.right - OldClient.right; int Dy = rtclientnew.bottom - OldClient.bottom;
IF (RTClientnew.width () <2 || RTClientnew.height () <2) Return;
For (Anchormap :: item.begin (); it! = Anchors.end (); iter) {cwnd * child = i = = = = i (== null ||! iswindow (child- > m_hwnd)) Continue; uint anchor = t = >-; child-> getwindowRect (& RTORIGN); ScreenToClient (& RTORIN);
Static CRECT RTNEW; RTNEW = RTORIGN; if (Anchor & Ar_left) {r rw.left = r r..f;} else {r rW.LEFT = DX / 2;
IF (Anchor & Ar_top) {RTNEW.TOP = RTORIGN.TOP;} else {r rW.top = DY / 2;
IF (Anchor & Ar_right) {r; (! (! (! (! (anchor & ar_left)) RTNEW.LEFT = rRIGN.LEFT DX;} else {r rww (}) = DX / 2; if (Anchor & Ar_left Rtnew.right = RTNew.LEFT RTORIGN.WIDTH ();
IF (Anchor & Ar_bottom) {RTNEW.BOTTOM = (DY); if (! (Anchor & Ar_top)) RTNew.top = r riin;}} else {r}} Else {RTNEW.BOTTOM = DY / 2; IF (Anchor & Ar_top) rtnew.bottom = rtnew.top rtORIGN.HEIGHT ();
Child-> MoveWindow (& RTNEW);
for (anchorMapById :: iterator iterById = anchorsById.begin (); iterById = anchorsById.end ();! iterById) {CWnd * child = GetDlgItem (iterById-> first);! if (child == NULL || IsWindow (child-> m_hwnd))))
Uint Anchor = neyId-> second; static create rtRect; Child-> getWindowRect (& RTORIGN); ScreenToClient (& RTORIGN);
Static CRECT RTNEW; RTNEW = RTORIGN; if (Anchor & Ar_left) {r rw.left = r r..f;} else {r rW.LEFT = DX / 2;
IF (Anchor & Ar_top) {rTNew.top = RTORIGN.TOP;} else {RTNew.top = DY / 2;} if (anchor & ar_right) {r rww.. = DX; if (! (! (! (! (! & ar_left) Rtnew.LEFT = RTORIGN.LEFT DX;} else {RTNew.right = DX / 2; if (anchor & ar_left) rlew.right = rtnew.LTNEW.Right = RTNew.LEFT RTORIGN.WIDTH ();}
IF (Anchor & Ar_bottom) {RTNEW.BOTTOM = (DY); if (! (Anchor & Ar_top)) RTNew.top = r riin;}} else {r}} Else {RTNEW.BOTTOM = DY / 2; IF (Anchor & Ar_top) rtnew.bottom = rtnew.top rtORIGN.HEIGHT ();
Child-> MoveWindow (& RTNEW);
OldClient = RTClientNew;
INVALIDATE ();