Home » Windows API

CreateCompatibleDC

說明:
CreateCompatibleDC 函式在記憶體中建立指定的裝置環境代碼(Device Context)一樣的裝置環境


引用函式庫:
GDI32


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


函式原型:

Declare Function CreateCompatibleDC Lib "gdi32" Alias "CreateCompatibleDC" ( _
    ByVal hdc As Long _
    ) As Long



參數型態及說明:
hdcLong 要在記憶體中建立的裝置環境代碼(Device Context)

回傳值:
Long 呼叫成功的話,將會回傳建立後的裝置環境代碼(Device Context);若呼叫失敗的話,則回傳 NULL

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


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
'VB的宣告
Private Declare Function CreateCompatibleDC Lib "gdi32" ( _
	ByVal hdc 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 mHdc As Long
 
'在記憶體中建立一個指定裝置環境
mHdc = CreateCompatibleDC(hdc)
 
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
'RB的宣告
Declare Function CreateCompatibleDC Lib "gdi32" ( _
	hdc As Integer _
	) As Integer
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 mHdc As Integer
 
'在記憶體中建立一個指定裝置環境
mHdc = CreateCompatibleDC(hdc)
 
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
'VB.NET的宣告
<DllImport("gdi32.dll")> _
Shared Function CreateCompatibleDC( _
	ByVal hdc As IntPtr _
	) As IntPtr
End Function
<DllImport("user32.dll")> _
Shared Function ReleaseDC( _
	ByVal hwnd As IntPtr, _
	ByVal hdc As IntPtr _
	) As UInteger
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 mHdc As IntPtr
 
'在記憶體中建立一個指定裝置環境
mHdc = CreateCompatibleDC(hdc)
 
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
//C#的宣告
[DllImport("gdi32")]
public static extern IntPtr CreateCompatibleDC(
	IntPtr hdc
	);
[DllImport("user32")]
public static extern uint ReleaseDC(
	IntPtr hwnd, 
	IntPtr hdc
	);
[DllImport("user32")]
public static extern IntPtr GetDC(
	IntPtr hwnd
	);
 
IntPtr hdc;
 
//取得目前執行視窗的裝置環境代碼(Device Context)
hdc = GetDC(this.Handle);
 
IntPtr mHdc;
 
//在記憶體中建立一個指定裝置環境
mHdc = CreateCompatibleDC(hdc);
 
uint r;
 
//釋放目前執行視窗的裝置環境代碼(Device Context)
r = ReleaseDC(this.Handle, hdc);

註釋:
若是 hdc 參數是 NULL 的話,則會建立目前應用程式畫面的裝置環境。不用時要用 DeleteDC 函式來釋放它

發表迴響