头文件

#if !defined(AFX_MOVECONTROL_H__3282B624_668E_4787_A17B_67EB32900090__INCLUDED_)
#define AFX_MOVECONTROL_H__3282B624_668E_4787_A17B_67EB32900090__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MoveControl.h : header file
//

/////////////////////////////////////////////////////////////////////////////
// MoveControl window
#define MOVEMAXCTRL 500 
class MoveControl : public CWnd
{
// Construction
public:
 MoveControl();

// Attributes
public:
 CWnd  *pwnd;
 CRect WindowRect;
 CRect ChildRect[MOVEMAXCTRL];
// Operations
public:
// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(MoveControl)
 //}}AFX_VIRTUAL

// Implementation
public:
    BOOL MoveRect();
 void ControlInit(CWnd *wnd);
 virtual ~MoveControl();

 // Generated message map functions
protected:
 //{{AFX_MSG(MoveControl)
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MOVECONTROL_H__3282B624_668E_4787_A17B_67EB32900090__INCLUDED_)

类文件

// MoveControl.cpp : implementation file
//

#include "stdafx.h"
#include "MoveControl.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// MoveControl

MoveControl::MoveControl()
{
 pwnd=NULL;
}

MoveControl::~MoveControl()
{
}


BEGIN_MESSAGE_MAP(MoveControl, CWnd)
 //{{AFX_MSG_MAP(MoveControl)
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// MoveControl message handlers

void MoveControl::ControlInit(CWnd *wnd)
{
 try
 {
  if(wnd!=NULL)
  {
     pwnd=wnd;
     int i=0;
     CWnd *Lwnd=wnd->GetWindow(GW_CHILD);
     while (Lwnd)
     {  //循环将所有的子窗口重新调整其位置
     Lwnd->GetWindowRect(ChildRect[i]);//取得句柄所对应子窗口的矩形坐标(屏幕坐标)存入rcChild
     pwnd->ScreenToClient(ChildRect[i]);
     i++;
     if(i>=MOVEMAXCTRL)
      break;
     Lwnd = Lwnd->GetNextWindow();//取得下一个子窗口的指针
     }
     pwnd->GetClientRect(WindowRect);
     pwnd->ScreenToClient(WindowRect);
  }
 }
 catch (...)
 {
 }
}

BOOL MoveControl::MoveRect()
{
 try
 {
  if ( pwnd )
  {
     CRect rcClientStart;
     pwnd->GetClientRect(rcClientStart);
     pwnd->ScreenToClient(rcClientStart);
     CRect  rcChild;
     CWnd* wndChild = pwnd->GetWindow(GW_CHILD);
     int i=0;
     while (wndChild)
     {
     rcChild=ChildRect[i];
     float zleft=float(ChildRect[i].left)/float(WindowRect.Width());
     float zright=float(ChildRect[i].right)/float(WindowRect.Width());
     float zbottom=float(ChildRect[i].bottom)/float(WindowRect.Height());
     float ztop=float(ChildRect[i].top)/float(WindowRect.Height());
     rcChild.left=int(rcClientStart.Width()*zleft);
     rcChild.right=int(rcClientStart.Width()*zright);
     rcChild.bottom=int(rcClientStart.Height()*zbottom);
     rcChild.top=int(rcClientStart.Height()*ztop);
     wndChild->MoveWindow(rcChild, FALSE);
     //wndChild->Invalidate(false);
     i++;
     wndChild = wndChild->GetNextWindow();
     }
     pwnd->Invalidate(TRUE);
     return TRUE;
  }
 }
 catch (...)
 {
  return FALSE;
 }
 return FALSE;
}

使用方法:

1、在窗体类文件中先定义对象

2、在窗体初始 化对话框中调用对象人ControlInit(),参数为this指针

3、在窗体的OnSize中加入下面的代码

if ( this )
 {
  if ( this->GetSafeHwnd() )
  {
            mv.MoveRect();
  }
 } 


注意:本文归作者所有,未经作者允许,不得转载