指定した幅、高さをもつウィンドウの作成
絶対的なクライエント領域をもつフォームはフォームの ScaleWidth、ScaleHeiht プロパティを 操作すればよさそうですが、このプロパティの操作では ScaleMode がユーザ定義になってしまいます。 ScaleMode プロパティが Twips を保持するには Width、Height プロパティの操作で ScaleWidth、ScaleHeight プロパティを調整しなければなりません。 でもフォームのクライエント領域の周囲には ボーダー幅、キャプション高さ、メニュー高さがあり これらは [コントロールパネル]-[画面]-[デザイン] の設定でユーザごとにまちまちです。 ボーダー幅やキャプション高さの値がどんな値でもフォームの周囲の情報を教えてくれる API があります。 AdjustWindowRectEx() API です。 この API からの情報を Width、Height プロパティに代入すれば目的の幅、高さのウィンドウを作成することが可能です。 サンプルは以下のとおりです。 *---------*---------*---------*---------*---------*---------*---------*---------* 以下のコードをフォームモジュールに貼り付けて実行してみてください。 Option Explicit Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Private Declare Function AdjustWindowRectEx Lib "user32" (lpRect As RECT, ByVal dsStyle As Long, ByVal bMenu As Long, ByVal dwEsStyle As Long) As Long Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Private Const GWL_EXSTYLE = (-20) Private Const GWL_STYLE = (-16) Private Sub Form_Click() Dim rc As RECT GetClientRect Me.hwnd, rc MsgBox rc.Right & vbCr & rc.Bottom & vbCr End Sub Private Sub Form_Load() Dim dws&, dwes&, rc As RECT rc.Left = 0 rc.Top = 0 rc.Right = 300 rc.Bottom = 200 AdjustWindowRectEx rc, GetWindowLong(Me.hwnd, GWL_STYLE), 0, GetWindowLong(Me.hwnd, GWL_EXSTYLE) Me.Width = (rc.Right - rc.Left) * Screen.TwipsPerPixelX Me.Height = (rc.Bottom - rc.Top) * Screen.TwipsPerPixelY End Sub サンプルでは 300 ピクセルの幅、200 ピクセルの高さをもつフォームを作成しています。 |
動作確認
Windows98 Second Edition + Visual Basic 6 (+SP4)このサンプルを使用しての感想や、質問は 掲示板や、 らくがき帳に書きとめて頂けるとうれしいです。