p{
font-size: 15px;
}
.alexrootdiv>div{
background: #eeeeee; border: 1px solid #aaa; width: 99%; padding: 5px; margin: 1em 0 1em 0;
}
.alextitlep{
font-size: 18px; font-weight: bold; color: red;
}
.alexrootdiv span{
color:blue;font-weight:bold;
}
.alexrootdiv table{
margin-top:10px;border-collapse:collapse;border:1px solid #aaa;width:100%;
}
.alexrootdiv table th{
vertical-align:baseline;padding:5px 15px 5px 6px;background-color:#d5d5d5;border:1px solid #aaa;text-align:left;
}
.alexrootdiv table td{
vertical-align:text-top;padding:6px 15px 6px 6px;background-color:#efefef;border:1px solid #aaa;
}
.attentationp{
font-size: 15px; text-indent: 2em; background: #ee0; color: red; font-weight: bold;
}
.borderstylediv{
width:12vw;text-align:center;
}
img{box-shadow:0 0 14px;}
-->
设备环境
GDI绘图是在设备环境上实施的,而我们使用GDI的基本步骤如下:
1、获取或建立设备环境
2、在设备环境中绘图
3、释放设备环境
那么接下来我们首先来了解下设备环境的获取、建立以及释放方法。之所以将释放方法放到前面来讲,是因为释放的方法与获取和建立的方法是相互对应的。我们常用的函数有以下一些:
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" ( _
ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As DEVMODE) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetDCEx Lib "user32" (ByVal hwnd As Long, ByVal hrgnclip As Long, ByVal fdwOptions As Long) As Long
Public Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Public Declare Function WindowFromDC Lib "user32" (ByVal hdc As Long) As Long
上述API函数中,CreateDC函数使用到了一个数据结构DEVMODE,下面将该数据结构的定义方式列出:
Public Type DEVMODE
dmDeviceName As String * CCHDEVICENAME
dmSpecVersion As Integer
dmDriverVersion As Integer
dmSize As Integer
dmDriverExtra As Integer
dmFields As Long
dmOrientation As Integer
dmPaperSize As Integer
dmPaperLength As Integer
dmPaperWidth As Integer
dmScale As Integer
dmCopies As Integer
dmDefaultSource As Integer
dmPrintQuality As Integer
dmColor As Integer
dmDuplex As Integer
dmYResolution As Integer
dmTTOption As Integer
dmCollate As Integer
dmFormName As String * CCHFORMNAME
dmUnusedPadding As Integer
dmBitsPerPel As Long
dmPelsWidth As Long
dmPelsHeight As Long
dmDisplayFlags As Long
dmDisplayFrequency As Long
End Type
以上函数中以Create开头的将会新创建一个设备环境DC,而Get开头的函数是通过窗体句柄获取相应窗体对应的DC对象。另外对于通过Create创建的设备环境,我们在最后释放时,是调用DeleteDC函数。而对于通过Get函数获取的DC对象需要使用ReleaseDC函数来完成释放。
在VB6中,如果需要在窗体上绘图,我们可以直接使用窗体本身的hDC属性来获取;如果是在VBA或者其他不能直接获取DC对象的情况下,我们就需要使用以上所提供的函数来创建或获取相应的DC对象。