Printscreening with AHK - Freeze the screen!

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
CosmicThing2
Posts: 19
Joined: 07 Oct 2016, 06:37
Location: UK

Printscreening with AHK - Freeze the screen!

24 Nov 2016, 07:39

Hello all,

So I'm an ICT Technician at a secondary school. We've recently updated our Smart Boards but the software doesn't come with any function to freeze the screens, which the teachers have used for years. Obviously many of them were upset so I went and created an AHK script for freezing the screen. This code is below, this script does work and will run so long as you're using two monitors and both with 1920 x 1080 resolution. Note that people are very welcome to use and take bits of this code if you'd like. It's fairly simple, it may appear to freeze the screen to the end user but in effect, it print screens and then copies this to a GUI and moves the created GUI window (with invisible borders and taskbars) to the correct location on the other screen to appear as if it's frozen.

While unrelated, if anyone has any points about how to improve this code or any issues with it, please let me know, I'd love to improve it. You're welcome to try it out and break it, tell me what I've done awfully.

The issue I'm having... this script works with all of the teachers PC's (all are running Windows 7 32 bit) with the exception of one (which is a 64 bit machine, if this makes a difference). I think it's a technical issue with the code, particularly the lines where I mention I don't understand it! I've never used DllCall up till now and that part is not my code. So when I use the script on this one PC, it freezes as normal but the created image is missing parts... like if you try to freeze the desktop, the image quality is really bad - the fonts are all blurry and the whole wallpaper is just grey on the GUI. Yet if I just press 'prtscn' on my keyboard manually and copy it to paint, the screenshot is fine. Various applications don't work at all with this script, like even Windows Explorer - the text doesn't copy over but the actual interface does. All this happens only on this one individual PC, every other PC works perfectly.

Basically... why!? Is my code wrong?

Thank you!

Code: Select all

#SingleInstance,Force
DetectHiddenWindows,On
open = 0
SetBatchLines -1


F8::
;This section detects whether the monitors are in duplicate or extend. If in duplicate, it forces extend
SysGet, monitorcount, MonitorCount 
if (monitorcount < 2) {
Run C:\Windows\System32\DisplaySwitch.exe /extend
Sleep 2500
}

;If we've already got a frozen screen window open, kill the existing one before opening another
if (open = 1) { 
Gui, Destroy
open = 0
goto next
}

;Press printscreen and retrieve the data from it. Also make sure the program knows the window is now open
next:
if (open = 0)
{
open = 1
send,^{PrintScreen}
Gui,+hwndhwnd
loop,c:\windows\*.bmp,1,1
if file:=A_LoopFileFullPath
break

;Set the window up to have no borders and not appear in the taskbar. (Comment these two lines out for debugging)
Gui, Margin, 0, 0
Gui, -Border -SysMenu -Caption -DPIScale +ToolWindow

;Determine which is the primary monitor. Then get data about the other one (the smartboard)
SysGet, primary, MonitorPrimary 
if (primary = 1)
	SysGet, smart, Monitor, 2
else
	SysGet, smart, Monitor, 1

;Format the picture to fit on the NON primary monitor (The smartboard)
if (smartLeft < 0)  
	Gui,Add,Picture,x-1920 y0 w%A_ScreenWidth% h%A_ScreenHeight%,%file%
else
	Gui,Add,Picture,x0 y0 w%A_ScreenWidth% h%A_ScreenHeight%,%file%

;Not my code! Unsure about this, something to do with the clipboard!
f:=DllCall("User32\OpenClipboard",int,hwnd)
info:=DllCall("User32\GetClipboardData",Uint,8)
handle:=DllCall("User32\GetClipboardData",Uint,2)
f:=DllCall("User32\CloseClipboard",int,hwnd)
SendMessage,0x172,0,handle,Static1,ahk_id%hwnd%

;Need to display the picture differently depending on whether the smartboard is to the left or right of the primary
;If the smartboard is to the left 
if (smartLeft < 0) 
	Gui,Show, x-3840 y0 w3840 h1080
;If the smartboard is to the right
else	           
	Gui, Show, w3840 h1080 x1920 y0

;Finally, maximize the window and then wait for the user to press another button
Gui, maximize
return
}

F9::
;Ask the question. '4' is the type of message box, this one has a yes and a no in it
MsgBox, 4, Force Duplicate Mode?, Would you like to switch to duplicate mode?
IfMsgBox Yes
	Run C:\Windows\System32\DisplaySwitch.exe /clone

;Whether the user pressed yes or no, kill the window and then wait for further input
Gui, Destroy
reload

;In an emergency or if required, kill the whole script with ctrl-shift-F12
^+F12::
ExitApp
vahju
Posts: 29
Joined: 30 Sep 2013, 16:09

Re: Printscreening with AHK - Freeze the screen!

01 Dec 2016, 14:04

I don't know DLL's but its possible that a Windows Theme is causing your issue. Any chance you can set the Windows Theme to Classic or to something the other people are using.

Also its possible that DLL is specific to 32 bit Windows. One of the AHK Gurus will need to reply but its possible you may need to check what bit version people are using and adjust the DLL code from there.

Wish I was able to provide more help.
User avatar
aztec3
Posts: 177
Joined: 07 Apr 2014, 12:05

Re: Printscreening with AHK - Freeze the screen!

01 Dec 2016, 15:08

CosmicThing2,
here's an interesting post, also along the lines of vahju questioning whether this is a DLL specific to 32 bit Windows. In a nutshell, what if you install the 32 bit version of AutoHotKey_L on the 64 bit system?

https://autohotkey.com/board/topic/7624 ... liability/
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Printscreening with AHK - Freeze the screen!

01 Dec 2016, 15:18

Let's skip that problem all together, shall we?
Requires Gdip_All

Code: Select all

#singleInstance force
#persistent
#include *i <Lib_1>
#include <Gdip_All>
onExit("quit")

if(!(p:=Gdip_Startup())){
    msgbox,,Error,Couldn't start GDI+
    exitApp
}
gui,+hwndghwnd -caption +alwaysontop +toolWindow
gui,add,picture,vmp 0xE
guiControlGet,chwnd,hwnd,mp
return

F5::
gui,cancel
bfs:=Gdip_BitmapFromScreen(0) ; number indicates which screen
guiControl,move,mp,% "x0 y0 w" Gdip_GetImageWidth(bfs) . " h" . Gdip_GetImageHeight(bfs)
hfb:=Gdip_CreateHBITMAPFromBitmap(bfs)
SetImage(chwnd,hfb)
gui,show,% "x0 y0 w" . Gdip_GetImageWidth(bfs) . " h" . Gdip_GetImageHeight(bfs)

Gdip_DisposeImage(bfs)
Gdip_DisposeImage(hfb)
return

F6::gui,cancel

quit(){
    Gdip_Shutdown(p)
    exitApp
}
You'll have to edit it to be multi-screen usable, as I set it up to work for only one. Shouldn't be very difficult, considering you already have bits to do such.
Last edited by Masonjar13 on 01 Dec 2016, 15:23, edited 1 time in total.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Printscreening with AHK - Freeze the screen!

01 Dec 2016, 15:20

You might want to clear the clipboard before you send printscreen, and then use Clipwait.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo and 124 guests