Sub-window (control) to the implementation of classification (C ++ implementation)

xiaoxiao2021-03-06  19

// Header file: //// file name: AlignAbility.h // Function Description: Make your window to align the ability. // Usage: 1. Define a aligned object in your parent window class (of course, outside the class). Such as: calignability m_align; // 2, set the handle of the parent window class for alignment class objects. Such as: m_align.setparenthwnd (GetSafehWnd ()); (For a dialog-based program, call in the OnInitDialog function.) // 3, call the AddalignItem function of the alignment class object, incoming a sub-window handle or ID and alignment Alignment can be used or operators multiple selection). / / If the handle of the parent window handle and the corresponding sub-window in the alignment class object, or the sub-window handle is not a child window of the parent window, the function call will fail. // such as: Rect EDS = {0, 0, 0, 0}; m_align.addalignItem (IDC_EDit_content, leftalign | Topalign | RightAlign | BOTTOMALIGN, & EDS); // (For a dialog-based program, call in the OnInitDialog function. In most cases, you can ignore the last parameters of the AddalignItem function, // This parameter is used to display the distance (in pixels) of the four sides of the corresponding sub-control to the parent window (in pixels), which is for non-installations The alignment of the sub-control placed is very useful .//4, when your window is aligned, call the Align function. Such as: m_align.align (); (usually calls in the WM_SIZE message processing function of the parent window).

// Note: In order to make it available for other C system programming environments, I avoid the use of the MFC class, which makes the code a bit blessing. If you want to use the C // Language SDK program, you will use the vector group instead .. .// 2005-2-28 .// If you have better improvements, remember to inform me: tangxinfa@sina.com # define alignability_h_ # include "windows.h" #include Using std :: vector; enum alignype {leftalign = 1, // Left alignment. TopAlign = 2, // Align. RightAlign = 4, // right alignment. Bottomalign = 8 //. }; class calignability {public: BOOL setParenthwnd (hwnd hwndparent); hwnd getparenthWnd () const; void align (); // is aligned. struct AlignCfg {public: AlignCfg (HWND hwndChild, int eType, RECT oriEDs): m_hwndChild (hwndChild), m_oriEDs (oriEDs), m_eType (eType) {} HWND m_hwndChild; // child window handle int m_eType; // alignment. . RECT M_ORIEDS; // Sub-control initially (usually designed) to the sides of the four sides of the parent window.}; Calignability (hwnd hwndparent = 0); Virtual ~ calignability (); Bool AddalignItem (int NSubwndID, int Aligntype, Rect * LPEDs = 0); Bool AddalignItem (HWND HWNDCHILD, INT Aligntype, Rect * LPEDs = 0); private: hwnd m_hwndparent; vector m_alignset;}; # endif // alignability_h_

// Implement file: //

File Name: Alignability.cpp # incrude "alignability.h" #include

#ifdef _debug # undef this_filestatic char this_file [] = __ file __; # Define new debug_new # Endif

//// construction / destruction //

Calignability :: Calignability (hwnd hwndparent / * = 0 * /): m_hwndparent (hwndparent) {assert (:: iswindow (m_hwndparent));}

Calignability :: ~ calignability () {

} BOOL CAlignability :: AddAlignItem (int nSubWndId, int aligntype, RECT * lpEDs / * = 0 * /) {HWND hwndChild (GetDlgItem (m_hwndParent, nSubWndId)); return AddAlignItem (hwndChild, aligntype, lpEDs);} BOOL CAlignability :: AddAlignItem (HWND hwndChild, int aligntype, RECT * lpEDs / * = 0 * /) {if {assert (0) (:: IsWindow (m_hwndParent)!);. // Parent window is Invalid return FALSE;} RECT subWndRect, parentWndRect ; :: GetClientRect (m_hwndParent, & parentWndRect); if (:: IsWindow (hwndChild)) {RECT oriEDs; if (lpEDs) oriEDs = * lpEDs; else {:: GetWindowRect (hwndChild, & subWndRect); POINT pt; pt.x = SubwndRect.Left; pt.y = subwndRect.top; :: screenToClient (m_hwndparent, & pt); subwndRect.top = pt.x, subwndRect.top = pt.y; pt.x = subwndRect.right; pt.y = subwndRect .bottom; :: ScreenToClient (m_hwndParent, & pt); subWndRect.right = pt.x, subWndRect.bottom = pt.y; oriEDs.left = subWndRect.left - parentWndRect.left; oriEDs.right = parentWndRect.right - subWndRect. Right; orieds.top = Subwn dRect.top - parentWndRect.top; oriEDs.bottom = parentWndRect.bottom - subWndRect.bottom;} m_alignset.push_back (AlignCfg (hwndChild, aligntype, oriEDs)); return true;} return FALSE;} void CAlignability :: Align () {If (! :: iswindow) {assert (0); // can't Aalign: Parent Window is invalid. Return;} Rect; :: getClientRect (m_hwndparent, & respons); const int cxparent = Rect. Right - Rect.Top; hWnd hwndchild = 0; hwnd hwndchild = 0; for (long i = 0, n = m_alignset.size (); i

:: Ischild (m_hwndparent, hwndchild) {:: getWindowRect (hwndchild, & review); point pt; pt.x = Rect.Left; pt.y = Rect.top; :: screenToclient (m_hwndparent, & pt); Rect.Left = pt.x, Rect.top = pt.y; pt.x = Rect.right; pt.y = Rect.bottom; :: screenToClient (m_hwndparent, & pt); Rect.Right = pt.x, Rect.bottom = Pt.y; const.top; if (m_alignset [i] .m_etype & leftalign) {rect.left = m_alignset [i] .m_orieds left; if (! (M_AlignSet [i] .m_type & rightalign)) Rect.right = Rect.Left oriWidth; // Hold the width unchanged.} IF (m_alignset [i] .m_type & topalign) {Rect.top = m_alignset [i] .m_orieds.top; if (! (m_alignset)) Rect.bottom = Rect.top oriHeight; // Keep height unchanged.} IF (m_alignset [i] .m_etype & rightAlign) {Rect.right = cx at - m_alignset [i] .m_orieds.right; if (! (m_alignset [i] .m_type & leftalign)) Rect.Left = Rect .right - OriWidth; // Hold the width. } IF (m_alignse T [i] .m_etype & bottomalign) {rect.bottom = cyparent - m_alignset [i] .m_orieds.bottom; if (! (m_alignset [i] .m_etype & topalign)) Rect.top = Rect.bottom - OriHeight; / / Keep height unchanged.} :: movewindow (hWndchild, Rect.Lect.top, Rect.right - Rect.Top, Rect.bottom - Rect.top, True);} else {assert (0); // Setalign to a item failed.}}}

HWND CalignAbility :: getparenwnd () const {return m_hwndparent;}

Bool Calignability :: setParenThwnd (HWND HWNDPARENT) {if (: iswindow (hwndparent)) {m_hwndparent = hwndparent; return true;} Return False;

转载请注明原文地址:https://www.9cbs.com/read-52821.html

New Post(0)