GDIP绘制桌面悬浮logo示例

一个简单的示例(依赖gdip_all.ahk基础库),对于新手来说可以参考一下基础用法。

鼠标滚轮调节logo大小、鼠标右键呼出菜单列表

#Persistent
#Include gdip_all.ahk
OnMessage(0x201,"WM_LBUTTONDOWN")

;图片路径
ImagePath:=A_ScriptDir "\bagel.png"
;缩放比例
logosize:=50
xpos:=200,ypos:=100
;载入图片
load_logo_box(ImagePath,logosize,xpos,ypos)
;==================鼠标滚轮调节logo大小========================
IsModifyLogoSize:= Func("IsExistLogo")
Hotkey,if, % IsModifyLogoSize
Hotkey $WheelDown, ModifyLogoDownSize
Hotkey $WheelUp, ModifyLogoUpSize
;==================实时监测logo坐标位置========================
SetTimer,GetLogoClientPos,On
Return
;==========================================

GetLogoClientPos:
	if WinExist("ahk_id " hlogo){
		VarSetCapacity( size, 16, 0 )
		DllCall( "GetClientRect", "Ptr", hlogo, "Ptr", &size )
		DllCall( "ClientToScreen", "Ptr", hlogo, "Ptr", &size )
		xpos := NumGet( size, 0, "Int"), ypos := NumGet( size, 4, "Int")
	}
Return
ModifyLogoDownSize:
	logosize--
	load_logo_box(ImagePath,logosize,xpos,ypos)
Return

ModifyLogoUpSize:
	logosize++
	load_logo_box(ImagePath,logosize,xpos,ypos)
Return

IsExistLogo(){
	Global hlogo
	CoordMode,Mouse,Screen
	MouseGetPos,,,MID

	Return (MID&&MID=hlogo?True:False)
}

load_logo_box(ImagePath,logosize:=50,xpos:=100,ypos:=100,logoAlpha:=255){
	Global hlogo
	if !FileExist(ImagePath){
		MsgBox,16,Error,图片文件不存在!
		Return
	}
	pToken := Gdip_Startup()
	pBitmap := Gdip_CreateBitmapFromFile(ImagePath)
	if !pBitmap {
		MsgBox,16,Error,图片载入失败!
		Return
	}
	Gui, logo: -Caption +E0x80000 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs +Hwndhlogo
	Gui, logo: Show, NA
	Ratio:=(logo_Width:=Gdip_GetImageWidth(pBitmap))/(logo_Height:=Gdip_GetImageHeight(pBitmap))
	, DisplayWidth:=Round(logo_Width*logosize/100), DisplayHeight:=DisplayWidth//Ratio
	pBitmap:=Gdip_ResizeBitmap(pBitmap, DisplayWidth, DisplayHeight, 0)
	hbm := CreateDIBSection(DisplayWidth, DisplayHeight),hdc := CreateCompatibleDC()
	obm := SelectObject(hdc, hbm),G := Gdip_GraphicsFromHDC(hdc)
	Gdip_SetInterpolationMode(G, 7),Gdip_DrawImageFast(G, pBitmap, 0, 0)
	UpdateLayeredWindow(hlogo, hdc,xpos , ypos, DisplayWidth, DisplayHeight,logoAlpha>0&&logoAlpha<=255?logoAlpha:255)
	SelectObject(hdc, obh), DeleteObject(hbm), DeleteDC(hdc),Gdip_DeleteGraphics(G),Gdip_DisposeImage(pBitmap)
	OnMessage(0x20, Func("WM_SETCURSOR").Bind(hlogo))

}

logoGuiContextMenu(GuiHwnd, CtrlHwnd, EventInfo, IsRightClick, X, Y){
	if (IsRightClick){
		if MenuGetHandle("logo")
			Menu, logo, DeleteAll
		Menu, logo, UseErrorLevel
		Menu, logo,Add, 菜单项1,MenuItemLabel
		Menu, logo,Add
		Menu, logo,Add, 菜单项2,MenuItemLabel
		Menu, logo,Add
		Menu, logo,Add, 菜单项3,MenuItemLabel
		Menu, logo,Add
		Menu, logo,Add, 菜单项4,MenuItemLabel
		Menu, logo,Show
		Return
	}

	MenuItemLabel:
		Switch A_ThisMenuItem
		{
			Case "菜单项1":
				MsgBox % A_ThisMenuItemPos "-" A_ThisMenuItem
			Case "菜单项2":
				MsgBox % A_ThisMenuItemPos "-" A_ThisMenuItem
			Case "菜单项3":
				MsgBox % A_ThisMenuItemPos "-" A_ThisMenuItem
			Case "菜单项4":
				MsgBox % A_ThisMenuItemPos "-" A_ThisMenuItem
		}
	Return
}


WM_LBUTTONDOWN(wParam, lParam, uMsg, hWnd){
	if (A_Gui="logo"&&HWND)
		DllCall("user32.dll\PostMessage", "ptr", hWnd, "uint", 0x00A1, "ptr", 2, "ptr", 0)
}

WM_SETCURSOR(hPic, wp) {
	static hCursor, flags := (LR_DEFAULTSIZE := 0x40) | (LR_SHARED := 0x8000)
		, params := [ "Ptr", 0, "UInt", OCR_HAND := 32649
			, "UInt", IMAGE_CURSOR := 2
			, "Int", 0, "Int", 0, "UInt", flags, "Ptr" ]
	(!hCursor && hCursor := DllCall("LoadImage", params*))
	if (wp = hPic)
		Return DllCall("SetCursor", "Ptr", hCursor)
}

给TA捐赠
共{{data.count}}人
人已捐赠
AHKV1教程案例

加载外部字体文件的几种方式整理[留档]

2022-7-16 10:49:38

AHKV2教程案例

AHK Leetcode系列 61-70

2022-7-16 14:34:38

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
有新私信 私信列表
搜索