Create Screenshot specific area Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 13:40

feeko wrote: Yeah, I have that included at the top of my script. Any reason why my file extension has 2 .ahk? I don't think it worked if I didn't have it.

Code: Select all

#include C:\Users\Owner\Documents\Other\AutoHotkey Scripts\Gdip.ahk.ahk
Yes, the reason is you have actually named the file Gdip.ahk.ahk. And you most likley think it's not named that because in File Explorer, you have the default setting enabled where it hides file extensions for known file types, so it displays .ahk files without the .ahk extension. So to make it look like it has one in File Explorer, you added .ahk to the file name that already had a .ahk extension. You should disable the terrible "Hide extensions for known file types" option in the "View" tab of the "Folder Options" window so you can see what files are actually named.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 19 Apr 2022, 14:25

Why doesn't this work:

Code: Select all

^+PrintScreen::
WinGetPos, X, Y, W, H, A
Clipboard := "(X" | "Y"| "W" | "H)"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen%Clipboard%
Gdip_SaveBitmapToFile(snap, OutFile, 100)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 15:04

@LAPIII -- The syntax is wrong on this line:

Code: Select all

Clipboard := "(X" | "Y"| "W" | "H)"
You put quotes around what would be the variable names, making them text, and you didn't put quotes around the pipe characters, making them bitwise OR operators instead of pipe characters. See Expressions to learn more. Should be:

Code: Select all

Clipboard := "(" X "|" Y "|" W "|" H ")"

Also this is not correct for an expression or a function call (missing the parentheses and shouldn't have % signs):

Code: Select all

snap := Gdip_BitmapFromScreen%Clipboard%
Should be:

Code: Select all

snap := Gdip_BitmapFromScreen(Clipboard)

Also, why use the clipboard as your variable? There seems to be no reason to use that rather than a regular variable.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 19 Apr 2022, 15:30

Perfect:

Code: Select all

#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

^+PrintScreen::
WinGetPos, X, Y, W, H, A
Clipboard := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Clipboard)
Gdip_SaveBitmapToFile(snap, OutFile, 100)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 15:37

More perfect:

Code: Select all

#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

^+PrintScreen::
WinGetPos, X, Y, W, H, A
Area := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Area)
Gdip_SaveBitmapToFile(snap, OutFile, 100)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

Again, there is no reason for using the Clipboard as a variable here.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 15:46

By the way, the 100 in this line doesn't do anything since your file is a png file:

Code: Select all

Gdip_SaveBitmapToFile(snap, OutFile, 100)
Defining the quality of the image only applies to jpg files. Lossless files like png and bmp are always 100% quality.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 19 Apr 2022, 16:31

If I attach this to the script:

Code: Select all

ToolTipFont("s14", "Comic Sans MS") 
ToolTipColor("green", "white")
ToolTip, Active Window Screenshot, 1525, 0
SetTimer, RemoveToolTip, -500
return

RemoveToolTip:
ToolTip
return

; ToolTipOpt v1.004
 
ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

Using Area doesn't work and with Clipboard, sometimes I've got to press the hotkey twice, I get:

TEST 2.ahk 4_19_2022 5_27_51 PM.png
TEST 2.ahk 4_19_2022 5_27_51 PM.png (22.46 KiB) Viewed 1277 times

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 16:45

What do you mean by attaching that to the script? You mean you just pasted one script on the end of the other with no attention to the auto-execute sections of each?

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 19 Apr 2022, 17:16

Yeah, could you help:

Code: Select all

#SingleInstance, Force
#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

^+PrintScreen::
WinGetPos, X, Y, W, H, A
Clipboard := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Clipboard)
Gdip_SaveBitmapToFile(snap, OutFile)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

ToolTipFont("s14", "Comic Sans MS") 
ToolTipColor("green", "white")
ToolTip, Active Window Screenshot, 1525, 0
SetTimer, RemoveToolTip, -500
return

RemoveToolTip:
ToolTip
return

; ToolTipOpt v1.004
 
ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 17:54

Next time I'd rather have you learn and apply the concept that I tell you is the issue.

Code: Select all

#SingleInstance, Force
#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

ToolTipFont("s14", "Comic Sans MS") 
ToolTipColor("green", "white")
ToolTip, Active Window Screenshot, 1525, 0
SetTimer, RemoveToolTip, -500
return

