ウィンドウズプログラミング講座第7回:ウィンドウサイズ制限
概要
ドキュメントウィンドウのサイズを固定にしたいので、まずはメインウィンドウのサイズ変更に制限を設けたいと思います。
ドキュメントウィンドウ
class DocView : public Window { static const INT width = 640; static const INT height = 480; };
説明
ドキュメントウィンドウの最大サイズを定義しました。
メインウィンドウ
class App : public Window { VOID AdjustSize(); LRESULT OnWmGetMinMaxInfo(); LRESULT OnViewToolBar(); LRESULT OnViewStatBar(); };
説明
サイズ制限に必要な関数を追加しました。
VOID App :: AdjustSize() { SIZE min; SIZE max; RECT rc; GetMinMaxSize( min, max ); GetWindowRect( hwnd, & rc ); if ( rc.right - rc.left < min.cx ) { rc.right = rc.left + min.cx; } else if ( rc.right - rc.left > max.cx ) { rc.right = rc.left + max.cx; } if ( rc.bottom - rc.top < min.cy ) { rc.bottom = rc.top + min.cy; } else if ( rc.bottom - rc.top > max.cy ) { rc.bottom = rc.top + max.cy; } MoveWindow( hwnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, TRUE ); }
説明
ツールバーとステータスバーの表示/非表示が切り替えられたら、これを呼んでサイズ制限範囲に照らし合わせてメインウィンドウのサイズを調整します。
VOID App :: GetMinMaxSize( SIZE & min, SIZE & max ) { DWORD dwStyle = GetWindowLong( hwnd, GWL_STYLE ); DWORD dwExStyle = GetWindowLong( hwnd, GWL_EXSTYLE ); RECT rc = { 0 }; RECT rc2; if ( bToolBar ) { GetWindowRect( hToolBar, & rc2 ); rc.bottom += rc2.bottom - rc2.top; } if ( bStatBar ) { GetWindowRect( hStatBar, & rc2 ); rc.bottom += rc2.bottom - rc2.top; } AdjustWindowRectEx( & rc, dwStyle, TRUE, dwExStyle ); min.cx = rc.right - rc.left + 200; min.cy = rc.bottom - rc.top + 100; max.cx = min.cx + docView.width; max.cy = min.cy + docView.height; }
説明
ツールバーとステータスバーの表示状態に応じて最大サイズを計算します。
LRESULT App :: OnWmGetMinMaxInfo() { LPMINMAXINFO lpmmi = ( LPMINMAXINFO ) lparam; SIZE min; SIZE max; GetMinMaxSize( min, max ); lpmmi->ptMinTrackSize.x = min.cx; lpmmi->ptMinTrackSize.y = min.cy; lpmmi->ptMaxTrackSize.x = max.cx; lpmmi->ptMaxTrackSize.y = max.cy; return 0; }
説明
メインウィンドウのサイズ制限の問い合わせメッセージに答えます。
LRESULT App :: OnViewToolBar() { AdjustSize(); } LRESULT App :: OnViewStatBar() { AdjustSize(); }
説明
ツールバーとステータスバーの表示切り替え時にサイズ調整します。