绿色风's Blog
专注AutoIT(Au3)
  • 首页
  • 流●年
  • 笔●记
    • 学习随记
    • 源码示例
  • 脚●本
    • UDF(收集)
    • 工作室UDF
    • 工具●教程
    • 教程之GDI
  • 作●品
  • 下●载
  • 情怀ExcelTip
2月142015

[教程]第十七讲 GDI+混合模式透明度

作者:绿色风   发布:2015-2-14 6:10 Saturday   分类:   阅读:16384次   评论:0条  

前两讲,讲了图像的相关内容,图像相关还有很多,比如多帧图像,这在我以前的GIF相关源码中已经有例子了,对于TIFF的多帧图像我没有TIFF图像,所以也不懂
对于图像方面还有很多,我也不清楚,用到再研究了
本讲,我想讲一下GDI+特有的 ALPHA通道(透明度)
GDI+的颜色是ARGB格式,A就是指alpha透明度,0为全透明,255指不透明
那是否设置了就是透明了呢?透明到哪层呢?
_GDIPlus_GraphicsSetCompositingMode($hGraphics, $iCompositionMode)
$iCompositionMode = 0,指合成模式--指同一个画布上,同一个地方,上层的能通过通明度看到下一层
$iCompositionMode = 1,指覆盖模式--指同一个画布上,同一个地方,上层直接穿过下层,相当于在这个位置没有下层的内容

这个还是用例子来看效果吧

NewImage.jpg

#include <APIConstants.au3>
#include <WinAPIEx.au3>
#include <GDIPlus.au3>
#include <GDIPlusEx.au3>
#include <GuiSlider.au3>
Global $alphaOfBack = 0, $alphaOfRed = 0x80, $alphaOfGreen = 0x80
GUICreate("第十七讲 GDI+混合模式透明度", 300, 300)
$nCtrlId = GUICtrlCreatePic("", 0, 0, 300, 200)
$hPicWnd = GUICtrlGetHandle($nCtrlId)

Global $rect[3][2]
init()
GUIRegisterMsg($WM_HSCROLL, "onHSCROLL")
GUISetState()

update()

While 1
        $Msg = GUIGetMsg()
        Switch $Msg
                Case -3
                        ExitLoop

        EndSwitch
WEnd

GUIDelete()
Exit

Func init()
        GUICtrlCreateLabel("黄色透明度", 5, 205)
        $rect[0][0] = GUICtrlCreateSlider(65, 205, 100)
        GUICtrlSetLimit(-1, 0xFF, 0)
        GUICtrlSetData(-1, $alphaOfBack)
        _GUICtrlSlider_SetTicFreq(-1, 16)
        $rect[0][1] = GUICtrlCreateLabel("", 175, 205, 40, 25)
        GUICtrlSetData(-1, $alphaOfBack)

        GUICtrlCreateLabel("红色透明度", 5, 235)
        $rect[1][0] = GUICtrlCreateSlider(65, 235, 100)
        GUICtrlSetLimit(-1, 0xFF, 0)
        GUICtrlSetData(-1, $alphaOfRed)
        _GUICtrlSlider_SetTicFreq(-1, 16)
        $rect[1][1] = GUICtrlCreateLabel("", 175, 235, 40, 25)
        GUICtrlSetData(-1, $alphaOfRed)

        GUICtrlCreateLabel("绿色透明度", 5, 265)
        $rect[2][0] = GUICtrlCreateSlider(65, 265, 100)
        GUICtrlSetLimit(-1, 0xFF, 0)
        GUICtrlSetData(-1, $alphaOfGreen)
        _GUICtrlSlider_SetTicFreq(-1, 16)
        $rect[2][1] = GUICtrlCreateLabel("", 175, 265, 40, 25)
        GUICtrlSetData(-1, $alphaOfGreen)
EndFunc   ;==>init

Func onHSCROLL($hWnd, $iMsg, $wParam, $lParam)
        Switch _WinAPI_GetDlgCtrlID($lParam)
                Case $rect[0][0]
                        $alphaOfBack = GUICtrlRead($rect[0][0])
                        GUICtrlSetData($rect[0][1], $alphaOfBack)
                Case $rect[1][0]
                        $alphaOfRed = GUICtrlRead($rect[1][0])
                        GUICtrlSetData($rect[1][1], $alphaOfRed)
                Case $rect[2][0]
                        $alphaOfGreen = GUICtrlRead($rect[2][0])
                        GUICtrlSetData($rect[2][1], $alphaOfGreen)
        EndSwitch
        update()
