Home » Windows API

MoveWindow

說明:
MoveWindow 函式可變更指定視窗的位置與大小


引用函式庫:
User32


適用系統:
Windows NT 3.1 或 Windows 95以上


函式原型:

Declare Function MoveWindow Lib "user32" Alias "MoveWindow" ( _
    ByVal hwnd As Long, _
    ByVal x As Long, _
    ByVal y As Long, _
    ByVal nWidth As Long, _
    ByVal nHeight As Long, _
    ByVal bRepaint As Long _
    ) As Long



參數型態及說明:
hwndLong 要移動或是改變大小的視窗代碼(Handle)
xLong 視窗左上方的新 X 座標
yLong 視窗左上方的新 Y 座標
nWidthLong 調整後視窗的寬
nHeightLong 調整後視窗的高
bRepaintLong 調整後視窗是否重繪,若要重繪則設為 True;反之則設為 False

回傳值:
Long 呼叫成功的話,將會回傳非零的數值;若呼叫失敗的話,則回傳 0

.NET Framework API:
System.Windows.Forms.Form.SetBounds
System.Windows.Forms.Form.DesktopLocation
System.Windows.Forms.Form.Size


VB範例:

檢視原始碼 Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'VB的宣告
Private Declare Function MoveWindow Lib "user32" ( _
	ByVal hwnd As Long, _
	ByVal x As Long, _
	ByVal y As Long, _
	ByVal nWidth As Long, _
	ByVal nHeight As Long, _
	ByVal bRepaint As Long _
	) As Long
 
Dim rtvl As Long
 
'移動目前執行視窗到(500, 50)的位置,並改變其寬高為500
rtvl = MoveWindow(Form1.hwnd, 500, 50, 500, 500, True)

RB範例:

檢視原始碼 REALBasic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'RB的宣告
Declare Function MoveWindow Lib "user32" ( _
	hwnd As Integer, _
	x As Integer, _
	y As Integer, _
	nWidth As Integer, _
	nHeight As Integer, _
	bRepaint As Boolean _
	) As Boolean
 
Dim rtvl As Boolean
 
'移動目前執行視窗到(500, 50)的位置,並改變其寬高為500
rtvl = MoveWindow(Window1.WinHWND, 500, 50, 500, 500, True)

VB.NET範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
'VB.NET的宣告
<DllImport("user32.dll")> _
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 Integer _
	) As Boolean
End Function
 
Dim rtvl As Boolean
 
'移動目前執行視窗到(500, 50)的位置,並改變其寬高為500
rtvl = MoveWindow(Me.Handle, 500, 50, 500, 500, True)

C#範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//C#的宣告
[DllImport("user32")]
public static extern bool MoveWindow(
	IntPtr hwnd, 
	int x, 
	int y, 
	int nWidth, 
	int nHeight, 
	bool bRepaint
	);
 
bool rtvl;
 
//移動目前執行視窗到(500, 50)的位置,並改變其寬高為500
rtvl = MoveWindow(this.Handle, 500, 50, 500, 500, true);

發表迴響