设备环境
GDI绘图是在设备环境上实施的,而我们使用GDI的基本步骤如下:
1、获取或建立设备环境
2、在设备环境中绘图
3、释放设备环境
那么接下来我们首先来了解下设备环境的获取、建立以及释放方法。之所以将释放方法放到前面来讲,是因为释放的方法与获取和建立的方法是相互对应的。我们常用的函数有以下一些:
1 Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long 2 Public Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" ( _ 3 ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As DEVMODE) As Long 4 Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long 5 Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long 6 Public Declare Function GetDCEx Lib "user32" (ByVal hwnd As Long, ByVal hrgnclip As Long, ByVal fdwOptions As Long) As Long 7 Public Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long 8 Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long 9 Public Declare Function WindowFromDC Lib "user32" (ByVal hdc As Long) As Long
上述API函数中,CreateDC函数使用到了一个数据结构DEVMODE,下面将该数据结构的定义方式列出:
1 Public Type DEVMODE 2 dmDeviceName As String * CCHDEVICENAME 3 dmSpecVersion As Integer 4 dmDriverVersion As Integer 5 dmSize As Integer 6 dmDriverExtra As Integer 7 dmFields As Long 8 dmOrientation As Integer 9 dmPaperSize As Integer 10 dmPaperLength As Integer 11 dmPaperWidth As Integer 12 dmScale As Integer 13 dmCopies As Integer 14 dmDefaultSource As Integer 15 dmPrintQuality As Integer 16 dmColor As Integer 17 dmDuplex As Integer 18 dmYResolution As Integer 19 dmTTOption As Integer 20 dmCollate As Integer 21 dmFormName As String * CCHFORMNAME 22 dmUnusedPadding As Integer 23 dmBitsPerPel As Long 24 dmPelsWidth As Long 25 dmPelsHeight As Long 26 dmDisplayFlags As Long 27 dmDisplayFrequency As Long 28 End Type
以上函数中以Create开头的将会新创建一个设备环境DC,而Get开头的函数是通过窗体句柄获取相应窗体对应的DC对象。另外对于通过Create创建的设备环境,我们在最后释放时,是调用DeleteDC函数。而对于通过Get函数获取的DC对象需要使用ReleaseDC函数来完成释放。
在VB6中,如果需要在窗体上绘图,我们可以直接使用窗体本身的hDC属性来获取;如果是在VBA或者其他不能直接获取DC对象的情况下,我们就需要使用以上所提供的函数来创建或获取相应的DC对象。