Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires AutoHotkey v2.0
- ; --- Konfiguracja ---
- AffinityExe := "Affinity.exe"
- global BTN_HEIGHT := 25, PADDING := 5
- global FONT_FAMILY := "Segoe UI"
- ; --- Wczytywanie ustawień ---
- global IniFile := A_ScriptDir "\AffinityKeyboard_Settings.ini"
- global posX := IniRead(IniFile, "Position", "X", 200)
- global posY := IniRead(IniFile, "Position", "Y", 200)
- ; --- Inicjalizacja GDI+ ---
- global gdiplusToken := Gdip_Startup()
- if !gdiplusToken {
- MsgBox("Krytyczny błąd: Nie można uruchomić biblioteki GDI+! Skrypt zostanie zamknięty.")
- ExitApp
- }
- ; --- Definicje przycisków ---
- global buttons := []
- buttons_data := [
- {name:"New", color:"0xFF8BC34A", action:"^n"},
- {name:"Open", color:"0xFF03A9F4", action:"^o"},
- {name:"Copy", color:"0xFFFFC107", action:"^c"},
- {name:"Paste", color:"0xFFFFEB3B", action:"^v"},
- {name:"Undo", color:"0xFFFF7043", action:"^z"},
- {name:"Redo", color:"0xFFE91E63", action:"^+z"},
- {name:"Delete", color:"0xFF9E9E9E", action:"{Backspace}"},
- {name:"Invert", color:"0xFFC47DD0", action:"^i"},
- {name:"Fit", color:"0xFFADD8E6", action:"^0"},
- {name:"[", color:"0xFFEBEBEB", action:"["},
- {name:"]", color:"0xFFEBEBEB", action:"]"},
- {name:"Merge.S", color:"0xFF32FD53", action:"^+e"},
- {name:"Merge.V", color:"0xFF2BE6FF", action:"^!+e"},
- {name:"Duplicate", color:"0xFFFFA8F09", action:"^j"},
- {name:"Exp2JPG", color:"0xFFFFEB3B", action:"^!+w"}
- ]
- ; --- Obliczanie optymalnego rozmiaru czcionki i dynamicznej szerokości przycisków ---
- longest_name := ""
- for _, btn_data in buttons_data {
- if StrLen(btn_data.name) > StrLen(longest_name)
- longest_name := btn_data.name
- }
- ; Oblicz max rozmiar czcionki bazując na najdłuższym tekście i przykładowej szerokości (np. 100)
- global maxFontSize := CalculateMaxFontSize(FONT_FAMILY, longest_name, 100, BTN_HEIGHT, 8)
- x := PADDING
- for _, btn_data in buttons_data {
- textWidth := MeasureTextWidth(btn_data.name, maxFontSize, FONT_FAMILY)
- nameLength := StrLen(btn_data.name)
- ; *** KLUCZOWA POPRAWKA ***
- ; Oblicz padding jako średnią szerokość jednego znaku z tekstu na przycisku.
- charPadding := 0
- if (nameLength > 0) {
- charPadding := textWidth / nameLength
- } else {
- ; Zapasowy padding, gdyby nazwa była pusta (mało prawdopodobne)
- charPadding := MeasureTextWidth("M", maxFontSize, FONT_FAMILY)
- }
- btn_width := Round(textWidth + (2 * charPadding)) ; Szerokość tekstu + padding z obu stron
- btn_data.x := x, btn_data.y := PADDING, btn_data.w := btn_width, btn_data.h := BTN_HEIGHT
- buttons.Push(btn_data)
- x += btn_width + PADDING
- }
- buttons.Push({name: "X", color: "0xFFF44336", action: "ExitApp", x: x + 10, y: PADDING, w: 25, h: 25})
- gui_width := buttons.Get(buttons.Length).x + buttons.Get(buttons.Length).w + PADDING
- ; --- Tworzenie GUI ---
- MyGui := Gui("+AlwaysOnTop +ToolWindow -Caption +E0x2000000")
- MyGui.OnEvent("Close", GuiClose)
- global hoveredButtonIndex := 0 ; Zmienna do śledzenia najechanego przycisku
- ; --- Przechwytywanie komunikatów systemowych ---
- OnMessage(0xF, WM_PAINT)
- OnMessage(0x200, WM_MOUSEMOVE)
- OnMessage(0x201, WM_LBUTTONDOWN)
- MyGui.Show("x" posX " y" posY " w" gui_width " h" (BTN_HEIGHT + PADDING * 2) " NA")
- return
- ; === GŁÓWNE FUNKCJE ===
- WM_PAINT(wParam, lParam, msg, hwnd) {
- global MyGui, maxFontSize, FONT_FAMILY, hoveredButtonIndex
- if (hwnd != MyGui.Hwnd)
- return
- ps := Buffer(A_PtrSize * (A_PtrSize == 4 ? 9 : 11))
- hdc := DllCall("BeginPaint", "ptr", hwnd, "ptr", ps, "ptr")
- pGraphics := Gdip_GraphicsFromHDC(hdc)
- Gdip_SetSmoothingMode(pGraphics, 4)
- Gdip_Clear(pGraphics, 0xFF404040)
- pFamily := Gdip_FontFamilyCreate(FONT_FAMILY)
- pFont := Gdip_FontCreate(pFamily, maxFontSize, 1)
- pFormat := Gdip_StringFormatCreate()
- Gdip_StringFormatSetAlign(pFormat, 1)
- Gdip_StringFormatSetLineAlign(pFormat, 1)
- pBrushText := Gdip_BrushCreateSolid(0xFF000000)
- for i, btn in buttons {
- btnColor := btn.color
- if (i = hoveredButtonIndex) {
- ; Zmień kolor dla efektu hover - ustaw alpha na ~70% (0xB3)
- btnColor := (btn.color & 0x00FFFFFF) | 0xB3000000
- }
- pBrushBG := Gdip_BrushCreateSolid(btnColor)
- Gdip_FillRectangle(pGraphics, pBrushBG, btn.x, btn.y, btn.w, btn.h)
- Gdip_DrawString(pGraphics, btn.name, pFont, pFormat, pBrushText, btn.x, btn.y, btn.w, btn.h)
- Gdip_DeleteBrush(pBrushBG)
- }
- Gdip_DeleteFontFamily(pFamily)
- Gdip_DeleteFont(pFont)
- Gdip_DeleteBrush(pBrushText)
- Gdip_DeleteStringFormat(pFormat)
- Gdip_DeleteGraphics(pGraphics)
- DllCall("EndPaint", "ptr", hwnd, "ptr", ps)
- return 0
- }
- WM_MOUSEMOVE(wParam, lParam, msg, hwnd) {
- global MyGui, buttons, hoveredButtonIndex
- if (hwnd != MyGui.Hwnd)
- return
- x := lParam & 0xFFFF
- y := (lParam >> 16) & 0xFFFF
- currentlyHovered := 0
- for i, btn in buttons {
- if (x >= btn.x and x <= btn.x + btn.w and y >= btn.y and y <= btn.y + btn.h) {
- currentlyHovered := i
- break
- }
- }
- if (currentlyHovered != hoveredButtonIndex) {
- hoveredButtonIndex := currentlyHovered
- ; Wymuś przerysowanie okna, aby pokazać/ukryć efekt hover
- DllCall("RedrawWindow", "ptr", MyGui.Hwnd, "ptr", 0, "ptr", 0, "uint", 0x0001) ; RDW_INVALIDATE
- }
- }
- WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {
- global MyGui, buttons, IniFile, posX, posY
- if (hwnd != MyGui.Hwnd)
- return
- x := lParam & 0xFFFF
- y := (lParam >> 16) & 0xFFFF
- for _, btn in buttons {
- if (x >= btn.x and x <= btn.x + btn.w and y >= btn.y and y <= btn.y + btn.h) {
- if (btn.action = "ExitApp") {
- WinGetPos(&posX, &posY, , , MyGui.Hwnd)
- IniWrite(posX, IniFile, "Position", "X")
- IniWrite(posY, IniFile, "Position", "Y")
- MyGui.Destroy()
- } else {
- SendToAffinity(btn.action)
- }
- return
- }
- }
- PostMessage(0xA1, 2, , , "A")
- }
- SendToAffinity(shortcut) {
- global AffinityExe
- if WinExist("ahk_exe " AffinityExe) {
- WinActivate("ahk_exe " AffinityExe)
- Send(shortcut)
- } else {
- MsgBox("Nie można znaleźć uruchomionego procesu " AffinityExe)
- }
- }
- GuiClose(*) {
- global gdiplusToken
- Gdip_Shutdown(gdiplusToken)
- ExitApp
- }
- MeasureTextWidth(text, fontSize, fontFamily) {
- hdc := DllCall("GetDC", "ptr", 0)
- pGraphics := Gdip_GraphicsFromHDC(hdc)
- pFamilyObj := Gdip_FontFamilyCreate(fontFamily)
- pFont := Gdip_FontCreate(pFamilyObj, fontSize, 1)
- layoutRect := Buffer(16), NumPut("float", 0, "float", 0, "float", 1000, "float", 100, layoutRect)
- boundingBox := Buffer(16)
- Gdip_MeasureString(pGraphics, text, pFont, layoutRect, 0, boundingBox)
- measuredW := NumGet(boundingBox, 8, "float")
- Gdip_DeleteFont(pFont)
- Gdip_DeleteFontFamily(pFamilyObj)
- Gdip_DeleteGraphics(pGraphics)
- DllCall("ReleaseDC", "ptr", 0, "ptr", hdc)
- return measuredW
- }
- CalculateMaxFontSize(fontFamily, textToMeasure, targetW, targetH, textPadding) {
- hdc := DllCall("GetDC", "ptr", 0)
- pGraphics := Gdip_GraphicsFromHDC(hdc)
- pFamilyObj := Gdip_FontFamilyCreate(fontFamily)
- layoutRect := Buffer(16), NumPut("float", 0, "float", 0, "float", targetW, "float", targetH, layoutRect)
- boundingBox := Buffer(16)
- size := 16
- while (size >= 4) {
- pFont := Gdip_FontCreate(pFamilyObj, size, 1)
- Gdip_MeasureString(pGraphics, textToMeasure, pFont, layoutRect, 0, boundingBox)
- measuredW := NumGet(boundingBox, 8, "float")
- measuredH := NumGet(boundingBox, 12, "float")
- Gdip_DeleteFont(pFont)
- if (measuredW < targetW - textPadding && measuredH < targetH) {
- Gdip_DeleteFontFamily(pFamilyObj)
- Gdip_DeleteGraphics(pGraphics)
- DllCall("ReleaseDC", "ptr", 0, "ptr", hdc)
- return size
- }
- size -= 0.5
- }
- Gdip_DeleteFontFamily(pFamilyObj)
- Gdip_DeleteGraphics(pGraphics)
- DllCall("ReleaseDC", "ptr", 0, "ptr", hdc)
- return 8
- }
- ; --- BIBLIOTEKA GDI+ ---
- Gdip_Startup() {
- if !DllCall("GetModuleHandle", "str", "gdiplus", "ptr")
- DllCall("LoadLibrary", "str", "gdiplus")
- pToken := Buffer(A_PtrSize)
- GdiplusStartupInput := Buffer(24, 0), NumPut("int", 1, GdiplusStartupInput)
- if DllCall("gdiplus\GdiplusStartup", "ptr", pToken, "ptr", GdiplusStartupInput, "ptr", 0)
- return 0
- return NumGet(pToken, "ptr")
- }
- Gdip_Shutdown(pToken) => DllCall("gdiplus\GdiplusShutdown", "ptr", pToken)
- Gdip_GraphicsFromHDC(hdc) {
- pGraphics := Buffer(A_PtrSize)
- DllCall("gdiplus\GdipCreateFromHDC", "ptr", hdc, "ptr", pGraphics)
- return NumGet(pGraphics, "ptr")
- }
- Gdip_DeleteGraphics(pGraphics) => DllCall("gdiplus\GdipDeleteGraphics", "ptr", pGraphics)
- Gdip_SetSmoothingMode(pGraphics, mode) => DllCall("gdiplus\GdipSetSmoothingMode", "ptr", pGraphics, "int", mode)
- Gdip_BrushCreateSolid(color) {
- pBrush := Buffer(A_PtrSize)
- DllCall("gdiplus\GdipCreateSolidFill", "uint", color, "ptr", pBrush)
- return NumGet(pBrush, "ptr")
- }
- Gdip_DeleteBrush(pBrush) => DllCall("gdiplus\GdipDeleteBrush", "ptr", pBrush)
- Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h) => DllCall("gdiplus\GdipFillRectangle", "ptr", pGraphics, "ptr", pBrush, "float", x, "float", y, "float", w, "float", h)
- Gdip_StringFormatCreate() {
- pFormat := Buffer(A_PtrSize)
- DllCall("gdiplus\GdipCreateStringFormat", "int", 0, "int", 0, "ptr", pFormat)
- return NumGet(pFormat, "ptr")
- }
- Gdip_DeleteStringFormat(pFormat) => DllCall("gdiplus\GdipDeleteStringFormat", "ptr", pFormat)
- Gdip_StringFormatSetAlign(pFormat, align) => DllCall("gdiplus\GdipSetStringFormatAlign", "ptr", pFormat, "int", align)
- Gdip_StringFormatSetLineAlign(pFormat, align) => DllCall("gdiplus\GdipSetStringFormatLineAlign", "ptr", pFormat, "int", align)
- Gdip_DrawString(pGraphics, s, pFont, pFormat, pBrush, x, y, w, h) {
- pRect := Buffer(16), NumPut("float", x, "float", y, "float", w, "float", h, pRect)
- DllCall("gdiplus\GdipDrawString", "ptr", pGraphics, "str", s, "int", -1, "ptr", pFont, "ptr", pRect, "ptr", pFormat, "ptr", pBrush)
- }
- Gdip_FontFamilyCreate(sFamily) {
- pFamily := Buffer(A_PtrSize)
- DllCall("gdiplus\GdipCreateFontFamilyFromName", "str", sFamily, "ptr", 0, "ptr", pFamily)
- return NumGet(pFamily, "ptr")
- }
- Gdip_DeleteFontFamily(pFamily) => DllCall("gdiplus\GdipDeleteFontFamily", "ptr", pFamily)
- Gdip_FontCreate(pFamily, nSize, nStyle := 0) {
- pFont := Buffer(A_PtrSize)
- DllCall("gdiplus\GdipCreateFont", "ptr", pFamily, "float", nSize, "int", nStyle, "int", 2, "ptr", pFont)
- return NumGet(pFont, "ptr")
- }
- Gdip_DeleteFont(pFont) => DllCall("gdiplus\GdipDeleteFont", "ptr", pFont)
- Gdip_MeasureString(pGraphics, s, pFont, pLayoutRect, pFormat, pBoundingBox) {
- DllCall("gdiplus\GdipMeasureString", "ptr", pGraphics, "str", s, "int", -1, "ptr", pFont, "ptr", pLayoutRect, "ptr", pFormat, "ptr", pBoundingBox, "ptr*", 0, "ptr*", 0)
- }
- Gdip_Clear(pGraphics, color) => DllCall("gdiplus\GdipGraphicsClear", "ptr", pGraphics, "uint", color)
Advertisement
Add Comment
Please, Sign In to add comment