SheridanCoriana6

Affinity my toolbar

Nov 14th, 2025 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.30 KB | Source Code | 0 0
  1. #Requires AutoHotkey v2.0
  2.  
  3. ; --- Konfiguracja ---
  4. AffinityExe := "Affinity.exe"
  5. global BTN_HEIGHT := 25, PADDING := 5
  6. global FONT_FAMILY := "Segoe UI"
  7.  
  8. ; --- Wczytywanie ustawień ---
  9. global IniFile := A_ScriptDir "\AffinityKeyboard_Settings.ini"
  10. global posX := IniRead(IniFile, "Position", "X", 200)
  11. global posY := IniRead(IniFile, "Position", "Y", 200)
  12.  
  13. ; --- Inicjalizacja GDI+ ---
  14. global gdiplusToken := Gdip_Startup()
  15. if !gdiplusToken {
  16. MsgBox("Krytyczny błąd: Nie można uruchomić biblioteki GDI+! Skrypt zostanie zamknięty.")
  17. ExitApp
  18. }
  19.  
  20. ; --- Definicje przycisków ---
  21. global buttons := []
  22. buttons_data := [
  23. {name:"New", color:"0xFF8BC34A", action:"^n"},
  24. {name:"Open", color:"0xFF03A9F4", action:"^o"},
  25. {name:"Copy", color:"0xFFFFC107", action:"^c"},
  26. {name:"Paste", color:"0xFFFFEB3B", action:"^v"},
  27. {name:"Undo", color:"0xFFFF7043", action:"^z"},
  28. {name:"Redo", color:"0xFFE91E63", action:"^+z"},
  29. {name:"Delete", color:"0xFF9E9E9E", action:"{Backspace}"},
  30. {name:"Invert", color:"0xFFC47DD0", action:"^i"},
  31. {name:"Fit", color:"0xFFADD8E6", action:"^0"},
  32. {name:"[", color:"0xFFEBEBEB", action:"["},
  33. {name:"]", color:"0xFFEBEBEB", action:"]"},
  34. {name:"Merge.S", color:"0xFF32FD53", action:"^+e"},
  35. {name:"Merge.V", color:"0xFF2BE6FF", action:"^!+e"},
  36. {name:"Duplicate", color:"0xFFFFA8F09", action:"^j"},
  37. {name:"Exp2JPG", color:"0xFFFFEB3B", action:"^!+w"}
  38. ]
  39.  
  40. ; --- Obliczanie optymalnego rozmiaru czcionki i dynamicznej szerokości przycisków ---
  41. longest_name := ""
  42. for _, btn_data in buttons_data {
  43. if StrLen(btn_data.name) > StrLen(longest_name)
  44. longest_name := btn_data.name
  45. }
  46.  
  47. ; Oblicz max rozmiar czcionki bazując na najdłuższym tekście i przykładowej szerokości (np. 100)
  48. global maxFontSize := CalculateMaxFontSize(FONT_FAMILY, longest_name, 100, BTN_HEIGHT, 8)
  49.  
  50. x := PADDING
  51. for _, btn_data in buttons_data {
  52. textWidth := MeasureTextWidth(btn_data.name, maxFontSize, FONT_FAMILY)
  53. nameLength := StrLen(btn_data.name)
  54.  
  55. ; *** KLUCZOWA POPRAWKA ***
  56. ; Oblicz padding jako średnią szerokość jednego znaku z tekstu na przycisku.
  57. charPadding := 0
  58. if (nameLength > 0) {
  59. charPadding := textWidth / nameLength
  60. } else {
  61. ; Zapasowy padding, gdyby nazwa była pusta (mało prawdopodobne)
  62. charPadding := MeasureTextWidth("M", maxFontSize, FONT_FAMILY)
  63. }
  64.  
  65. btn_width := Round(textWidth + (2 * charPadding)) ; Szerokość tekstu + padding z obu stron
  66.  
  67. btn_data.x := x, btn_data.y := PADDING, btn_data.w := btn_width, btn_data.h := BTN_HEIGHT
  68. buttons.Push(btn_data)
  69. x += btn_width + PADDING
  70. }
  71. buttons.Push({name: "X", color: "0xFFF44336", action: "ExitApp", x: x + 10, y: PADDING, w: 25, h: 25})
  72. gui_width := buttons.Get(buttons.Length).x + buttons.Get(buttons.Length).w + PADDING
  73.  
  74. ; --- Tworzenie GUI ---
  75. MyGui := Gui("+AlwaysOnTop +ToolWindow -Caption +E0x2000000")
  76. MyGui.OnEvent("Close", GuiClose)
  77. global hoveredButtonIndex := 0 ; Zmienna do śledzenia najechanego przycisku
  78.  
  79. ; --- Przechwytywanie komunikatów systemowych ---
  80. OnMessage(0xF, WM_PAINT)
  81. OnMessage(0x200, WM_MOUSEMOVE)
  82. OnMessage(0x201, WM_LBUTTONDOWN)
  83.  
  84. MyGui.Show("x" posX " y" posY " w" gui_width " h" (BTN_HEIGHT + PADDING * 2) " NA")
  85. return
  86.  
  87. ; === GŁÓWNE FUNKCJE ===
  88. WM_PAINT(wParam, lParam, msg, hwnd) {
  89. global MyGui, maxFontSize, FONT_FAMILY, hoveredButtonIndex
  90. if (hwnd != MyGui.Hwnd)
  91. return
  92.  
  93. ps := Buffer(A_PtrSize * (A_PtrSize == 4 ? 9 : 11))
  94. hdc := DllCall("BeginPaint", "ptr", hwnd, "ptr", ps, "ptr")
  95.  
  96. pGraphics := Gdip_GraphicsFromHDC(hdc)
  97. Gdip_SetSmoothingMode(pGraphics, 4)
  98. Gdip_Clear(pGraphics, 0xFF404040)
  99.  
  100. pFamily := Gdip_FontFamilyCreate(FONT_FAMILY)
  101. pFont := Gdip_FontCreate(pFamily, maxFontSize, 1)
  102.  
  103. pFormat := Gdip_StringFormatCreate()
  104. Gdip_StringFormatSetAlign(pFormat, 1)
  105. Gdip_StringFormatSetLineAlign(pFormat, 1)
  106. pBrushText := Gdip_BrushCreateSolid(0xFF000000)
  107.  
  108. for i, btn in buttons {
  109. btnColor := btn.color
  110. if (i = hoveredButtonIndex) {
  111. ; Zmień kolor dla efektu hover - ustaw alpha na ~70% (0xB3)
  112. btnColor := (btn.color & 0x00FFFFFF) | 0xB3000000
  113. }
  114.  
  115. pBrushBG := Gdip_BrushCreateSolid(btnColor)
  116. Gdip_FillRectangle(pGraphics, pBrushBG, btn.x, btn.y, btn.w, btn.h)
  117. Gdip_DrawString(pGraphics, btn.name, pFont, pFormat, pBrushText, btn.x, btn.y, btn.w, btn.h)
  118. Gdip_DeleteBrush(pBrushBG)
  119. }
  120.  
  121. Gdip_DeleteFontFamily(pFamily)
  122. Gdip_DeleteFont(pFont)
  123. Gdip_DeleteBrush(pBrushText)
  124. Gdip_DeleteStringFormat(pFormat)
  125. Gdip_DeleteGraphics(pGraphics)
  126.  
  127. DllCall("EndPaint", "ptr", hwnd, "ptr", ps)
  128. return 0
  129. }
  130.  
  131. WM_MOUSEMOVE(wParam, lParam, msg, hwnd) {
  132. global MyGui, buttons, hoveredButtonIndex
  133. if (hwnd != MyGui.Hwnd)
  134. return
  135.  
  136. x := lParam & 0xFFFF
  137. y := (lParam >> 16) & 0xFFFF
  138.  
  139. currentlyHovered := 0
  140. for i, btn in buttons {
  141. if (x >= btn.x and x <= btn.x + btn.w and y >= btn.y and y <= btn.y + btn.h) {
  142. currentlyHovered := i
  143. break
  144. }
  145. }
  146.  
  147. if (currentlyHovered != hoveredButtonIndex) {
  148. hoveredButtonIndex := currentlyHovered
  149. ; Wymuś przerysowanie okna, aby pokazać/ukryć efekt hover
  150. DllCall("RedrawWindow", "ptr", MyGui.Hwnd, "ptr", 0, "ptr", 0, "uint", 0x0001) ; RDW_INVALIDATE
  151. }
  152. }
  153.  
  154.  
  155. WM_LBUTTONDOWN(wParam, lParam, msg, hwnd) {
  156. global MyGui, buttons, IniFile, posX, posY
  157. if (hwnd != MyGui.Hwnd)
  158. return
  159.  
  160. x := lParam & 0xFFFF
  161. y := (lParam >> 16) & 0xFFFF
  162.  
  163. for _, btn in buttons {
  164. if (x >= btn.x and x <= btn.x + btn.w and y >= btn.y and y <= btn.y + btn.h) {
  165. if (btn.action = "ExitApp") {
  166. WinGetPos(&posX, &posY, , , MyGui.Hwnd)
  167. IniWrite(posX, IniFile, "Position", "X")
  168. IniWrite(posY, IniFile, "Position", "Y")
  169. MyGui.Destroy()
  170. } else {
  171. SendToAffinity(btn.action)
  172. }
  173. return
  174. }
  175. }
  176. PostMessage(0xA1, 2, , , "A")
  177. }
  178.  
  179. SendToAffinity(shortcut) {
  180. global AffinityExe
  181. if WinExist("ahk_exe " AffinityExe) {
  182. WinActivate("ahk_exe " AffinityExe)
  183. Send(shortcut)
  184. } else {
  185. MsgBox("Nie można znaleźć uruchomionego procesu " AffinityExe)
  186. }
  187. }
  188.  
  189. GuiClose(*) {
  190. global gdiplusToken
  191. Gdip_Shutdown(gdiplusToken)
  192. ExitApp
  193. }
  194.  
  195. MeasureTextWidth(text, fontSize, fontFamily) {
  196. hdc := DllCall("GetDC", "ptr", 0)
  197. pGraphics := Gdip_GraphicsFromHDC(hdc)
  198. pFamilyObj := Gdip_FontFamilyCreate(fontFamily)
  199. pFont := Gdip_FontCreate(pFamilyObj, fontSize, 1)
  200.  
  201. layoutRect := Buffer(16), NumPut("float", 0, "float", 0, "float", 1000, "float", 100, layoutRect)
  202. boundingBox := Buffer(16)
  203. Gdip_MeasureString(pGraphics, text, pFont, layoutRect, 0, boundingBox)
  204. measuredW := NumGet(boundingBox, 8, "float")
  205.  
  206. Gdip_DeleteFont(pFont)
  207. Gdip_DeleteFontFamily(pFamilyObj)
  208. Gdip_DeleteGraphics(pGraphics)
  209. DllCall("ReleaseDC", "ptr", 0, "ptr", hdc)
  210. return measuredW
  211. }
  212.  
  213. CalculateMaxFontSize(fontFamily, textToMeasure, targetW, targetH, textPadding) {
  214. hdc := DllCall("GetDC", "ptr", 0)
  215. pGraphics := Gdip_GraphicsFromHDC(hdc)
  216. pFamilyObj := Gdip_FontFamilyCreate(fontFamily)
  217. layoutRect := Buffer(16), NumPut("float", 0, "float", 0, "float", targetW, "float", targetH, layoutRect)
  218. boundingBox := Buffer(16)
  219.  
  220. size := 16
  221. while (size >= 4) {
  222. pFont := Gdip_FontCreate(pFamilyObj, size, 1)
  223. Gdip_MeasureString(pGraphics, textToMeasure, pFont, layoutRect, 0, boundingBox)
  224. measuredW := NumGet(boundingBox, 8, "float")
  225. measuredH := NumGet(boundingBox, 12, "float")
  226. Gdip_DeleteFont(pFont)
  227. if (measuredW < targetW - textPadding && measuredH < targetH) {
  228. Gdip_DeleteFontFamily(pFamilyObj)
  229. Gdip_DeleteGraphics(pGraphics)
  230. DllCall("ReleaseDC", "ptr", 0, "ptr", hdc)
  231. return size
  232. }
  233. size -= 0.5
  234. }
  235.  
  236. Gdip_DeleteFontFamily(pFamilyObj)
  237. Gdip_DeleteGraphics(pGraphics)
  238. DllCall("ReleaseDC", "ptr", 0, "ptr", hdc)
  239. return 8
  240. }
  241.  
  242. ; --- BIBLIOTEKA GDI+ ---
  243. Gdip_Startup() {
  244. if !DllCall("GetModuleHandle", "str", "gdiplus", "ptr")
  245. DllCall("LoadLibrary", "str", "gdiplus")
  246. pToken := Buffer(A_PtrSize)
  247. GdiplusStartupInput := Buffer(24, 0), NumPut("int", 1, GdiplusStartupInput)
  248. if DllCall("gdiplus\GdiplusStartup", "ptr", pToken, "ptr", GdiplusStartupInput, "ptr", 0)
  249. return 0
  250. return NumGet(pToken, "ptr")
  251. }
  252. Gdip_Shutdown(pToken) => DllCall("gdiplus\GdiplusShutdown", "ptr", pToken)
  253. Gdip_GraphicsFromHDC(hdc) {
  254. pGraphics := Buffer(A_PtrSize)
  255. DllCall("gdiplus\GdipCreateFromHDC", "ptr", hdc, "ptr", pGraphics)
  256. return NumGet(pGraphics, "ptr")
  257. }
  258. Gdip_DeleteGraphics(pGraphics) => DllCall("gdiplus\GdipDeleteGraphics", "ptr", pGraphics)
  259. Gdip_SetSmoothingMode(pGraphics, mode) => DllCall("gdiplus\GdipSetSmoothingMode", "ptr", pGraphics, "int", mode)
  260. Gdip_BrushCreateSolid(color) {
  261. pBrush := Buffer(A_PtrSize)
  262. DllCall("gdiplus\GdipCreateSolidFill", "uint", color, "ptr", pBrush)
  263. return NumGet(pBrush, "ptr")
  264. }
  265. Gdip_DeleteBrush(pBrush) => DllCall("gdiplus\GdipDeleteBrush", "ptr", pBrush)
  266. Gdip_FillRectangle(pGraphics, pBrush, x, y, w, h) => DllCall("gdiplus\GdipFillRectangle", "ptr", pGraphics, "ptr", pBrush, "float", x, "float", y, "float", w, "float", h)
  267. Gdip_StringFormatCreate() {
  268. pFormat := Buffer(A_PtrSize)
  269. DllCall("gdiplus\GdipCreateStringFormat", "int", 0, "int", 0, "ptr", pFormat)
  270. return NumGet(pFormat, "ptr")
  271. }
  272. Gdip_DeleteStringFormat(pFormat) => DllCall("gdiplus\GdipDeleteStringFormat", "ptr", pFormat)
  273. Gdip_StringFormatSetAlign(pFormat, align) => DllCall("gdiplus\GdipSetStringFormatAlign", "ptr", pFormat, "int", align)
  274. Gdip_StringFormatSetLineAlign(pFormat, align) => DllCall("gdiplus\GdipSetStringFormatLineAlign", "ptr", pFormat, "int", align)
  275. Gdip_DrawString(pGraphics, s, pFont, pFormat, pBrush, x, y, w, h) {
  276. pRect := Buffer(16), NumPut("float", x, "float", y, "float", w, "float", h, pRect)
  277. DllCall("gdiplus\GdipDrawString", "ptr", pGraphics, "str", s, "int", -1, "ptr", pFont, "ptr", pRect, "ptr", pFormat, "ptr", pBrush)
  278. }
  279. Gdip_FontFamilyCreate(sFamily) {
  280. pFamily := Buffer(A_PtrSize)
  281. DllCall("gdiplus\GdipCreateFontFamilyFromName", "str", sFamily, "ptr", 0, "ptr", pFamily)
  282. return NumGet(pFamily, "ptr")
  283. }
  284. Gdip_DeleteFontFamily(pFamily) => DllCall("gdiplus\GdipDeleteFontFamily", "ptr", pFamily)
  285. Gdip_FontCreate(pFamily, nSize, nStyle := 0) {
  286. pFont := Buffer(A_PtrSize)
  287. DllCall("gdiplus\GdipCreateFont", "ptr", pFamily, "float", nSize, "int", nStyle, "int", 2, "ptr", pFont)
  288. return NumGet(pFont, "ptr")
  289. }
  290. Gdip_DeleteFont(pFont) => DllCall("gdiplus\GdipDeleteFont", "ptr", pFont)
  291. Gdip_MeasureString(pGraphics, s, pFont, pLayoutRect, pFormat, pBoundingBox) {
  292. DllCall("gdiplus\GdipMeasureString", "ptr", pGraphics, "str", s, "int", -1, "ptr", pFont, "ptr", pLayoutRect, "ptr", pFormat, "ptr", pBoundingBox, "ptr*", 0, "ptr*", 0)
  293. }
  294. Gdip_Clear(pGraphics, color) => DllCall("gdiplus\GdipGraphicsClear", "ptr", pGraphics, "uint", color)
Tags: Affinity
Advertisement
Add Comment
Please, Sign In to add comment