VC++之隨父視窗變化調整控制項大小

來源:互聯網
上載者:User

如果對話方塊或視類的大小調後,控制項的大小和位置沒有變化,介面看起來會很不爽
控制項是從CWnd派生的,但不能使用SetWindowPos()或OnSize()或OnSizing()來改變其大小,應在父視窗的WM_SIZE訊息中使用MoveWindow()來進行調整。

VC++之根據對話方塊大小調整控制項大小
1、在對話方塊類中加入成員變數CRect m_rect;用於儲存對話方塊大小變化前的大小;
2、在對話方塊的OnInitDialog()函數中擷取對話方塊建立時的大小:GetClientRect(&m_rect);
3、在WM_SIZE的響應函數OnSize()中加入以下代碼:
 

CWnd *pWnd;
 pWnd = GetDlgItem(IDC_LIST);     //擷取控制項控制代碼
 if(pWnd)//判斷是否為空白,因為對話方塊建立時會調用此函數,而當時控制項還未建立
{
this.style.display='none'; Codehighlighter1_99_455_Open_Text.style.display='none'; Codehighlighter1_99_455_Closed_Image.style.display='inline'; Codehighlighter1_99_455_Closed_Text.style.display='inline';
}" src="http://image2.360doc.com/DownloadImg/2008/12/10/799_2094373_2.gif" alt="" align="top"> {
  CRect rect;   //擷取控制項變化前大小
  pWnd->GetWindowRect(&rect);
  ScreenToClient(&rect);//將控制項大小轉換為在對話方塊中的地區座標
  // cx/m_rect.Width()為對話方塊在橫向的變化比例
{
this.style.display='none'; Codehighlighter1_279_290_Open_Text.style.display='none'; Codehighlighter1_279_290_Closed_Image.style.display='inline'; Codehighlighter1_279_290_Closed_Text.style.display='inline';
}" src="http://image2.360doc.com/DownloadImg/2008/12/10/799_2094373_4.gif" alt="" align="top">  rect.left=rect.left*cx/m_rect.Width();/////調整控制項大小
  rect.right=rect.right*cx/m_rect.Width();
  rect.top=rect.top*cy/m_rect.Height();
  rect.bottom=rect.bottom*cy/m_rect.Height();
  pWnd->MoveWindow(rect);//設定控制項大小
 }
 GetClientRect(&m_rect);//將變化後的對話方塊大小設為舊大小

 

不過有個問題,就是視窗最小化後再讓它顯示就會報錯了,除零錯誤。

 

轉自:http://www.360doc.com/content/081210/17/799_2094373.html

 

VC中自動改變控制項位置和大小的對話方塊類(轉)

http://www.360doc.com/content/081209/22/799_2087989.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.