^+PrintScreen::
WinGetPos, X, Y, W, H, A
Clipboard := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Clipboard)
Gdip_SaveBitmapToFile(snap, OutFile)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

RemoveToolTip:
ToolTip
return

; ToolTipOpt v1.004
 
ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 19 Apr 2022, 18:07

Same error and the tooltip only shows when the script is ran.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 19 Apr 2022, 18:31

The error was because you were messing with the clipboard. You need to use another variable, like Area, and it needs to either be passed as a parameter to the function or declared as global like I did. I moved the ToolTip into the hotkey subroutine because I assume that's what you wanted when you said it appeared only once. You can't just put two scripts together and it expect it to know you what you had in mind for when it should appear.

Code: Select all

#SingleInstance, Force
#Include <GDIP_All> ; C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

ToolTipFont("s14", "Comic Sans MS") 
ToolTipColor("green", "white")
return

^+PrintScreen::
ToolTip, Active Window Screenshot, 1525, 0
SetTimer, RemoveToolTip, -500
WinGetPos, X, Y, W, H, A
Clipboard := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Clipboard)
Gdip_SaveBitmapToFile(snap, OutFile)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

RemoveToolTip:
ToolTip
return

; ToolTipOpt v1.004
 
ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 21 Apr 2022, 20:35

Nothing happens when I press the hotkey:

Code: Select all

#SingleInstance, Force
#Include <GDIP_All> ; C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

ToolTipFont("s14", "Comic Sans MS") 
ToolTipColor("green", "white")
return

^+PrintScreen::
ToolTip, Active Window Screenshot, 1525, 0
SetTimer, RemoveToolTip, -500
WinGetPos, X, Y, W, H, A
Area := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
Screenshot(OutFile)
{
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Area)
Gdip_SaveBitmapToFile(snap, OutFile)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

RemoveToolTip:
ToolTip
return

; ToolTipOpt v1.004
 
ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 21 Apr 2022, 20:40

When I press it it shows the tooltip (in the location you specified, relative to the active window) and saves a screenshot of the active window in the script directory. Are you sure you saved the updated script file and closed any other version of it?

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 21 Apr 2022, 20:42

You probably need to change this line:

Code: Select all

#Include <GDIP_All> ; C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
…back to this:

Code: Select all

#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk

I don’t think it would run without errors if you didn’t already change it, though.

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 21 Apr 2022, 20:50

It does work, whether <GDIP_All> or the path, but I still get the error.

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 21 Apr 2022, 21:34

And you’re using the version above, that does not use the clipboard as a variable as you posted in the other thread?

LAPIII
Posts: 668
Joined: 01 Aug 2021, 06:01

Re: Create Screenshot specific area

Post by LAPIII » 21 Apr 2022, 21:53

Gotta share the script with your fix Boiler:

Code: Select all

#SingleInstance, Force
#Include C:\Users\LPIII\Documents\AutoHotkey\Lib\GDIP_All.ahk
SetWorkingDir, C:\Users\LPIII\Pictures\Captures   

ToolTipFont("s14", "Comic Sans MS") 
ToolTipColor("green", "white")
return

^+PrintScreen::
ToolTip, Active Window Screenshot, 1525, 0
SetTimer, RemoveToolTip, -500
WinGetPos, X, Y, W, H, A
Area := "(" X "|" Y "|" W "|" H ")"
FormatTime, DateTime,, d-M-yy h-mm-ss tt
WinGetActiveTitle, Title
Screenshot(Title "_" DateTime ".png")
return

Screenshot(OutFile)
{
global Area
pToken := Gdip_Startup()
snap := Gdip_BitmapFromScreen(Area)
Gdip_SaveBitmapToFile(snap, OutFile)
Gdip_DisposeImage(snap)		;Dispose of the bitmap to free memory.
Gdip_Shutdown( pToken ) 	;Turn off gdip
return
}

RemoveToolTip:
ToolTip
return

; ToolTipOpt v1.004
 
ToolTipFont(Options := "", Name := "", hwnd := "") {
    static hfont := 0
    if (hwnd = "")
        hfont := Options="Default" ? 0 : _TTG("Font", Options, Name), _TTHook()
    else
        DllCall("SendMessage", "ptr", hwnd, "uint", 0x30, "ptr", hfont, "ptr", 0)
}
 
