本帖最后由 seniors 于 2013-6-16 07:56 编辑
GDI之设置图片到控件
前面几讲,所有的图形只要最小化,窗口界面中就看不到了
原因是,我们直接在控件的设备环境DC中作图,没有能告诉控件,我们想一直显示这个图形
那下面我们来看一下,怎么样让它一直存在
一、首先画图不要直接在DC中;
当然不是不能,在DC中画图其实是很方便的,但是它有缺点
如果画图操作时间比较长,画面会卡顿
最好是其它地方,然后一次把图形拷贝过去这个有一系列的操作
如_WInAPi_BitBlt等一系列blt操作
对这一系列操作现在还不怎么熟悉,先不讲
二、要让画画的能够记住
所以我们要用到位图
_WinAPI_CreateCompatibleBitmap能够创建和DC相兼容的位图
三、怎么样画在位图上
_WinAPI_CreateCompatibleDC建立兼容DC环境,经常会把这个DC叫作内存DC,因为和窗口及控件没有关联
然后把上面的位图调入内存DC
接下来的操作就能画在位图上了
四、把画好的图设置到控件
对于设置有$SS_BITMAP样式的静态控件Label/Pic/Icon
能够使用_SendMessage($hWnd, $STM_SETIMAGE, 0, $hBitmap)设置图片
;$STM_SETIMAGE = 0x0172
引用的图案没人画,我来画吧
画图是很烦的,下一讲,说自绘了
#include <APIConstants.au3>
#include <StaticConstants.au3>
#include <WinAPIEx.au3>
;我用的是Lable
GUICreate("第六讲", 300, 200)
$nPicId = GUICtrlCreateLabel("", 0, 0, 300, 200,BitOR ($SS_NOTIFY, $SS_LEFT,$SS_BITMAP))
$hPicWnd = GUICtrlGetHandle($nPicId)
GUISetState()
gditest()
While 1
$Msg = GUIGetMsg()
Switch $Msg
Case -3
ExitLoop
EndSwitch
WEnd
GUIDelete()
Exit
Func gditest()
;建立与DC相兼容的位图
Local $hDC = _WinAPI_GetDC($hPicWnd)
Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, 300, 200);建立兼容位图,最好不好用_winapi_CreateBitmap建立,
;_winapi_CreateBitmap主要是建立单色位图,当然也能用_winapi_CreateBitmap来建立位图,但是把它调入DC时还是会做兼容处理的
;~ $hCDC = _WinAPI_CreateCompatibleDC($hDC)
_WinAPI_ReleaseDC($hPicWnd, $hDC);释放DC
;创建兼容DC环境,0是屏幕兼容的内存DC,你也可以用上面注释的那句
$hCDC = _WinAPI_CreateCompatibleDC(0)
;把位图调入DC,那对DC的操作就作用到位图上了
$HoldBitmap = _WinAPI_SelectObject($hCDC, $hBitmap)
;清空背景,使用0xEEEEEE画刷
$tRECT = _WinAPI_CreateRect(0, 0, 300, 200)
$hBrush = _WinAPI_CreateSolidBrush(0xEEEEEE)
_WinAPI_FillRect($hCDC, DllStructGetPtr($tRECT), $hBrush)
_WinAPI_DeleteObject($hBrush)
;绘制“引用”图形的路径
_WinAPI_SetArcDirection($hCDC, $AD_CLOCKWISE)
_WinAPI_BeginPath($hCDC)
_WinAPI_MoveTo($hCDC, 50, 10)
_WinAPI_LineTo($hCDC, 250, 10)
$tRECT = _WinAPI_CreateRect(210, 10, 290, 90)
_WinAPI_ArcTo($hCDC, $tRECT, 250, 10, 290, 50)
_WinAPI_LineTo($hCDC, 290, 100)
_WinAPI_OffsetRect($tRECT, 0, 50)
_WinAPI_ArcTo($hCDC, $tRECT, 290, 100, 250, 140)
_WinAPI_LineTo($hCDC, 200, 140)
_WinAPI_LineTo($hCDC, 200, 180)
_WinAPI_LineTo($hCDC, 170, 140)
_WinAPI_LineTo($hCDC, 50, 140)
_WinAPI_OffsetRect($tRECT, -200, 0)
_WinAPI_ArcTo($hCDC, $tRECT, 50, 140, 10, 100)
_WinAPI_LineTo($hCDC, 10, 50)
_WinAPI_OffsetRect($tRECT, 0, -50)
_WinAPI_ArcTo($hCDC, $tRECT, 10, 50, 50, 10)
_WinAPI_EndPath($hCDC)
;设置画笔。画刷
$hBrush = _WinAPI_CreateSolidBrush(0x996633)
$hPen = _WinAPI_CreatePen($PS_SOLID, 1, 0xC0C0C0)
_WinAPI_SelectObject($hCDC, $hBrush)
_WinAPI_SelectObject($hCDC, $hPen)
;路径描边并填充
_WinAPI_StrokeAndFillPath($hCDC)
;设置字体
$hFont = _WinAPI_CreateFont(38, 0, 0, 0, $FW_NORMAL, 0, 0, 0, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $ANTIALIASED_QUALITY, $DEFAULT_PITCH)
_WinAPI_SelectObject($hCDC, $hFont)
_WinAPI_SetBkMode($hCDC, $TRANSPARENT)
_WinAPI_SetTextColor($hCDC, 0xC0C0C0)
_WinAPI_TextOut($hCDC, 90, 20, "“引用”")
_WinAPI_DrawLine($hCDC, 50, 60, 250, 60)
_WinAPI_DrawLine($hCDC, 50, 100, 250, 100)
;删除兼容DC
_WinAPI_DeleteDC($hCDC)
;设置图片到控件
setBitmap($hPicWnd, $hBitmap)
EndFunc ;==>gditest
Func setBitmap($hWnd, $hBitmap)
$hObj = _SendMessage($hWnd, 0x0172, 0, $hBitmap) ;$STM_SETIMAGE = 0x0172
_WinAPI_DeleteObject($hObj)
Local $hObj = _SendMessage($hWnd, 0x0173) ;$STM_GETIMAGE = 0x0173
If $hObj <> $hBitmap Then
_WinAPI_DeleteObject($hBitmap)
EndIf
EndFunc ;==>setBitmap
|