ab.com コミュニティ https://www.activebasic.com/forum/ |
|
ウインドウを画面の中央に表示 https://www.activebasic.com/forum/viewtopic.php?t=2573 |
ページ 1 / 1 |
作成者: | 水波形 [ 2009年6月07日(日) 20:49 ] |
記事の件名: | ウインドウを画面の中央に表示 |
ABで作成したウインドウは、何も指定しなければ、左端で起動のたびにうろちょろします。 ふと、『メッセージボックスのように中央寄せ出来ないかな・・・?』と思い、コードを書いてみました。 何もひねっていませんし、かなり単純なので、余り使い道がないと思いますが・・・(^^; [ここをクリックすると内容が表示されます]
あとは、足したり引いたりして、好きな場所に移動できます。コード: 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) 何か、変な場所がありましたら、指摘してあげてください。 もしも、重複がありましたら、申し訳ございません。 |
作成者: | たかせ [ 2011年2月07日(月) 14:36 ] |
記事の件名: | Re: ウインドウを画面の中央に表示 |
> 何もひねっていませんし、かなり単純なので、余り使い道がないと思いますが・・・(^^; 大変重宝しています。 WIN-XPデスクトップ(画素数1024*768)で作成した1024*740のノーマルウインドウのプログラムに上記のソースコードを追加してWIN-7ノート(画素数1366*768)で実行すると画面中央に表示されて見栄えがよくなりました。 2013年9月3日修正 |
作成者: | 水波形 [ 2011年2月27日(日) 17:20 ] |
記事の件名: | Re: ウインドウを画面の中央に表示 |
引用: > > 何もひねっていませんし、かなり単純なので、余り使い道がないと思いますが・・・(^^;
おお!> > 大変重宝しています。 > WIN-XPデスクトップ(画素数1024*768)で作成した1024*740のノーマルウインドウのプログラムに上記のソースコードを追加してWIN-7ノート(画素数1368*768)で実行すると画面中央に表示されて見栄えがよくなりました。 お役に立ててよかったです。 ちなみに私は、こんな感じで関数にして使っています。 [ここをクリックすると内容が表示されます]
かっこつけてFunctionで定義してますが、正直Subでよかったです(^^;
hWnd→中央に持っていく対象のウインドウ dx→中央からのズレ(x軸。中央に持っていく場合、この引数は0) dy→中央からのズレ(y軸。中央に持っていく場合、この引数は0) コード: 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 後、ウインドウを既存のウインドウの中央に持って行くのもこれを応用して使ってます。 [ここをクリックすると内容が表示されます]
良かったらどうぞ~
hOWnd→ここで指定したウインドウの中央に持って行きます。 hWnd→ここで指定したウインドウをhOWndの中央に持っていきます。 コード: 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 |
作成者: | たかせ [ 2011年2月27日(日) 22:31 ] |
記事の件名: | Re: ウインドウを画面の中央に表示 |
投稿なさった関数を SetWindowCenter(hMainWnd,0,-28)で指定したら、 画面の中央に表示することをWindows7のPCで確認しました。 これでフルHD(画素数1920×1080)でも十分いけますね? 本当にどうもありがとうございました。 |
作成者: | HSABP [ 2011年2月28日(月) 15:49 ] |
記事の件名: | ウインドウ内に収める |
上記プログラミングはdx,dyによって右と下はウインドウがはみ出るのことがあるで それが嫌な人は コード: 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 |
作成者: | HSABP [ 2011年2月28日(月) 15:50 ] |
記事の件名: | ウインドウ内に収める |
上記プログラミングはdx,dyによって右と下はウインドウがはみ出るのことがあるで それが嫌な人は コード: 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 |
作成者: | HSABC [ 2011年2月28日(月) 15:51 ] |
記事の件名: | ウインドウ内に収める |
上記プログラミングはdx,dyによって右と下はウインドウがはみ出るのことがあるで それが嫌な人は コード: 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 |
ページ 1 / 1 | 全ての表示時間は UTC+09:00 です |
Powered by phpBB® Forum Software © phpBB Limited https://www.phpbb.com/ |