ToolTipColor(Background := "", Text := "", hwnd := "") {
    static bc := "", tc := ""
    if (hwnd = "") {
        if (Background != "")
            bc := Background="Default" ? "" : _TTG("Color", Background)
        if (Text != "")
            tc := Text="Default" ? "" : _TTG("Color", Text)
        _TTHook()
    }
    else {
        VarSetCapacity(empty, 2, 0)
        DllCall("UxTheme.dll\SetWindowTheme", "ptr", hwnd, "ptr", 0
            , "ptr", (bc != "" && tc != "") ? &empty : 0)
        if (bc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1043, "ptr", bc, "ptr", 0)
        if (tc != "")
            DllCall("SendMessage", "ptr", hwnd, "uint", 1044, "ptr", tc, "ptr", 0)
    }
}
 
_TTHook() {
    static hook := 0
    if !hook
        hook := DllCall("SetWindowsHookExW", "int", 4
            , "ptr", RegisterCallback("_TTWndProc"), "ptr", 0
            , "uint", DllCall("GetCurrentThreadId"), "ptr")
}
 
_TTWndProc(nCode, _wp, _lp) {
    Critical 999
   ;lParam  := NumGet(_lp+0*A_PtrSize)
   ;wParam  := NumGet(_lp+1*A_PtrSize)
    uMsg    := NumGet(_lp+2*A_PtrSize, "uint")
    hwnd    := NumGet(_lp+3*A_PtrSize)
    if (nCode >= 0 && (uMsg = 1081 || uMsg = 1036)) {
        _hack_ = ahk_id %hwnd%
        WinGetClass wclass, %_hack_%
        if (wclass = "tooltips_class32") {
            ToolTipColor(,, hwnd)
            ToolTipFont(,, hwnd)
        }
    }
    return DllCall("CallNextHookEx", "ptr", 0, "int", nCode, "ptr", _wp, "ptr", _lp, "ptr")
}
 
_TTG(Cmd, Arg1, Arg2 := "") {
    static htext := 0, hgui := 0
    if !htext {
        Gui _TTG: Add, Text, +hwndhtext
        Gui _TTG: +hwndhgui +0x40000000
    }
    Gui _TTG: %Cmd%, %Arg1%, %Arg2%
    if (Cmd = "Font") {
        GuiControl _TTG: Font, %htext%
        SendMessage 0x31, 0, 0,, ahk_id %htext%
        return ErrorLevel
    }
    if (Cmd = "Color") {
        hdc := DllCall("GetDC", "ptr", htext, "ptr")
        SendMessage 0x138, hdc, htext,, ahk_id %hgui%
        clr := DllCall("GetBkColor", "ptr", hdc, "uint")
        DllCall("ReleaseDC", "ptr", htext, "ptr", hdc)
        return clr
    }
}

feeko
Posts: 51
Joined: 15 Apr 2022, 00:20

Re: Create Screenshot specific area

Post by feeko » 24 May 2022, 09:39

Is it possible to add a clipboard snippet to the saved file in this code so that when the script saves the screenshot it includes whatever text I copied in the saved file?

ie. "(clipboard content) - Test"

I tried adding % clipboard to the script, but it literally just showed "% clipboard - Test" as the saved file.

Code: Select all

#include C:\Users\Owner\Documents\Other\AutoHotkey Scripts\Gdip_All.ahk

^Numpad5::
; Test
pToken := Gdip_Startup()

snap := Gdip_BitmapFromScreen("1400|148|1142|1163")
Gdip_SaveBitmapToFile(snap, "C:\Users\Owner\Desktop\ - Test.png")

fPath := "C:\Users\Owner\Desktop"
Gdip_SaveBitmapToFile(snap,fPath "\ - Test.png")

Gdip_SaveBitmapToFile(snap,A_Desktop "\ - Test.png")

fName	:= "my.txt"
fPath	:=  A_Desktop

Return

User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Create Screenshot specific area

Post by boiler » 24 May 2022, 09:45

I don't see in your posted code where you tried doing that, but it doesn't matter. The answer is no, you can't just add text to it and have it somehow show up along with the image in the saved file. You would have to modify the image to add text to it.

Post Reply

Return to “Ask for Help (v1)”