'変数の明示的な宣言を強制します。
Option Explicit
'API関数 SetWindowPos の使用を宣言します。
Private Declare Function SetWindowPos Lib "user32"
_
(ByVal hwnd As Long, _
ByVal hWndInsertAfter As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal cx As Long, _
ByVal cy As Long, _
ByVal wFlags As Long) As Long
'第1引数:ウインドウのハンドル
'第2引数:ウインドウのZオーダーを指定する定数(以下はその一部)
Const HWND_TOPMOST = -1 'ウインドウを最前面に置く
Const HWND_NOTOPMOST = -2 'ウインドウを最前面に指定されたウインドウの下に置く
'第3引数:ウインドウのX座標
'第4引数:ウインドウのY座標
'第5引数:ウインドウの幅
'第6引数:ウインドウの高さ
'第7引数:ウインドウの位置とサイズを指定する定数(以下はその一部)
Const SWP_NOMOVE = &H2 'ウインドウの位置(X座標,Y座標)は指定しない
Const SWP_NOSIZE = &H1 'ウインドウのサイズ(幅,高さ)は指定しない
'戻り値 :成功すると"0"以外。失敗すると"0"。
Private Sub Command1_Click()
'SetWindowPos の戻り値
Dim Ret As Long
'もしチェックボタンがチェックされていたら...
If Check1.Value = 1 Then
Ret = SetWindowPos(Form1.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE
Or SWP_NOSIZE)
Else
Ret = SetWindowPos(Form1.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE
Or SWP_NOSIZE)
End If
End Sub
Private Sub Form_Load()
Form1.Caption = "SetWindowPosの使用例"
Command1.Caption = "設定"
Check1.Caption = "フォームを常に手前に表示する"
End Sub