tryhi 发表于 2010-11-9 16:08:13

如何在屏幕中显示一张PNG透明图片【GDI相关】

本帖最后由 tryhi 于 2010-11-9 16:26 编辑

有点类似于此贴http://www.autoitx.com/forum.php?mod=viewthread&tid=13262&highlight=png,看不到窗口出现,仅仅在在屏幕上显示一张图片,此贴的源码看了几天仍无所获,源码实在太长不懂筛选。#include <GDIPlus.au3>
#include <WinAPI.au3>
_GDIPlus_Startup()
$NormalImage = _GDIPlus_ImageLoadFromFile("normal.png");新建图像文件的句柄
$GC = _GDIPlus_ImageGetGraphicsContext($NormalImage)
$newBmp = _GDIPlus_BitmapCreateFromGraphics(118, 53, $GC)
$newGC = _GDIPlus_ImageGetGraphicsContext($newBmp)
_GDIPlus_GraphicsDrawImageRect($newGC, $NormalImage, 0, 0, 118, 53)
While 1
        Sleep(100)
WEnd为何以上代码不会显示出normal.png图片呢

马甲 发表于 2010-11-9 16:08:14

#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>
Global Const $AC_SRC_ALPHA = 1
_GDIPlus_Startup()
$hImage = _GDIPlus_ImageLoadFromFile("normal.png")
$width = _GDIPlus_ImageGetWidth($hImage)
$height = _GDIPlus_ImageGetHeight($hImage)
$gui = GUICreate("", $width, $height, -1, -1, $WS_POPUP, $WS_EX_LAYERED, GUICreate(""))
SetBitmap($gui, $hImage, 255)
GUISetState()
WinSetOnTop($gui, "", 1)
Sleep(10000)

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", $AC_SRC_ALPHA)
        _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   ;==>SetBitmap

afan 发表于 2010-11-10 00:40:25

看看这贴,应该自己能搞定 http://www.autoitx.com/forum.php?mod=viewthread&tid=13262
简单的说,就是先
_GDIPlus_Startup()
然后 GUICreate("png gui", $width, $height, -1, -1, $WS_POPUP, $WS_EX_LAYERED) ;创建一个透明的窗口
然后用上贴中包含的一个函数 SetBitmap() 加载png图片即可

gapkiller 发表于 2010-11-10 10:49:12

本帖最后由 gapkiller 于 2010-11-10 11:12 编辑


#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1

; Create GUI
$hGUI = GUICreate("Show PNG", 250, 250)

; Load PNG image
_GDIPlus_StartUp()
$hImage   = _GDIPlus_ImageLoadFromFile("normal.png")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

; Loop until user exits
do
until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_ShutDown()

; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc

3mile 发表于 2010-11-10 13:09:51

借用楼上的代码表述下,但鼠标穿透是个大问题。
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GDIPlus.au3>
#Include <WinAPI.au3>

Global $hGUI, $hImage, $hGraphic, $hImage1

; Create GUI
$hGUI = GUICreate("Show PNG", 250, 250,100,200,$WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TOPMOST))
GUIctrlSetBkColor(-1,0xABCDEF)
; Load PNG image
_GDIPlus_StartUp()
$hImage   = _GDIPlus_ImageLoadFromFile("normal.png")
$hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)

_WinAPI_SetLayeredWindowAttributes($hGUI, 0xABCDEF, 200)
GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
GUISetState()

; Loop until user exits
do
       
until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources
_GDIPlus_GraphicsDispose($hGraphic)
_GDIPlus_ImageDispose($hImage)
_GDIPlus_ShutDown()

; Draw PNG image
Func MY_WM_PAINT($hWnd, $Msg, $wParam, $lParam)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0)
    _WinAPI_RedrawWindow($hGUI, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc
页: [1]
查看完整版本: 如何在屏幕中显示一张PNG透明图片【GDI相关】