Home » Windows API

Rectangle

說明:
Rectangle 函式會在指定的裝置環境代碼(Device Context)上繪製矩形


引用函式庫:
GDI32


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


函式原型:

Declare Function Rectangle Lib "gdi32" Alias "Rectangle" ( _
    ByVal hdc As Long, _
    ByVal X1 As Long, _
    ByVal Y1 As Long, _
    ByVal X2 As Long, _
    ByVal Y2 As Long _
    ) As Long



參數型態及說明:
hdcLong 要繪製矩形的裝置環境代碼(Device Context)
X1Long 矩形的左上方 X 座標
Y1Long 矩形的左上方 Y 座標
X2Long 矩形的右下方 X 座標
Y2Long 矩形的右下方 Y 座標

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

.NET Framework API:
System.Drawing.Graphics.DrawRectangle
System.Drawing.Graphics.FillRectangle


VB範例:

檢視原始碼 Visual Basic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'VB的宣告
Private Declare Function Rectangle Lib "gdi32" ( _
	ByVal hdc As Long, _
	ByVal X1 As Long, _
	ByVal Y1 As Long, _
	ByVal X2 As Long, _
	ByVal Y2 As Long _
	) As Long
Private Declare Function ReleaseDC Lib "user32" ( _
	ByVal hwnd As Long, _
	ByVal hdc As Long _
	) As Long
Private Declare Function GetDC Lib "user32" ( _
	ByVal hwnd As Long _
	) As Long
 
Dim hdc As Long
 
'取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(Form1.hwnd)
 
Dim rec As Long
 
'在目前執行視窗上的(0, 0)畫一個長高為100的矩形
rec = Rectangle(hdc, 0, 0, 100, 100)
 
Dim r As Long
 
'釋放目前執行視窗的裝置環境代碼(Device Context)
r = ReleaseDC(Form1.hwnd, hdc)

RB範例:

檢視原始碼 REALBasic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'RB的宣告
Declare Function Rectangle Lib "gdi32" ( _
	hdc As Integer, _
	X1 As Integer, _
	Y1 As Integer, _
	X2 As Integer, _
	Y2 As Integer _
	) As Boolean
Declare Function ReleaseDC Lib "user32" ( _
	hwnd As Integer, _
	hdc As Integer _
	) As Integer
Declare Function GetDC Lib "user32" ( _
	hwnd As Integer _
	) As Integer
 
Dim hdc As Integer
 
'取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(Window1.WinHWND)
 
Dim rec As Boolean
 
'在目前執行視窗上的(0, 0)畫一個長高為100的矩形
rec = Rectangle(hdc, 0, 0, 100, 100)
 
Dim r As Integer
 
'釋放目前執行視窗的裝置環境代碼(Device Context)
r = ReleaseDC(Window1.WinHWND, hdc)

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
24
25
26
27
28
29
30
31
32
33
34
35
36
'VB.NET的宣告
<DllImport("gdi32.dll")> _
Shared Function Rectangle( _
	ByVal hdc As IntPtr, _
	ByVal X1 As Integer, _
	ByVal Y1 As Integer, _
	ByVal X2 As Integer, _
	ByVal Y2 As Integer _
	) As Boolean
End Function
<DllImport("user32.dll")> _
Shared Function ReleaseDC( _
	ByVal hwnd As IntPtr, _
	ByVal hdc As IntPtr _
	) As Integer
End Function
<DllImport("user32.dll")> _
Shared Function GetDC( _
	ByVal hwnd As IntPtr _
	) As IntPtr
End Function
 
Dim hdc As IntPtr
 
'取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(Me.Handle)
 
Dim rec As Boolean
 
'在目前執行視窗上的(0, 0)畫一個長高為100的矩形
rec = Rectangle(hdc, 0, 0, 100, 100)
 
Dim r As Integer
 
'釋放目前執行視窗的裝置環境代碼(Device Context)
r = ReleaseDC(Me.Handle, hdc)

C#範例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//C#的宣告
[DllImport("gdi32")]
public static extern bool Rectangle(
	IntPtr hdc, 
	int X1, 
	int Y1, 
	int X2, 
	int Y2
	);
[DllImport("user32")]
public static extern int ReleaseDC(
	IntPtr hwnd, 
	IntPtr hdc
	);
[DllImport("user32")]
public static extern IntPtr GetDC(
	IntPtr hwnd
	);
 
IntPtr hdc;
 
//取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(this.Handle);
 
bool rec;
 
//在目前執行視窗上的(0, 0)畫一個長高為100的矩形
rec = Rectangle(hdc, 0, 0, 100, 100);
 
int r;
 
//釋放目前執行視窗的裝置環境代碼(Device Context)
r = ReleaseDC(this.Handle, hdc);

發表迴響