通常フォームの大きさは、SystemInformation.MaxWindowTrackSizeプロパティで取得できる大きさより大きくすることができません。これは、「フォームの位置と大きさを取得、変更する」や「フォームのサイズを制限する」で説明している通りです。
どうしてもフォームをMaxWindowTrackSize以上の大きさにしたい場合は、Win32 APIを使用する必要があります。これは、こちらで紹介されている方法です。
以下にフォームをMaxWindowTrackSize以上の大きさにするコードの例を示します。このコードは、フォームクラスに記述してください。
<System.Runtime.InteropServices.DllImport("user32.dll")> _
Private Shared Function MoveWindow( _
ByVal hWnd As IntPtr, _
ByVal X As Integer, _
ByVal Y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal bRepaint As Boolean) As Boolean
End Function
''' <summary>
''' ウィンドウの位置とサイズを変更する
''' </summary>
''' <param name="rect">変更後のウィンドウの位置とサイズ</param>
Public Sub SetWindowBounds(ByVal rect As Rectangle)
'MaximumSizeを大きくしておく
If Me.MaximumSize.Width < rect.Width Then
Me.MaximumSize = _
New Size(rect.Width, Me.MaximumSize.Height)
End If
If Me.MaximumSize.Height < rect.Height Then
Me.MaximumSize = _
New Size(Me.MaximumSize.Width, rect.Height)
End If
MoveWindow(Me.Handle, _
rect.X, rect.Y, rect.Width, rect.Height, True)
Me.UpdateBounds()
End Sub
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool MoveWindow(
IntPtr hWnd,
int X,
int Y,
int nWidth,
int nHeight,
bool bRepaint);
/// <summary>
/// ウィンドウの位置とサイズを変更する
/// </summary>
/// <param name="rect">変更後のウィンドウの位置とサイズ</param>
public void SetWindowBounds(Rectangle rect)
{
//MaximumSizeを大きくしておく
if (this.MaximumSize.Width < rect.Width)
this.MaximumSize =
new Size(rect.Width, this.MaximumSize.Height);
if (this.MaximumSize.Height < rect.Height)
this.MaximumSize =
new Size(this.MaximumSize.Width, rect.Height);
MoveWindow(this.Handle, rect.X, rect.Y,
rect.Width, rect.Height, true);
this.UpdateBounds();
}
注意:この記事では、基本的な事柄の説明が省略されているかもしれません。初心者の方は、特に以下の点にご注意ください。