GDI+在透明窗口上创建半透明图形出现杂色[已解决]
本帖最后由 xyold1 于 2015-10-10 20:12 编辑我要在透明的子窗体上作图,显示一些信息,而在画半透明图的时候出现了一些问题
#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
_Main()
Func _Main()
Local $hGUI, $hGraphic, $hPen,$hGUI2
; Create GUI
$hGUI = GUICreate("GDI+", 400, 300)
GUISetState()
$hGUI2 = GUICreate("透明", 280, 220, 0,0, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hGUI)
GUISetBkColor(0x00ff00)
_API_SetLayeredWindowAttributes($hGUI2, 0x00ff00)
GUISetState()
; Draw line
_GDIPlus_Startup ()
;~ $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI)
$hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hGUI2)
$hPen = _GDIPlus_PenCreate (0x55ff0000,10)
_GDIPlus_GraphicsDrawLine ($hGraphic, 10, 150, 390, 150, $hPen)
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
; Clean up resources
_GDIPlus_PenDispose ($hPen)
_GDIPlus_GraphicsDispose ($hGraphic)
_GDIPlus_Shutdown ()
EndFunc ;==>_Main
Func _API_SetLayeredWindowAttributes($hwnd, $i_transcolor, $Transparency = 255, $isColorRef = False)
Local Const $AC_SRC_ALPHA = 1
Local Const $ULW_ALPHA = 2
Local Const $LWA_ALPHA = 0x2
Local Const $LWA_COLORKEY = 0x1
If Not $isColorRef Then
$i_transcolor = Hex(String($i_transcolor), 6)
$i_transcolor = Execute('0x00' & StringMid($i_transcolor, 5, 2) & StringMid($i_transcolor, 3, 2) & StringMid($i_transcolor, 1, 2))
EndIf
Local $Ret = DllCall("user32.dll", "int", "SetLayeredWindowAttributes", "hwnd", $hwnd, "long", $i_transcolor, "byte", $Transparency, "long", $LWA_COLORKEY + $LWA_ALPHA)
Select
Case @error
Return SetError(@error, 0, 0)
Case $Ret = 0
Return SetError(4, 0, 0)
Case Else
Return 1
EndSelect
EndFunc ;==>_API_SetLayeredWindowAttributes
上面的代码$hGUI2是屏蔽了00ff00颜色的透明窗口,用红色半透明的画笔DrawLine,线不是红色的
如下图
不是下面的半透明红色
那么如何让半透明的颜色正确显示呢
是在别的控件上画图
还是更改窗体透明的方式呢 等高手。。。。。。。。。。。。。 人少了 今时不同往日 要用_WinAPI_UpdateLayeredWindowEx,而不是_API_SetLayeredWindowAttributes 回复 4# seniors
Gdi+的大神呀
可能 是我AU3的版本低吧,没找到_WinAPI_UpdateLayeredWindowEx 只有_WinAPI_UpdateLayeredWindow
本来不是计算机的,对我而言 这个函数的参数是相当复杂,下面这个例子加载一个png图片就行了,可怎么让用Gdi自绘的图形显示呢
有说WS_EX_LAYERED属性是不可以设置给子窗口的。_WinAPI_UpdateLayeredWindow 是不是也不能在子窗口上做图呢Func SetBitmap($hGUI, $hImage, $iOpacity)
Local $hScrDC, $hMemDC, $hBitmap, $hOld, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
$hScrDC = _WinAPI_GetDC(0)
$hMemDC = _WinAPI_CreateCompatibleDC($hScrDC)
$hBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hImage)
$hOld = _WinAPI_SelectObject($hMemDC, $hBitmap)
$tSize = DllStructCreate($tagSIZE)
$pSize = DllStructGetPtr($tSize)
DllStructSetData($tSize, "X", _GDIPlus_ImageGetWidth($hImage))
DllStructSetData($tSize, "Y", _GDIPlus_ImageGetHeight($hImage))
$tSource = DllStructCreate($tagPOINT)
$pSource = DllStructGetPtr($tSource)
$tBlend = DllStructCreate($tagBLENDFUNCTION)
$pBlend = DllStructGetPtr($tBlend)
DllStructSetData($tBlend, "Alpha", $iOpacity)
DllStructSetData($tBlend, "Format", 1)
_WinAPI_UpdateLayeredWindow($hGUI, $hScrDC, 0, $pSize, $hMemDC, $pSource, 0, $pBlend, $ULW_ALPHA)
_WinAPI_ReleaseDC(0, $hScrDC)
_WinAPI_SelectObject($hMemDC, $hOld)
_WinAPI_DeleteObject($hBitmap)
_WinAPI_DeleteDC($hMemDC)
EndFunc 本帖最后由 seniors 于 2015-10-9 15:15 编辑
反正我是能的,我用的版本3.3.7.15,可以用在子窗口,另外,55ff0000看上是粉红是因为和下面的窗口颜色混合后的效果。我改成99ff0000这样是半透明,颜色红些了
#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <GDIPlusEx.au3>
#include <WindowsConstants.au3>
Opt('MustDeclareVars', 1)
_Main()
Func _Main()
Local $hGUI, $hGraphic, $hPen,$hGUI2
; Create GUI
$hGUI = GUICreate("GDI+", 400, 300)
GUICtrlCreateLabel("放点字看看透明效果",10,140)
GUISetState()
$hGUI2 = GUICreate("透明", 280, 220, 0,0, $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_MDICHILD),$hGUI)
GUISetState()
; Draw line
_GDIPlus_Startup ()
Local $hBitmap = _WinAPI_CreateBitmap(280, 220, 1, 32)
Local $hCDC = _WinAPI_CreateCompatibleDC(0)
Local $hOld = _WinAPI_SelectObject($hCDC, $hBitmap)
Local $hGraphic = _GDIPlus_GraphicsCreateFromHDC($hCDC)
$hPen = _GDIPlus_PenCreate (0x99ff0000,10)
_GDIPlus_GraphicsDrawLine ($hGraphic, 10, 150, 390, 150, $hPen)
; Clean up resources
_GDIPlus_PenDispose ($hPen)
_GDIPlus_GraphicsDispose ($hGraphic)
_GDIPlus_Shutdown ()
_WinAPI_SelectObject($hCDC, $hOld)
_WinAPI_DeleteDC($hCDC)
_WinAPI_UpdateLayeredWindowEx($hGUI2, -1, -1, $hBitmap, 255, 1)
_WinAPI_DeleteObject($hBitmap)
_WinAPI_EmptyWorkingSet()
; Loop until user exits
Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE
EndFunc ;==>_Main
另外说明一下SetLayeredWindowAttributes后,窗口就不是分层窗口(WS_EX_LAYERED)了,也就是不具有透明属性了 回复 7# seniors
谢谢,seniors就是厉害,你发的好多教程由于版本的原因没学,更新au3版本定要多研究 稍微补充下
#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
;#include <GDIPlusEx.au3>
#include <WindowsConstants.au3>
#include <WinAPIGdi.au3>
#include <WinAPISys.au3>
#include <WinAPIProc.au3> 谢谢分享,楼主好心人
页:
[1]