Home » Windows API

GetClientRect

說明:
GetClientRect 函式可取得指定視窗代碼(Handle)的工作區大小


引用函式庫:
User32


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


函式原型:

Declare Function GetClientRect Lib "user32" Alias "GetClientRect" ( _
    ByVal hwnd As Long, _
    ByRef lpRect As RECT _
    ) As Long



參數型態及說明:
hwndLong 要取得工作區大小的視窗代碼(Handle)
lpRectRECT 工作區的 RECT 座標結構

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

.NET Framework API:
System.Windows.Forms.Form.ClientRectangle


VB範例:

檢視原始碼 Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'宣告一個RECT
Private Type RECT
	Left As Long
	Top As Long
	Right As Long
	Bottom As Long
End Type
 
'VB的宣告
Private Declare Function GetClientRect Lib "user32" ( _
	ByVal hwnd As Long, _
	ByRef lpRect As RECT _
	) As Long
 
'設定一個RECT
Dim rec As RECT
 
Dim r As Long
 
'取得目前執行視窗的工作區大小
r = GetClientRect(Form1.hwnd, rec)

RB範例:

檢視原始碼 REALBasic
1
2
3
4
5
6
7
8
9
10
11
12
13
'RB的宣告
Declare Function GetClientRect Lib "user32" ( _
	hwnd As Integer, _
	lpRect As Ptr _
	) As Boolean
 
'設定MemoryBlock陣列 Long(0) = Left, Long(4) = Top, Long(8) = Right, Long(12) = Bottom
Dim rec As New MemoryBlock(16)
 
Dim rtvl As Boolean
 
'取得目前執行視窗的工作區大小
rtvl = GetClientRect(Window1.WinHWND, rec)

VB.NET範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
'宣告一個RECT
Public Structure RECT
	Dim Left As UInteger
	Dim Top As UInteger
	Dim Right As UInteger
	Dim Bottom As UInteger
End Structure
 
'VB.NET的宣告
<DllImport("user32.dll")> _
Shared Function GetClientRect( _
	ByVal hwnd As IntPtr, _
	ByRef lpRect As RECT _
	) As Boolean
End Function
 
'設定一個RECT
Dim rec As RECT
 
Dim rtvl As Boolean
 
'取得目前執行視窗的工作區大小
rtvl = GetClientRect(Me.Handle, rec)

C#範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//宣告一個RECT
public struct RECT {
	public uint Left;
	public uint Top;
	public uint Right;
	public uint Bottom;
}
 
//C#的宣告
[DllImport("user32")]
public static extern bool GetClientRect(
	IntPtr hwnd, 
	out RECT lpRect
	);
 
//設定一個RECT
RECT rec = new RECT();
 
bool rtvl;
 
//取得目前執行視窗的工作區大小
rtvl = GetClientRect(this.Handle, out rec);

註釋:
GetClientRect 函式所取得的 LeftTop 值會是 0RightBottom 值會是 widthheight

發表迴響