Home » Windows API

LineTo

說明:
LineTo 函式會在指定的裝置環境代碼(Device Context)上繪製一直線,並將目前的座標移至直線終點


引用函式庫:
GDI32


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


函式原型:

Declare Function LineTo Lib "gdi32" Alias "LineTo" ( _
    ByVal hdc As Long, _
    ByVal nXEnd As Long, _
    ByVal nYEnd As Long _
    ) As Long



參數型態及說明:
hdcLong 要繪製的裝置環境代碼(Device Context)
nXEndLong 直線的終點 X 座標
nYEndLong 直線的終點 Y 座標

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

.NET Framework API:
System.Drawing.Graphics.DrawLine


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
'VB的宣告
Private Declare Function LineTo Lib "gdi32" ( _
	ByVal hdc As Long, _
	ByVal nXEnd As Long, _
	ByVal nYEnd 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 rtvl As Long
 
'在目前執行視窗上的(0, 0)至(200, 200)畫一條直線
rtvl = LineTo(hdc, 200, 200)
 
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
'RB的宣告
Declare Function LineTo Lib "gdi32" ( _
	hdc As Integer, _
	nXEnd As Integer, _
	nYEnd As Integer _
	) As Boolean
Declare Function ReleaseDC Lib "user32" ( _
	hwnd As Integer, _
	hdc As Integer _
	) As Boolean
Declare Function GetDC Lib "user32" ( _
	hwnd As Integer _
	) As Integer
 
Dim hdc As Integer
 
'取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(Window1.WinHWND)
 
Dim rtvl As Boolean
 
'在目前執行視窗上的(0, 0)至(200, 200)畫一條直線
rtvl = LineTo(hdc, 200, 200)
 
Dim r As Boolean
 
'釋放目前執行視窗的裝置環境代碼(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
'VB.NET的宣告
<DllImport("gdi32.dll")> _
Shared Function LineTo( _
	ByVal hdc As IntPtr, _
	ByVal nXEnd As UInteger, _
	ByVal nYEnd As UInteger _
	) As Boolean
End Function
<DllImport("user32.dll")> _
Shared Function ReleaseDC( _
	ByVal hwnd As IntPtr, _
	ByVal hdc As IntPtr _
	) As Boolean
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 rtvl As Boolean
 
'在目前執行視窗上的(0, 0)至(200, 200)畫一條直線
rtvl = LineTo(hdc, 200, 200)
 
Dim r As Boolean
 
'釋放目前執行視窗的裝置環境代碼(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
//C#的宣告
[DllImport("gdi32")]
public static extern bool LineTo(
	IntPtr hdc, 
	uint nXEnd, 
	uint nYEnd
	);
[DllImport("user32")]
public static extern bool ReleaseDC(
	IntPtr hwnd, 
	IntPtr hdc
	);
[DllImport("user32")]
public static extern IntPtr GetDC(
	IntPtr hwnd
	);
 
IntPtr hdc;
 
//取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(this.Handle);
 
bool rtvl;
 
//在目前執行視窗上的(0, 0)至(200, 200)畫一條直線
rtvl = LineTo(hdc, 200, 200);
 
bool r;
 
//釋放目前執行視窗的裝置環境代碼(Device Context)
r = ReleaseDC(this.Handle, hdc);

發表迴響