Dim sx As Long 'スクリーンx座標
Dim sy As Long 'スクリーンy座標
Dim mp As RECT '自分のウインドウの場所
Dim x As Long
Dim y As Long
sx=GetSystemMetrics(SM_CXSCREEN)/2
sy=GetSystemMetrics(SM_CYSCREEN)/2
GetWindowRect(hMainWnd,mp)
x=mp.left+( sx-( (mp.left+mp.right)/2 ) )
y=mp.top+( sy-( (mp.top+mp.bottom)/2 ) )
SetWindowPos(hMainWnd,HWND_TOP,x,y,0,0,SWP_NOSIZE or SWP_SHOWWINDOW)
Function SetWindowCenter(hWnd As HWND,dx As Long,dy As Long) As Long
Dim x As Long,y As Long 'ウインドウの位置
Dim sx As Long,sy As Long 'スクリーン座標
Dim Basho As RECT 'ウインドウのrc
'ウインドウの位置を取得
GetWindowRect(hWnd,Basho)
'スクリーンの大きさ取得
sx=GetSystemMetrics(SM_CXSCREEN)
sy=GetSystemMetrics(SM_CYSCREEN)
If sx=0 or sy=0 Then
SetWindowCenter=0
Exit Function
End If
'中央に持っていく計算
x=(sx/2) - ( ((Basho.left+Basho.right)/2)-Basho.left )
y=(sy/2) - ( ((Basho.top+Basho.bottom)/2)-Basho.top )
x=x+dx
y=y+dy
If x<0 Then
x=0
Else If x>sx Then
x=sx - (Basho.right-Basho.left)
End If
If y<0 Then
y=0
Else If y>sy Then
y=sy - (Basho.bottom-Basho.top)
End If
'中央に持っていく
If SetWindowPos(hWnd,HWND_TOP,x,y,0,0,SWP_NOSIZE)=0 Then
SetWindowCenter=0
Exit Function
End If
SetWindowCenter=1
End Function
Sub SetWindowCenterIn(hOWnd As HWND,hWnd As HWND,sx As Long,sy As Long)
Dim Basho As RECT
Dim my As RECT
Dim x As Long,y As Long
GetWindowRect(hOWnd,Basho)
GetWindowRect(hWnd,my)
x=( (Basho.left+Basho.right)/2 ) - ( ((my.left+my.right)/2)-my.left )
y=( (Basho.top+Basho.bottom)/2 ) - ( ((my.top+my.bottom)/2)-my.top )
x=x+sx
y=y+sy
SetWindowPos(hWnd,NULL,x,y,0,0,SWP_NOSIZE)
End Sub
Function SetWindowCenter(hWnd As HWND,dx As Long,dy As Long) As Long
Dim x As Long,y As Long 'ウインドウの位置
Dim sx As Long,sy As Long 'スクリーン座標
Dim Basho As RECT 'ウインドウのrc
'ウインドウの位置を取得
GetWindowRect(hWnd,Basho)
'スクリーンの大きさ取得
sx=GetSystemMetrics(SM_CXSCREEN)
sy=GetSystemMetrics(SM_CYSCREEN)
If sx * sy = 0 Then
SetWindowCenter=0
Exit Function
End If
'中央に持っていく計算
x=(sx-Basho.right+Basho.left)/2+dx
y=(sy-Basho.bottom+Basho.top)/2+dy
If x<0 Then
x=0
Else If x>sx-Basho.right+Basho.left Then
x=sx-Basho.right+Basho.left
End If
If y<0 Then
y=0
Else If y>sy-Basho.bottom+Basho.top Then
y=sy-Basho.bottom+Basho.top
End If
'中央に持っていく
SetWindowCenter=SetWindowPos(hWnd,HWND_TOP,x,y,0,0,SWP_NOSIZE)
End Function
Function SetWindowCenter(hWnd As HWND,dx As Long,dy As Long) As Long
Dim x As Long,y As Long 'ウインドウの位置
Dim sx As Long,sy As Long 'スクリーン座標
Dim Basho As RECT 'ウインドウのrc
'ウインドウの位置を取得
GetWindowRect(hWnd,Basho)
'スクリーンの大きさ取得
sx=GetSystemMetrics(SM_CXSCREEN)
sy=GetSystemMetrics(SM_CYSCREEN)
If sx * sy = 0 Then
SetWindowCenter=0
Exit Function
End If
'中央に持っていく計算
x=(sx-Basho.right+Basho.left)/2+dx
y=(sy-Basho.bottom+Basho.top)/2+dy
If x<0 Then
x=0
Else If x>sx-Basho.right+Basho.left Then
x=sx-Basho.right+Basho.left
End If
If y<0 Then
y=0
Else If y>sy-Basho.bottom+Basho.top Then
y=sy-Basho.bottom+Basho.top
End If
'中央に持っていく
SetWindowCenter=SetWindowPos(hWnd,HWND_TOP,x,y,0,0,SWP_NOSIZE)
End Function
Function SetWindowCenter(hWnd As HWND,dx As Long,dy As Long) As Long
Dim x As Long,y As Long 'ウインドウの位置
Dim sx As Long,sy As Long 'スクリーン座標
Dim Basho As RECT 'ウインドウのrc
'ウインドウの位置を取得
GetWindowRect(hWnd,Basho)
'スクリーンの大きさ取得
sx=GetSystemMetrics(SM_CXSCREEN)
sy=GetSystemMetrics(SM_CYSCREEN)
If sx * sy = 0 Then
SetWindowCenter=0
Exit Function
End If
'中央に持っていく計算
x=(sx-Basho.right+Basho.left)/2+dx
y=(sy-Basho.bottom+Basho.top)/2+dy
If x<0 Then
x=0
Else If x>sx-Basho.right+Basho.left Then
x=sx-Basho.right+Basho.left
End If
If y<0 Then
y=0
Else If y>sy-Basho.bottom+Basho.top Then
y=sy-Basho.bottom+Basho.top
End If
'中央に持っていく
SetWindowCenter=SetWindowPos(hWnd,HWND_TOP,x,y,0,0,SWP_NOSIZE)
End Function