EndFunc   ;==>onHSCROLL

Func update()
        Local $HWND_CX = _WinAPI_GetWindowWidth($hPicWnd)
        Local $HWND_CY = _WinAPI_GetWindowHeight($hPicWnd)
        _GDIPlus_Startup()
        Local $hGraphics = _GDIPlus_GraphicsCreateFromHWND($hPicWnd)
        Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics($HWND_CX, $HWND_CY, $hGraphics)
        Local $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
        _GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)
        DoubleDraw($hBackbuffer)
        _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, $HWND_CX, $HWND_CY)
        _GDIPlus_BitmapDispose($hBitmap)
        _GDIPlus_GraphicsDispose($hBackbuffer)
        _GDIPlus_GraphicsDispose($hGraphics)
        _GDIPlus_Shutdown()
EndFunc   ;==>update

Func DoubleDraw($hGraphics)
        Local $hBitmap
        BackColor($hGraphics);绘制背景竖条纹

;~ ;========================生成混合模式为合成的图片,并与背景竖条纹分别用合成和覆盖方式,在左侧显示===================================================
        $hBitmap = myImage($hGraphics, 0)
        _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 100, 80)
        _GraphicsDrawString($hGraphics, "图片合成背景", 5, 80)
        _GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 100)

        _GDIPlus_GraphicsSetCompositingMode($hGraphics, 1)
        _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 100, 80)
        _GraphicsDrawString($hGraphics, "图片覆盖背景", 5, 80)
        _GDIPlus_BitmapDispose($hBitmap)
;~ ;========================左侧显示结束===================================================

;~ ;========================生成混合模式为覆盖的图片,并与背景竖条纹分别用合成和覆盖方式,在右侧显示===================================================
        $hBitmap = myImage($hGraphics, 1)
        _GDIPlus_GraphicsResetTransform($hGraphics)
        _GDIPlus_GraphicsTranslateTransform($hGraphics, 150, 0)
        _GDIPlus_GraphicsSetCompositingMode($hGraphics, 0)
        _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 100, 80)
        _GraphicsDrawString($hGraphics, "图片合成背景", 5, 80)

        _GDIPlus_GraphicsTranslateTransform($hGraphics, 0, 100)
        _GDIPlus_GraphicsSetCompositingMode($hGraphics, 1)
        _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, 100, 80)
        _GraphicsDrawString($hGraphics, "图片覆盖背景", 5, 80)
        _GDIPlus_BitmapDispose($hBitmap)
;~ ;========================右侧显示结束===================================================
EndFunc   ;==>DoubleDraw

Func myImage($hGraphics, $i)
        Local $hBitmap = _GDIPlus_BitmapCreateFromGraphics(100, 80, $hGraphics)
        Local $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
        _GDIPlus_GraphicsSetSmoothingMode($hBackbuffer, 2)
        _GDIPlus_GraphicsClear($hBackbuffer, BitOR(BitShift($alphaOfBack, -24), 0xFFFF00))
        Local $hBrushback = _GDIPlus_BrushCreateSolid(BitOR(BitShift($alphaOfGreen, -24), 0x00FF00))
        Local $hBrushtop = _GDIPlus_BrushCreateSolid(BitOR(BitShift($alphaOfRed, -24), 0xFF0000))
        _GDIPlus_GraphicsSetCompositingMode($hBackbuffer, $i)
        _GDIPlus_GraphicsFillEllipse($hBackbuffer, 0, 0, 100, 80, $hBrushback)
        _GDIPlus_GraphicsFillEllipse($hBackbuffer, 20, 10, 50, 60, $hBrushtop)
        If $i = 0 Then
                _GraphicsDrawString($hBackbuffer, "图片合成模式", 5, 40)
        Else
                _GraphicsDrawString($hBackbuffer, "图片覆盖模式", 5, 40)
        EndIf
        ;释放画笔
        _GDIPlus_BrushDispose($hBrushback)
        _GDIPlus_BrushDispose($hBrushtop)
        _GDIPlus_GraphicsDispose($hBackbuffer)
        Return $hBitmap
