关于内存DC画图的研究
本帖最后由 JCHB1234 于 2012-12-07 09:52:35 编辑 Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long
'Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal lpString As String, ByVal nCount As Long) As Long
Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function StrokeAndFillPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Const SRCAND = &H8800C6
Private Const SRCCOPY = &HCC0020
Private Sub Command1_Click()
Dim DFORM As Long
Dim DBMP As Long
Dim hBrush As Long
Dim oldbrush As Long
Dim LNGP As Long
Picture1.AutoRedraw = True
Picture1.ScaleMode = vbPixels
DFORM = CreateCompatibleDC(Picture1.hdc)
DBMP = CreateCompatibleBitmap(DFORM, Picture1.ScaleWidth, Picture1.ScaleHeight)
hBrush = CreateSolidBrush(vbRed)
oldbrush = SelectObject(DFORM, hBrush)
SelectObject DFORM, DBMP
'BeginPath DFORM ''***''
TextOut DFORM, 0, 0, "12345", 5
'EndPath DFORM ''***''
'StrokeAndFillPath DFORM ''***''
LNGP = BitBlt(Picture1.hdc, 0, 0, 300, 300, DFORM, 0, 0, SRCCOPY)
DeleteObject hBrush
DeleteObject oldbrush
DeleteObject DBMP
DeleteObject DFORM
Picture1.Refresh
End Sub
上面的代码,PICTURE1里看到黑色12345,如果去掉*号的注示行,只有一片黑,但经测试,不是
AUTORERAW引起的,不知为何,请高手帮忙,如果直接对PICTURE1定义画刷,效果正常,好像内存位图和内存DC与
画刷有冲突.
[解决办法]
使用GDI+显然更好,参阅:
http://download.csdn.net/detail/veron_04/4039695
http://download.csdn.net/detail/veron_04/4814790
[解决办法]
oldbrush = SelectObject(DFORM, hBrush)
SelectObject DFORM, DBMP
把这两行代码顺序掉转一下看效果~
[解决办法]
你试了我上面说的方法还是不行的话,就应该是背景色填充的问题了
因为新创建的内存 DC 默认背景色是黑色的
你可以在输出文字前, 把内存 DC 的背景填充一下
SetBkColor ...
或
FillRect ....
然后设置文字输出透明:
SetBkMode TRANSPARENT
这样的话, 背景会显示你设置的颜色, 文字应该是黑色
[解决办法]
http://download.csdn.net/detail/veron_04/2368159
[解决办法]
CreateCompatibleDC创建的内存DC,里面的位图是单色(只有黑白二色)的,接着DBMP = CreateCompatibleBitmap(DFORM, Picture1.ScaleWidth, Picture1.ScaleHeight),得到的位图DBMP也是单色的,且背景是黑色.
修正:
DBMP = CreateCompatibleBitmap(DFORM, Picture1.ScaleWidth, Picture1.ScaleHeight)
改为
DBMP = CreateCompatibleBitmap(Picture1.hdc, Picture1.ScaleWidth, Picture1.ScaleHeight)
对内存DC操作前应先把位图选进去,(注意背景是黑色的,可fillrect填充其它颜色,或者直接
BitBlt(DFORM, 0, 0, 300, 300, Picture1.hdc, 0, 0, SRCCOPY)
把picture1.hdc的背景copy过去
[解决办法]