Re: Subtitle.Render() - Show text with style (Easy, painless Gdip interpreter)
Posted: 15 Sep 2017, 00:47
thanx a lot for these great examples... opened my eyes instantly!
Let's help each other out
https://www.autohotkey.com/boards/
Code: Select all
; subtitleDESKTOPCLOCK - using class_Subtitle and Gdip_All (many thanks to iseahound and tic)
; draggable with CTRL + leftbutton (center-positioned)
; Exit with ESC
; ===========================================================================================
#include class_Subtitle.ahk
#include Gdip_All.ahk
CoordMode, Mouse, Screen
OnMessage(0x201, "OnLeftClick")
IfNotExist, %A_ScriptDir%\config.ini
ExitApp
IniRead, xpos, %A_ScriptDir%\config.ini, desktopclock, xpos
IniRead, ypos, %A_ScriptDir%\config.ini, desktopclock, ypos
pToken := Gdip_Startup()
desktopclock := new Subtitle()
font := "Arial Black"
size := 100
w := size * 6
ydif := size - 28
background := {"x":xpos, "y":ypos, "w":"550", "h":"150", "r":"20" , "anchor":2, "color":"0x10FFFFFF"}
text := {"x":xpos, "y":ypos, "w":w, "anchor":2, "font":font, "size":size, "color":"FFFFFFFF"}
text.outline := {"width":"20", "color":"FF8000", "glow":"0", "glowcolor":"FFFFFFFF"}
text.dropShadow := {"blur":"8", "color":"25000000", "horizontal":"8", "vertical":"8", "strength":"100"}
SetTimer, ticktack, 992
ticktack:
text.x := background.x := xpos
text.y := background.y := ypos
FormatTime, time,, HH·mm·ss
desktopclock.Draw(time, background, text)
desktopclock.Render(time, "cNone", text)
Return
GuiEscape:
ExitApp
OnLeftClick() {
GetKeyState, status, Ctrl ; draggable only with CTRL
If status = D ; while key is down...
{
SendMessage, 0xA1, 2 ; make draggable
GetKeyState, status, SHIFT ; get dummybutton-status
If status = U ; positioning after release dummybutton
{
global xpos, ypos, ydif
MouseGetPos, dragx, dragy
xpos := dragx
ypos := dragy - ydif ; repositioning mouse, centered for dragging after
IniWrite, %xpos%, %A_ScriptDir%\config.ini, desktopclock, xpos
IniWrite, %ypos%, %A_ScriptDir%\config.ini, desktopclock, ypos
}
}
}
Return
Code: Select all
[desktopclock]
xpos =200
ypos =200
Code: Select all
; subtitleDESKTOPCLOCK - using class_Subtitle and Gdip_All (many thanks to iseahound and tic)
; draggable with CTRL + leftbutton (center-positioned)
; Exit with ESC
; ===========================================================================================
#include class_Subtitle.ahk
#include Gdip_All.ahk
CoordMode, Mouse, Screen
Global xpos, ypos
OnMessage(0x201, "OnLeftClick")
IfNotExist, %A_ScriptDir%\config.ini
xpos := 400, ypos := 200
Else {
IniRead, xpos, %A_ScriptDir%\config.ini, desktopclock, xpos
IniRead, ypos, %A_ScriptDir%\config.ini, desktopclock, ypos
}
pToken := Gdip_Startup()
desktopclock := new Subtitle()
font := "Arial Black"
size := 100
w := size * 6
ydif := size - 28
background := {"x":xpos, "y":ypos, "w":"550", "h":"150", "r":"20" , "anchor":2, "color":"0x10FFFFFF"}
text := {"x":xpos, "y":ypos, "w":w, "anchor":2, "font":font, "size":size, "color":"FFFFFFFF"}
text.outline := {"width":"20", "color":"FF8000", "glow":"0", "glowcolor":"FFFFFFFF"}
text.dropShadow := {"blur":"8", "color":"25000000", "horizontal":"8", "vertical":"8", "strength":"100"}
SetTimer, ticktack, 992
ticktack:
text.x := background.x := xpos
text.y := background.y := ypos
FormatTime, time,, HH:mm:ss
desktopclock.Draw(time, background, text)
desktopclock.Render(time, "cNone", text)
Return
GuiEscape:
ExitApp
OnLeftClick() {
MouseGetPos, xClick, yClick ; Where did we click on the screen
GetKeyState, status, Ctrl ; draggable only with CTRL
If (status = "D") { ; while key is down...
SendMessage, 0xA1, 2 ; make draggable
MouseGetPos, dragx, dragy
xpos := dragx - (xclick - xpos) ; Get our x-offset on the rendering
ypos := dragy - (yClick - ypos) ; Get our y-offset on the rendering
; Save the values
IniWrite, %xpos%, %A_ScriptDir%\config.ini, desktopclock, xpos
IniWrite, %ypos%, %A_ScriptDir%\config.ini, desktopclock, ypos
; Update the position
GoSub, ticktack
}
}
Return
Code: Select all
; subtitleDESKTOPCLOCK - using class_Subtitle and Gdip_All (many thanks to iseahound and tic)
; draggable with CTRL + leftbutton (center-positioned)
; Exit with ESC
; ===========================================================================================
#include class_Subtitle.ahk
#include Gdip_All.ahk
CoordMode, Mouse, Screen
OnMessage(0x201, "OnLeftClick")
isClicked := 0
pToken := Gdip_Startup()
desktopclock := new Subtitle()
font := "Arial Black"
size := 100
background := {"x":400, "y":200, "anchor":2, "color":"0x10FFFFFF"}
text := {"font":font, "size":size, "color":"FFFFFFFF"}
text.outline := {"width":"20", "color":"FF8000", "glow":"0", "glowcolor":"FFFFFFFF"}
text.dropShadow := {"blur":"8", "color":"25000000", "horizontal":"8", "vertical":"8", "strength":"100"}
SetTimer, ticktack, 10 ; Changed to 10 milliseconds for smooth animation.
return
ticktack(){
global desktopClock, background, text
global xpos, ypos, isClicked, xClick, yClick
FormatTime, time,, HH:mm:ss
if (isClicked = 1 && GetKeyState("Ctrl")) {
MouseGetPos, dragx, dragy
background.x := xpos + dragx - xClick
background.y := ypos + dragy - yClick
}
if (!GetKeyState("LButton"))
isClicked := 0
desktopclock.Render(time, background, text)
Return
}
GuiEscape:
ExitApp
OnLeftClick() {
global ; Make all these values global.
isClicked := 1
MouseGetPos, xClick, yClick
xpos := background.x
ypos := background.y
}
Return
I did mention that it would eat up CPU power, as it is rendering every 10ms ≈ 100 frames per second. That's quite excessive, so it is possible to render a frame only when 1) time changes, 2) x position changes, or 3) y position changes.kczx3 wrote:Iseahound, your version is quite laggy imo.
Code: Select all
; subtitleDESKTOPCLOCK - using class_Subtitle and Gdip_All (many thanks to iseahound and tic)
; draggable with CTRL + leftbutton (center-positioned)
; Exit with ESC
; ===========================================================================================
#include class_Subtitle.ahk
#include <Gdip_All>
CoordMode, Mouse, Screen
OnMessage(0x201, "OnLeftClick")
isClicked := 0
pToken := Gdip_Startup()
desktopclock := new Subtitle()
font := "Arial Black"
size := 100
background := {"x":400, "y":200, "anchor":2, "color":"0x10FFFFFF"}
text := {"font":font, "size":size, "color":"FFFFFFFF"}
text.outline := {"width":"20", "color":"FF8000", "glow":"0", "glowcolor":"FFFFFFFF"}
text.dropShadow := {"blur":"8", "color":"25000000", "horizontal":"8", "vertical":"8", "strength":"100"}
SetTimer, ticktack, 10
return
ticktack(){
global desktopClock, background, text
global xpos, ypos, isClicked, xClick, yClick
static time_past, x_past, y_past
FormatTime, time,, HH:mm:ss
if (isClicked = 1 && GetKeyState("Ctrl")) {
MouseGetPos, dragx, dragy
background.x := xpos + dragx - xClick
background.y := ypos + dragy - yClick
}
if (!GetKeyState("LButton"))
isClicked := 0
if (time != time_past || background.x != x_past || background.y != y_past) {
time_past := time, x_past := background.x, y_past := background.y
desktopclock.Render(time, background, text)
}
Return
}
GuiEscape:
ExitApp
OnLeftClick() {
global ; Make all these values global.
isClicked := 1
MouseGetPos, xClick, yClick
xpos := background.x
ypos := background.y
}
Return
Code: Select all
static instances := []
static pToken := ""
past := {}, ScreenWidth := A_ScreenWidth, ScreenHeight := A_ScreenHeight
__New(name := "")
{
if (Subtitle.instances.Length() = 0)
Subtitle.pToken := Gdip_Startup()
Subtitle.instances.Push(&this)
parent := ((___ := RegExReplace(A_ThisFunc, "^(.*)\..*\..*$", "$1")) != A_ThisFunc) ? ___ : ""
Loop, Parse, parent, .
this.parent := (A_Index=1) ? %A_LoopField% : this.parent[A_LoopField]
this.parent.Startup()
Gui, New, +LastFound +AlwaysOnTop -Caption -DPIScale +E0x80000 +ToolWindow +hwndSecretName
this.hwnd := SecretName
this.name := (name != "") ? name "_Subtitle" : "Subtitle_" this.hwnd
DllCall("ShowWindow", "ptr",this.hWnd, "int",8)
DllCall("SetWindowText", "ptr",this.hWnd, "str",this.name)
this.hbm := CreateDIBSection(this.ScreenWidth, this.ScreenHeight)
this.hdc := CreateCompatibleDC()
this.obm := SelectObject(this.hdc, this.hbm)
this.G := Gdip_GraphicsFromHDC(this.hdc)
this.colorMap := this.colorMap()
}
__Delete(){
this.parent.Shutdown()
for k, v in Subtitle.instances
if (v = &this)
Subtitle.instances.RemoveAt(k)
if (Subtitle.instances.Length() = 0)
Gdip_Shutdown(Subtitle.pToken)
}
Code: Select all
class Graphics {
static pToken, Gdip := 0
Startup(){
return Graphics.pToken := (Graphics.Gdip++ > 0) ? Graphics.pToken : Gdip_Startup()
}
Shutdown(){
return Graphics.pToken := (--Graphics.Gdip == 0) ? Gdip_Shutdown(Graphics.pToken) : Graphics.pToken
}
// insert Subtitle class here
}
Code: Select all
; Check if this class is nested within a parent class
parent := ((___ := RegExReplace(A_ThisFunc, "^(.*)\..*\..*$", "$1")) != A_ThisFunc) ? ___ : ""
; Find the name of the parent class
Loop, Parse, parent, .
this.parent := (A_Index=1) ? %A_LoopField% : this.parent[A_LoopField]
; Call the startup method of the parent class
this.parent.Startup()