EndFunc   ;==>myImage

Func BackColor($hGraphics)
        Local $hPen = _GDIPlus_PenCreate(0xFF000000)
        Local $r, $g, $b, $color
        For $i = 0 To 300
                $r = Random(0, 255)
                $g = Random(0, 255)
                $b = Random(0, 255)
                $color = BitOR(0xFF000000, _;Alpha
                                BitShift($r, -16), _;Red
                                BitShift($g, -8), _;Green
                                $b);Blue
                _GDIPlus_PenSetColor($hPen, $color)
                _GDIPlus_GraphicsDrawLine($hGraphics, $i, 0, $i, 200, $hPen)
        Next
        ;释放画笔
        _GDIPlus_PenDispose($hPen)
EndFunc   ;==>BackColor

Func _GraphicsDrawString($hGraphics, $sString, $nX, $nY, $hBrush = 0, $sFont = "Arial", $nSize = 10, $iFormat = 0)
        Local $hFormat = _GDIPlus_StringFormatCreate($iFormat)
        Local $hFamily = _GDIPlus_FontFamilyCreate($sFont)
        Local $hFont = _GDIPlus_FontCreate($hFamily, $nSize)
        Local $tLayout = _GDIPlus_RectFCreate($nX, $nY, 0, 0)
        Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphics, $sString, $hFont, $tLayout, $hFormat)
        __GDIPlus_BrushDefCreate($hBrush)
        Local $aResult = _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $aInfo[0], $hFormat, $hBrush)
        Local $iError = @error
        __GDIPlus_BrushDefDispose()
        _GDIPlus_FontDispose($hFont)
        _GDIPlus_FontFamilyDispose($hFamily)
        _GDIPlus_StringFormatDispose($hFormat)
        Return SetError($iError, 0, $aResult)
EndFunc   ;==>_GraphicsDrawString```

 

 





本文固定链接: http://www.jianyiit.com/post-72.html

blogger
该日志由 绿色风 于2015-2-14 6:10 Saturday发表在 分类下。
版权所有:《绿色风's Blog》 → 《[教程]第十七讲 GDI+混合模式透明度》;
除特别标注,本博客很多文章均为原创. 互联分享,尊重版权,转载请以链接形式标明本文地址;
本文标签:

扫描二维码,在手机上阅读
上一篇::[教程]第十八讲 GDI+窗口自绘无控件
下一篇:[教程] 第十六讲 GDI+图像之图像属性项读取EXIF信息及缩略图读取

热门文章

相关文章

  • [教程]第十二讲 GDI+路径
  • [教程]第十三讲 GDI+区域及剪切区域
  • [教程] 第十六讲 GDI+图像之图像属性项读取EXIF信息及缩略图读取
  • [教程]第二讲 GDI文字DrawText
  • [教程]第十四讲 GDI+图像之图元文件
取消回复

发表评论

亲,头像对么?

60 + 73 =

提交中,请稍候……


木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!


    站点统计
    • 运行时间: 20254 天
    • 日志总数: 365 篇
    • 评论数量: 7238 条
    • 微语数量: 6 条
    • 附件总量: 388 件
  • 逝出的青春

  • 打赏"绿色风"



      扫码关注本站公众号 可搜本站内容

  • Autoit V3 脚本交流群

      常驻群1:905774875
      常驻群2:40672266


  • 链接

    • AU3中文论坛
    • Excel资料库
    • 完美者博客
    • 顺网小哥'S Blog
    • 猛牛哥的博客
    • 网吧系统下载
  • 分类

    • 流●年(66)
    • 笔●记(0)
    • 脚●本(0)
    • 作品(21)
    • 学习随记(51)
    • 源码示例(68)
    • UDF(收集)(26)
    • 工作室UDF(30)
    • 工具●教程(62)
    • 教程之GDI(24)
Copyright © 2013 绿色风's Blog. Powered by emlog. Theme by 射雕天龙. 鄂ICP备2021011689号-1 鄂公网安备42102302000078号 sitemap