MFC实现对话框自适应

BOOL Zoom_Window(HWND hWnd, CRect TaregetEX_Pos, LPRECT mOrgRect=NULL, BOOL bZoomEx_Only=FALSE)
{
  if(hWnd)
  {
    BOOL IsVisable = IsWindowVisible(hWnd);

    if(!bZoomEx_Only)
    {
      ::SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOZORDER|SWP_NOSIZE|SWP_HIDEWINDOW);

      CRect SrcWndRect;
      if(mOrgRect)
      {
        SrcWndRect = mOrgRect;
      }
      else
      {
        GetWindowRect(hWnd,&SrcWndRect);
      }

      DWORD style = GetWindowLong(hWnd, GWL_STYLE);

      INT_PTR Board_dx = 0, Board_dy = 0;

      if( WS_CAPTION == (style & WS_CAPTION) )
      {
        Board_dy += GetSystemMetrics(SM_CYCAPTION);
      }

      if( WS_DLGFRAME == (style & WS_DLGFRAME) )
      {
        Board_dx += 2 * GetSystemMetrics(SM_CXBORDER);
        Board_dy += 2 * GetSystemMetrics(SM_CYBORDER);
      }
      else if( WS_BORDER == (style & WS_BORDER) )
      {
        Board_dx += GetSystemMetrics(SM_CXBORDER);
        Board_dy += GetSystemMetrics(SM_CYBORDER);
      }
      else
      {
      }			

      double dx = (double)(TaregetEX_Pos.Width() - Board_dx) / (double)(SrcWndRect.Width() - Board_dx);
      double dy = (double)(TaregetEX_Pos.Height() - Board_dy) / (double)(SrcWndRect.Height() - Board_dy);

      HWND hChild = ::GetWindow(hWnd, GW_CHILD);
      while(hChild != NULL)
      {
        CRect rect;
        GetWindowRect(hChild, &rect);
        CPoint pt_lt(rect.left, rect.top);
        ScreenToClient(hWnd, &pt_lt);
        MoveWindow(hChild, 
          (INT)(pt_lt.x * dx), 
          (INT)(pt_lt.y * dy),
          (INT)(rect.Width() * dx), 
          (INT)(rect.Height() * dy), true);

        hChild = GetWindow(hChild, GW_HWNDNEXT);
      }
    }
    //MoveWindow(hWnd, TaregetEX_Pos.left, TaregetEX_Pos.top, TaregetEX_Pos.right, TaregetEX_Pos.bottom, true);
    UINT nFlage = SWP_NOZORDER|SWP_DRAWFRAME|SWP_DEFERERASE;
    if(IsVisable) nFlage |= SWP_SHOWWINDOW;
    ::SetWindowPos(hWnd, 0, 
      TaregetEX_Pos.left, TaregetEX_Pos.top, TaregetEX_Pos.Width(), TaregetEX_Pos.Height(),
      nFlage);
  }
  return(false);
}


//应用
CCCCDlg::OnInitDialog()
{
 CDialog::OnInitDialog();

 Zoom_Window(GetSafeHwnd(), 
   CRect(0,0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)) );
 ……
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值