子窗口(控件)對齊類的實現(C++實現)

//頭文件://////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//文件名稱:Alignability.h
//功能說明: 使你的窗口具有子窗口對齊能力。
//用法說明:1,在你的父窗口類中定義一個對齊類對象(當然也可放在類外).如: CAlignability m_align;
//   2,爲對齊類對象設置父窗口類的句柄。如: m_align.SetParentHwnd(GetSafeHwnd()); (對於基於對話框的程序,在OnInitDialog函數中調用。)
//   3,調用對齊類對象的AddAlignItem函數,傳入一個子窗口句柄或ID及對齊方式(對齊方式可用按位或運算符多選)。
//    如果對齊類對象中父窗口句柄及相應子窗口的句柄無效,或子窗口句柄並非父窗口的子窗口,函數調用將失敗。
//    如:RECT eds = {0, 0, 0, 0}; m_align.AddAlignItem(IDC_EDIT_CONTENT, leftalign|topalign|rightalign|bottomalign, &eds);
//    (對於基於對話框的程序,在OnInitDialog函數中調用。),在大部分情況下,你可以忽略AddAlignItem函數的最後一個參數,    
//    這個參數是用來顯示設置相應子控件的四邊到父窗口四邊的距離(以像素爲單位),這對於非設計期放置的子控件的對齊設置很有用.
//   4,當你的窗口的子控件需要對齊的時候,調用Align函數。如:m_align.Align(); (通常在父窗口的WM_SIZE消息處理函數中調用)。

//備註:爲了使它能用於其它C系統編程環境,我避免了對MFC類的使用,這讓代碼有點發福,如果要用於C
//    語言SDK編程就要將vector用數組代替...
//                            2005-2-28. writer by txf.
//                            如果你有更好的改進,記得通知我:[email protected]   
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef ALIGNABILITY_H_
#define ALIGNABILITY_H_
#include "windows.h"
#include <vector>   
using std::vector;
enum AlignType{
 leftalign = 1,   //左對齊。
 topalign = 2,   //上對齊。
 rightalign = 4,   //右對齊。
 bottomalign = 8    //下對齊。
};
class CAlignability
{
public:
 BOOL SetParentHwnd(HWND hwndParent);
 HWND GetParentHwnd() const;
 void Align(); //進行對齊。
 struct AlignCfg{
 public:
  AlignCfg(HWND hwndChild, int eType, RECT oriEDs)
    :m_hwndChild(hwndChild), m_oriEDs(oriEDs), m_eType(eType){}
  HWND  m_hwndChild;//子窗口的句柄.
  int   m_eType; //對齊方式.
  RECT  m_oriEDs; //子控件初始時(通常爲設計時)到父窗口的四邊的邊距.
 };
 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<AlignCfg> m_alignset;
};

#endif //ALIGNABILITY_H_

//實現文件://////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

文件名稱:Alignability.cpp
#include "Alignability.h"
#include <assert.h>

#ifdef _DEBUG
#undef THIS_FILE
static 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(! ::IsWindow(m_hwndParent))
 {
  assert(0);//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.left = 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 = subWndRect.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(m_hwndParent))
 {
  assert(0);//Can't AAlign: Parent window is Invalid.
  return;
 }
 RECT rect;
 ::GetClientRect(m_hwndParent, &rect);
 const int cxParent = rect.right - rect.left;
 const int cyParent = rect.bottom - rect.top;
 HWND  hwndChild = 0;
 for(long i=0, n=m_alignset.size(); i < n; ++i)
 { 
  hwndChild = m_alignset[i].m_hwndChild;
  if(::IsWindow(hwndChild) && ::IsChild(m_hwndParent, hwndChild))
  {
   ::GetWindowRect(hwndChild, &rect);
   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 int oriWidth = rect.right - rect.left;
   const int oriHeight= rect.bottom - rect.top;
   if(m_alignset[i].m_eType & leftalign)
   {
    rect.left = m_alignset[i].m_oriEDs.left;
    if(!(m_alignset[i].m_eType & rightalign))
     rect.right = rect.left + oriWidth; //保持寬度不變.
   }
   if(m_alignset[i].m_eType & topalign)
   {
    rect.top = m_alignset[i].m_oriEDs.top;
    if(!(m_alignset[i].m_eType & bottomalign))
     rect.bottom = rect.top + oriHeight; //保持高度不變.
   }
   if(m_alignset[i].m_eType & rightalign)
   {
    rect.right = cxParent - m_alignset[i].m_oriEDs.right;
    if(!(m_alignset[i].m_eType & leftalign))
     rect.left = rect.right - oriWidth; //保持寬度不變.
   }
   if(m_alignset[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; //保持高度不變.
   }
   ::MoveWindow(hwndChild, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, TRUE); 
  }
  else
  {
   assert(0); //SetAlign to a item failed.
  }
 } 
}

 

HWND CAlignability::GetParentHwnd() const
{
 return m_hwndParent;
}

BOOL CAlignability::SetParentHwnd(HWND hwndParent)
{
 if(::IsWindow(hwndParent))
 {
  m_hwndParent = hwndParent;
  return TRUE;
 }
 return FALSE;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章