小池啓仁 ヒロヒト応援ブログ By はてな

小池啓仁(コイケヒロヒト)の動画など。

小池啓仁 ヒロヒト応援ブログ By はてな

フォームをデスクトップの任意の位置に表示させる

VB2005でフォームをデスクトップの任意の位置に表示させるには、以下の4つの要素が必要です。

  1. デスクトップの高さ(System.Windows.Forms.Screen.GetWorkingArea(Me).Height)
  2. デスクトップの幅(System.Windows.Forms.Screen.GetWorkingArea(Me).Width)
  3. フォームの高さ(Me.Size.Height)
  4. フォームの幅(Me.Size.Width)

この4つの要素の値がわかれば、上手く計算して座標を求めることができ、デスクトップの任意の位置に表示することができますね。
そして、表示するメソッドは、『SetDesktopLocation(幅,高さ)』を使用します。

Dim intDesktopHeight As Integer
Dim intDesktopWidth As Integer
Dim intFormHeight As Integer
Dim intFormWidth As Integer

intDesktopHeight = System.Windows.Forms.Screen.GetWorkingArea(Me).Height
intDesktopWidth = System.Windows.Forms.Screen.GetWorkingArea(Me).Width
intFormHeight = Me.Size.Height
intFormWidth = Me.Size.Width

'デスクトップ左上
Me.SetDesktopLocation(0, 0)
'デスクトップ左下
Me.SetDesktopLocation(0, intDesktopHeight - intFormHeight)
'デスクトップ右上
Me.SetDesktopLocation(intDesktopWidth - intFormWidth, 0)
'デスクトップ右下
Me.SetDesktopLocation(intDesktopWidth - intFormWidth, intDesktopHeight - intFormHeight)