本帖最后由 seniors 于 2012-4-27 08:11 编辑
gdi是一定要设备DC的,没有DC,GDI无法操作
gdi+与设备无关,但最终还是要送到一定的设备上的
总之GDI+功能比GDI多
下面上源码,大家讨论讨论
修改了代码,gdi和gdi+多分了直接绘制和缓冲绘制
#include <WinAPIEx.au3>
#include <GDIPlusEx.au3>
#include <APIConstants.au3>
Opt('GUIOnEventMode', 1)
GUICreate("GDI和GDI+区别", 400, 300)
GUISetOnEvent(-3, "exitfunc")
GUICtrlCreatePic("", 0, 0, 400, 200)
$hPic = GUICtrlGetHandle(-1)
GUICtrlCreateButton("GDI绘图直接绘图", 5, 210)
GUICtrlSetOnEvent(-1, "GDIDraw")
GUICtrlCreateButton("GDI绘图缓冲刷新", 5, 240)
GUICtrlSetOnEvent(-1, "GDIDraw2")
GUICtrlCreateButton("GDI+绘图直接绘图", 205, 210)
GUICtrlSetOnEvent(-1, "GDIPlusDraw")
GUICtrlCreateButton("GDI+绘图缓冲刷新", 205, 240)
GUICtrlSetOnEvent(-1, "GDIPlusDraw2")
GUISetState()
While 1
Sleep(100)
WEnd
Func exitfunc()
Exit
EndFunc ;==>exitfunc
Func GDIDraw()
$hDC = _WinAPI_GetDC($hPic)
;写字符串的方式一
$oldTextColor = _WinAPI_SetTextColor($hDC, 0xCD0091);文字颜色设置
_WinAPI_SetBkMode($hDC, $TRANSPARENT);文字背景透明
$hFont = _WinAPI_CreateFont(38, 0, 0, 0, $FW_NORMAL, 0, 0, 0, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $ANTIALIASED_QUALITY, $DEFAULT_PITCH, '黑体')
$oldFontObj = _WinAPI_SelectObject($hDC, $hFont);字体选择
_WinAPI_TextOut($hDC, 10, 10, 'TextOut方式写字符串');10,10处输出文字
_WinAPI_SelectObject($hDC, $oldFontObj);改回原来默认字体
_WinAPI_DeleteObject($hFont);释放字体对象
_WinAPI_SetTextColor($hDC, $oldTextColor);改回原来默认文字颜色
;写字方式二
$tRECT = _WinAPI_CreateRect(10, 60, 300, 84);文字区域左上右下
_WinAPI_SetBkMode($hDC, $OPAQUE);设置文字背景不透明
_WinAPI_SetBkColor($hDC, 0x0000FF);背景颜色
_WinAPI_DrawText($hDC, 'DrawText方式写字符串', $tRECT, $DT_LEFT);在上述区域左对齐输出文字
;绘制椭圆
$tRECT = _WinAPI_CreateRect(10, 100, 300, 184);椭圆区域
$hBrush = _WinAPI_CreateBrushIndirect($BS_HATCHED, 0x00A820, $HS_DIAGCROSS);椭圆填充画刷
$hObj = _WinAPI_SelectObject($hDC, $hBrush);画刷选择
_WinAPI_Ellipse($hDC, $tRECT);在矩形区域绘制椭圆,边框用默认画笔默认颜色,用上述画刷填充
_WinAPI_SelectObject($hDC, $hObj);改回来原来默认填充画刷
_WinAPI_DeleteObject($hBrush);释放画刷对象
;释放DC
_WinAPI_ReleaseDC($hPic, $hDC)
EndFunc ;==>GDIDraw
Func GDIDraw2()
$hDC = _WinAPI_GetDC($hPic)
$hMemDC = _WinAPI_CreateCompatibleDC($hDC);建立兼容DC,即内存中的DC
$hBitmap = _WinAPI_CreateCompatibleBitmapEx($hDC, 400, 200, _WinAPI_SwitchColor(_WinAPI_GetSysColor($COLOR_3DFACE)));创建hBitmap
$hSv = _WinAPI_SelectObject($hMemDC, $hBitmap);调入DC作图对象,即把图形画在$hBitmap上
;写字符串的方式一
$oldTextColor = _WinAPI_SetTextColor($hMemDC, 0xCD0091)
_WinAPI_SetBkMode($hMemDC, $TRANSPARENT)
$hFont = _WinAPI_CreateFont(38, 0, 0, 0, $FW_NORMAL, 0, 0, 0, $DEFAULT_CHARSET, $OUT_DEFAULT_PRECIS, $CLIP_DEFAULT_PRECIS, $ANTIALIASED_QUALITY, $DEFAULT_PITCH, '黑体')
$oldFontObj = _WinAPI_SelectObject($hMemDC, $hFont)
_WinAPI_TextOut($hMemDC, 10, 10, 'TextOut方式写字符串')
_WinAPI_SelectObject($hMemDC, $oldFontObj)
_WinAPI_DeleteObject($hFont)
_WinAPI_SetTextColor($hMemDC, $oldTextColor)
;写字方式二
$tRECT = _WinAPI_CreateRect(10, 60, 300, 84)
_WinAPI_SetBkMode($hMemDC, $OPAQUE)
_WinAPI_SetBkColor($hMemDC, 0x0000FF)
_WinAPI_DrawText($hMemDC, 'DrawText方式写字符串', $tRECT, $DT_LEFT)
;绘制椭圆
$tRECT = _WinAPI_CreateRect(10, 100, 300, 184)
$hBrush = _WinAPI_CreateBrushIndirect($BS_HATCHED, 0x00A820, $HS_DIAGCROSS)
$hObj = _WinAPI_SelectObject($hMemDC, $hBrush)
_WinAPI_Ellipse($hMemDC, $tRECT)
_WinAPI_SelectObject($hMemDC, $hObj)
_WinAPI_DeleteObject($hBrush)
;把内存DC中的内容拷贝到我们的pic对象DC中
;这二句注释了,设置hBitmap到pic控件别注释也能运行,而且最小化了图片还在
;~ _WinAPI_BitBlt($hDC, 0, 0, 400, 200, $hMemDC, 0, 0, $SRCCOPY)
;~ _WinAPI_DeleteObject($hBitmap)
;释放创建的对象及DC
_WinAPI_SelectObject($hMemDC, $hSv)
_WinAPI_DeleteDC($hMemDC)
_WinAPI_ReleaseDC($hPic, $hDC)
;设置hBitmap到pic控件
$oldObj = _SendMessage($hPic, 0x0172, 0, $hBitmap) ;$STM_SETIMAGE = 0x0172
_WinAPI_DeleteObject($oldObj)
Local $hObj = _SendMessage($hPic, 0x0173) ;$STM_GETIMAGE = 0x0173
If $hObj <> $hBitmap Then
_WinAPI_DeleteObject($hBitmap)
EndIf
EndFunc ;==>GDIDraw2
Func GDIPlusDraw()
_GDIPlus_Startup()
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hPic)
_GDIPlus_GraphicsClear($hGraphic, 0xFFCCCCCC);清空图形
;设置平滑
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
_GDIPlus_GraphicsSetInterpolationMode($hGraphic, 7)
;drawstring方式写字
;~ _GDIPlus_GraphicsDrawString($hGraphic, '黑体', 10, 10,'黑体');一时没弄清,这怎么不能出效果了
$hBrush = _GDIPlus_BrushCreateSolid(0xFF9100CD)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("黑体")
$hFont = _GDIPlus_FontCreate($hFamily, 24, 0)
$tLayout = _GDIPlus_RectFCreate(10, 10, 0, 0)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, 'DrawString方式写字符串', $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDrawStringEx($hGraphic, 'DrawString方式写字符串', $hFont, $aInfo[0], $hFormat, $hBrush)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
;文字转化为路径方式写字
$hFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_StringFormatSetAlign($hFormat, 0);0为左对齐
$hFamily = _GDIPlus_FontFamilyCreate('Arial')
$tLayout = _GDIPlus_RectFCreate(0, 50, 390, 30)
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, 'Path方式写字符串', $tLayout, $hFamily, 1, 38, $hFormat)
$hBrush = _GDIPlus_LineBrushCreateFromRect($tLayout, 0xFF02B9EE, 0xFF004696, 1, 3)
$hPen = _GDIPlus_PenCreate(0xFF263E57, 2, 0)
_GDIPlus_GraphicsFillPath($hGraphic, $hPath, $hBrush)
_GDIPlus_GraphicsDrawPath($hGraphic, $hPath, $hPen)
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
;绘制椭圆
$tLayout = _GDIPlus_RectFCreate(10, 100, 290, 84)
$hBrush = _GDIPlus_LineBrushCreateFromRect($tLayout, 0x0002B9EE, 0xFF004696, 2, 3)
$hPen = _GDIPlus_PenCreate(0xFFFFDD00, 1, 0)
_GDIPlus_GraphicsDrawEllipse($hGraphic, 10, 100, 290, 84, $hPen)
_GDIPlus_GraphicsFillEllipse($hGraphic, 10, 100, 290, 84, $hBrush)
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush)
;释放对象
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_Shutdown()
EndFunc ;==>GDIPlusDraw
Func GDIPlusDraw2()
_GDIPlus_Startup()
Local $hBitmap = _WinAPI_CreateBitmap(400, 200, 1, 32);建立背景位图
Local $hCDC = _WinAPI_CreateCompatibleDC(0);建立绘图DC
Local $hOld = _WinAPI_SelectObject($hCDC, $hBitmap);调入图片到DC
Local $hGraphic = _GDIPlus_GraphicsCreateFromHDC($hCDC);从DC创建图形对象
_GDIPlus_GraphicsClear($hGraphic, 0xFFCCCCCC);清空图形
;设置平滑
_GDIPlus_GraphicsSetSmoothingMode($hGraphic, 2)
_GDIPlus_GraphicsSetInterpolationMode($hGraphic, 7)
;drawstring方式写字
;~ _GDIPlus_GraphicsDrawString($hGraphic, '黑体', 10, 10,'黑体');一时没弄清,这怎么不能出效果了
$hBrush = _GDIPlus_BrushCreateSolid(0xFF9100CD)
$hFormat = _GDIPlus_StringFormatCreate()
$hFamily = _GDIPlus_FontFamilyCreate("黑体")
$hFont = _GDIPlus_FontCreate($hFamily, 24, 0)
$tLayout = _GDIPlus_RectFCreate(10, 10, 0, 0)
$aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, 'DrawString方式写字符串', $hFont, $tLayout, $hFormat)
_GDIPlus_GraphicsDrawStringEx($hGraphic, 'DrawString方式写字符串', $hFont, $aInfo[0], $hFormat, $hBrush)
_GDIPlus_FontDispose($hFont)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_BrushDispose($hBrush)
;文字转化为路径方式写字
$hFormat = _GDIPlus_StringFormatCreate()
_GDIPlus_StringFormatSetAlign($hFormat, 0);0为左对齐
$hFamily = _GDIPlus_FontFamilyCreate('Arial')
$tLayout = _GDIPlus_RectFCreate(0, 50, 390, 30)
$hPath = _GDIPlus_PathCreate()
_GDIPlus_PathAddString($hPath, 'Path方式写字符串', $tLayout, $hFamily, 1, 38, $hFormat)
$hBrush = _GDIPlus_LineBrushCreateFromRect($tLayout, 0xFF02B9EE, 0xFF004696, 1, 3)
$hPen = _GDIPlus_PenCreate(0xFF263E57, 2, 0)
_GDIPlus_GraphicsFillPath($hGraphic, $hPath, $hBrush)
_GDIPlus_GraphicsDrawPath($hGraphic, $hPath, $hPen)
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush)
_GDIPlus_PathDispose($hPath)
_GDIPlus_FontFamilyDispose($hFamily)
_GDIPlus_StringFormatDispose($hFormat)
;绘制椭圆
$tLayout = _GDIPlus_RectFCreate(10, 100, 290, 84)
$hBrush = _GDIPlus_LineBrushCreateFromRect($tLayout, 0x0002B9EE, 0xFF004696, 2, 3)
$hPen = _GDIPlus_PenCreate(0xFFFFDD00, 1, 0)
_GDIPlus_GraphicsDrawEllipse($hGraphic, 10, 100, 290, 84, $hPen)
_GDIPlus_GraphicsFillEllipse($hGraphic, 10, 100, 290, 84, $hBrush)
_GDIPlus_PenDispose($hPen)
_GDIPlus_BrushDispose($hBrush)
;释放对象
_GDIPlus_GraphicsDispose($hGraphic)
_WinAPI_SelectObject($hCDC, $hOld)
_WinAPI_DeleteDC($hCDC)
_GDIPlus_Shutdown()
;设置hBitmap到pic控件
$oldObj = _SendMessage($hPic, 0x0172, 0, $hBitmap) ;$STM_SETIMAGE = 0x0172
_WinAPI_DeleteObject($oldObj)
Local $hObj = _SendMessage($hPic, 0x0173) ;$STM_GETIMAGE = 0x0173
If $hObj <> $hBitmap Then
_WinAPI_DeleteObject($hBitmap)
EndIf
EndFunc ;==>GDIPlusDraw2
|