Coloured Buttons with GDI+
Image:
Code:
Code:
#NoEnv
; Bitte auskommentieren, wenn sich Gdip.ahk im Standardverzeichnis befindet
#Include, Gdip.ahk
Gui, Margin, 50, 20
Gui, Color, 608080
Gui, Font, s8, Arial
CreateButtonImage("SetDefault", "FN", "Arial")
Gui, Add, Button, w100, s8!
Gui, Add, Button, y+10 w100 hwndBT1ID, s8!
hbmBT1 := CreateButtonImage(BT1ID, "00FF00", "000000", "", "", 1)
Gui, Font, s10
Gui, Add, Button, y+10 w100 h40 hwndBT2ID, s10!
hbmBT2 := CreateButtonImage(BT2ID, "D0E000", "800000", 10, "", 1)
Gui, Font, s12
Gui, Add, Button, y+10 w100 h71 hwndBT3ID, s12!
hbmBT3 := CreateButtonImage(BT3ID, "FF0000", "00FFFF", 12, "", 1)
Gui, Font, s48, Trebuchet MS
Gui, Add, Button, y+10 hwndBT4ID, S48 AUTOSIZE!
hbmBT4 := CreateButtonImage(BT4ID, "C0C0C0", "FF0000", 48, "Trebuchet MS", 2, 1)
Gui, Show, , Farbige Buttons
Msg := "Der Standardfont ist:`n`n`" CreateButtonImage("GetDefault", "FN")
MsgBox, 0, Farbige Buttons, %MSG%, 1
Return
;-------------------------------------------------------------------------------
GuiClose:
GuiEscape:
DllCall("DeleteObject", "UInt", hbmBT1)
DllCall("DeleteObject", "UInt", hbmBT2)
DllCall("DeleteObject", "UInt", hbmBT3)
DllCall("DeleteObject", "UInt", hbmBT4)
ExitApp
; ------------------------------------------------------------------------------
; ##############################################################################
; ------------------------------------------------------------------------------
CreateButtonImage(HWND
, CB
, CT = ""
, FS = ""
, FN = ""
, 3D = ""
, GC = "") {
; ---------------------------------------------------------------------------
; HWND : HWND des Buttons
; CB : Hintergrundfarbe (RGB: 6-stelliger Hexwert: RRGGBB )
; CT : Textfarbe (RGB: 6-stelliger Hexwert: RRGGBB )
; FS : Schriftgröße
; FN : Zeichensatzname
; 3D : 3D-Effekte (0 = ohne, 1 = erhaben, 2 = 3D)
; GC : Gammakorrektur (0 = Nein, 1 = Ja)
; ---------------------------------------------------------------------------
; Für die meisten GDI+ Aufrufe wird das Skript GDIP.AHK von tic benötigt
; --> http://www.autohotkey.com/forum/post-198949.html
; Ohne dieses Skript gäbe es das hier nicht!!!
; Ergänzungen sind durch vorangestellte Kommentare und die Einleitung
; GpStatus WINGDIPAPI ...
; gekennzeichnet.
; ---------------------------------------------------------------------------
; Buttonkonstanten
Static BS_BITMAP := 0x80
Static BM_SETIMAGE := 0xF7
Static IMAGE_BITMAP := 0x0
; Standardwerte für Parameter
Static SETDEFAULT := "SetDefault"
Static GETDEFAULT := "GetDefault"
Static DEFAULTS := "FS,FN,CT,3D,GC"
Static DEFFS := 8
Static DEFFN := "Microsoft Sans Serif"
Static DEFCT := 0x000000
Static DEF3D := 0
Static DEFGC := 1
; ---------------------------------------------------------------------------
; Standardwerte setzen?
If (HWND = SETDEFAULT) {
If InStr(DEFAULTS, CB) {
DEF%CB% := CT
Return True
} Else {
Return False
}
}
; ---------------------------------------------------------------------------
; Standardwerte ausgeben?
If (HWND = GETDEFAULT) {
If InStr(DEFAULTS, CB) {
Return DEF%CB%
} Else {
Return "ERROR"
}
}
; ---------------------------------------------------------------------------
; Ggf. Standardparameterwerte setzen
If (CT = "") {
CT := DEFCT
}
If (FS = "") {
FS := DEFFS
}
If (FN = "") {
FN := DEFFN
}
If (3D = "") {
3D := DEF3D
}
If (GC = "") {
GC := DEFGC
}
; ---------------------------------------------------------------------------
; Prüfen, ob GDIPlus verfügbar ist
If (!pToken := Gdip_Startup()) {
MsgBox, 48, GDIplus Fehler!
, % "GDIplus konnte nicht gestartet werden!`n"
. "Überprüfen Sie, ob GDIPlus auf Ihrem System vorhanden ist!"
Return 0
}
; ---------------------------------------------------------------------------
; Jetzt wird geprüft, ob der übergebene Font vorhanden ist
If !(hFamily := Gdip_FontFamilyCreate(FN)) {
MsgBox, 48, Font Fehler!
, Der übergebene Font ist auf diesem System nicht vorhanden!
Gdip_Shutdown(pToken)
Return 0
}
; ---------------------------------------------------------------------------
; Voreinstellung der Anzeige-DPI ermitteln
; Geklaut von http://www.autohotkey.com/forum/post-198727.html
hDC := DllCall("GetDC")
DPI := DllCall("GetDeviceCaps"
, "UInt", hDC
, "Int", 88)
DllCall("ReleaseDC"
, "Int", 0
, "UInt", hDC)
; ---------------------------------------------------------------------------
; Clientbereich des Buttons ermitteln
VarSetCapacity(RECT, 16, 0)
DllCall("GetClientRect"
, "UInt", HWND
, "UInt", &RECT)
W := NumGet(RECT, 8)
H := NumGet(RECT, 12)
; ---------------------------------------------------------------------------
; Buttontext holen
VarSetCapacity(TX, 255, 0)
DllCall("GetWindowText"
, "UInt", HWND
, "Str", TX
, "Int", 255)
VarSetCapacity(TX, -1)
; ---------------------------------------------------------------------------
; GDI+ Bitmap erzeugen
pBitmap := Gdip_CreateBitmap(W , H)
; ---------------------------------------------------------------------------
; Zeiger auf das zugehörige Grafikobjekt erzeugen
pGraphics := Gdip_GraphicsFromImage(pBitmap)
; ---------------------------------------------------------------------------
; Kantenglättung auf Antializing = 4 setzen
Gdip_SetSmoothingMode(pGraphics, 4)
; ---------------------------------------------------------------------------
; Buttonrechteck mit einem Verlauf füllen
; ---------------------------------------------------------------------------
; POINT Struktur für den Start füllen
VarSetCapacity(Point1, 8, 0)
NumPut(0, Point1, 0, "Float")
NumPut(0, Point1, 4, "Float")
; ---------------------------------------------------------------------------
; POINT Struktur für das Ende füllen
VarSetCapacity(Point2, 8, 0)
NumPut(0, Point2, 0, "Float")
NumPut(H, Point2, 4, "Float")
; ---------------------------------------------------------------------------
; Startfarbe: Schwarz
Color1 := "0xFF000000"
; ---------------------------------------------------------------------------
; Endfarbe: Hintergrundfarbe
Color2 := "0xFF" CB
; ---------------------------------------------------------------------------
; "linear gradient" Pinsel erstellen
; GpStatus WINGDIPAPI GdipCreateLineBrush(GDIPCONST GpPointF* point1
; , GDIPCONST GpPointF* point2
; , ARGB color1
; , ARGB color2
; , GpWrapMode wrapMode
; , GpLineGradient **lineGradient)
DllCall("gdiplus\GdipCreateLineBrush"
, "UInt", &Point1
, "UInt", &Point2
, "Int", Color1
, "Int", Color2
, "Int", 0
, "UInt*", pBrush)
; ---------------------------------------------------------------------------
; Array für relative Intensität versorgen
VarSetCapacity(RELINT, 20, 0)
I1 := (3D = 0 ? 1.0 : 3D = 1 ? 0.1 : 0.0)
I2 := (3D = 0 ? 1.0 : 3D = 1 ? 1.0 : 0.4)
I3 := (3D = 0 ? 1.0 : 3D = 1 ? 1.0 : 1.0)
I4 := (3D = 0 ? 1.0 : 3D = 1 ? 1.0 : 0.4)
I5 := (3D = 0 ? 1.0 : 3D = 1 ? 0.1 : 0.0)
NumPut(I1, RELINT, 0, "Float")
NumPut(I2, RELINT, 4, "Float")
NumPut(I3, RELINT, 8, "Float")
NumPut(I4, RELINT, 12, "Float")
NumPut(I5, RELINT, 16, "Float")
; ---------------------------------------------------------------------------
; Array für relative Position versorgen
I2 := (H * 0.2) < 6 ? 6 / H : 0.2
I4 := 1.0 - I2
VarSetCapacity(RELPOS, 20, 0)
NumPut(0.0, RELPOS, 0, "Float")
NumPut(I2, RELPOS, 4, "Float")
NumPut(0.5, RELPOS, 8, "Float")
NumPut(I4, RELPOS, 12, "Float")
NumPut(1.0, RELPOS, 16, "Float")
; ---------------------------------------------------------------------------
; Überblendregeln setzen
; GpStatus WINGDIPAPI GdipSetLineBlend(GpLineGradient *brush
; , GDIPCONST REAL *blend
; , GDIPCONST REAL* positions
; , INT count)
DllCall("gdiplus\GdipSetLineBlend"
, "UInt", pBrush
, "UInt", &RELINT
, "UInt", &RELPOS
, "Int", 5)
; ---------------------------------------------------------------------------
; "gamma correction" setzen
; GpStatus WINGDIPAPI GdipSetLineGammaCorrection(GpLineGradient *brush
; , BOOL *useGammaCorrection)
DllCall("gdiplus\GdipSetLineGammaCorrection"
, "UInt", pBrush
, "Int", GC)
; ---------------------------------------------------------------------------
; Buttonrechteck füllen
Gdip_FillRectangle(pGraphics, pBrush, 0, 0, W, H)
; ---------------------------------------------------------------------------
; Pinsel wieder freigeben
Gdip_DeleteBrush(pBrush)
; ---------------------------------------------------------------------------
; Fontobjekt mit Größe = S und Stil = Normal (0) erzeugen
hFont := Gdip_FontCreate(hFamily, FS * (DPI/72), 0)
; ---------------------------------------------------------------------------
; Formatobjekt erzeugen
hFormat := Gdip_StringFormatCreate(0x4000)
; ---------------------------------------------------------------------------
; Übergebene Textfarbe setzen
pBrush := Gdip_BrushCreateSolid("0xFF" CT)
; ---------------------------------------------------------------------------
; Textausrichtung auf zentriert = 1 setzen
Gdip_SetStringFormatAlign(hFormat, 1)
; ---------------------------------------------------------------------------
; Renderqualität auf AntiAliasGridFit = 3 setzen
Gdip_SetTextRenderingHint(pGraphics, 3)
; ---------------------------------------------------------------------------
; Rechteck für den Text vorgeben
CreateRectF(RECT, 0, 0, W, H)
; ---------------------------------------------------------------------------
; Benötigtes Rechteck für Text vermessen
TR := Gdip_MeasureString(pGraphics, TX, hFont, hFormat, RECT)
; ---------------------------------------------------------------------------
; Rechteck für den Text dem Ergebnis der Messung anpassen
StringSplit, TR, TR, |
Y := (H / 2) - (TR4 / 2)
CreateRectF(RECT, 0, Y, W, TR4)
; ---------------------------------------------------------------------------
; Text zeichnen
Gdip_DrawString(pGraphics, TX, hFont, hFormat, pBrush, RECT)
; ---------------------------------------------------------------------------
; HBITMAP Objekt aus der Bitmap erzeugen
hBitmap := Gdip_CreateHBITMAPFromBitmap(pBitmap)
; ---------------------------------------------------------------------------
; Ressourcen freigeben
; Die Bitmap kann jetzt gelöscht werden
Gdip_DisposeImage(pBitmap)
; Pinsel wieder freigeben
Gdip_DeleteBrush(pBrush)
; Formatobjekt wieder freigeben
Gdip_DeleteStringFormat(hFormat)
; Fontobjekt wieder freigeben
Gdip_DeleteFont(hFont)
; Fontfamilyobjekt wieder freigeben
Gdip_DeleteFontFamily(hFamily)
; Grafikobjekt wieder freigeben
Gdip_DeleteGraphics(pGraphics)
; GDI+ beenden
Gdip_Shutdown(pToken)
; ---------------------------------------------------------------------------
; Und jetzt den Button versorgen:
WinSet, Style, +%BS_BITMAP%, ahk_id %HWND%
SendMessage, BM_SETIMAGE, IMAGE_BITMAP, hBitmap, , ahk_id %HWND%
; HBITMAP wird zurückgegeben, damit das Objekt später freigegeben werden kann
Return hBitmap
}