Gui Crosshair, Gui Close Function

Get help with using AutoHotkey and its commands and hotkeys
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Gui Crosshair, Gui Close Function

24 Oct 2017, 11:32

Hey guys,
I cant figure out how to make my cross hairs work after initial initiation of the application i am developing.
At start its loads fine, but when i click to pause the area of zoom and then start it again the cross hairs don't appear. Also the issue is flicker cross hairs, but i am not sure if that can be avoidable.

My script should operate like this:
When you launch it ask for source window, works on game windows best(calculator seams to not work with my app), then once you give the proper window name it opens 3 guis. One a copy of the whole window, 2nd is the panel for my app. Inside the panel there is a frame to hold the maximized refreshing image of where the mouse is/should be of the chosen window. It uses the same bitmap that's a copy of the real chosen window just used differently in a different gui. And is meant to have cross hairs while the frame is active. There is a button OK to initiate that frame if it is stopped. When you click left mouse button on the full window copy gui the zoom frame freezes till you click OK button.

How it actually works:
gets name of the window(no validation yet), when name is correct if fills the full copy gui and initiates the zoom frame with cross hair like it should. reacts to left click appropriately. But when its initiated again it fails to draw the cross hairs in the zoom frame.

Code: Select all

#SingleInstance, Force
#NoEnv
SetBatchLines, -1

#Include Gdip_All.ahk 
CoordMode Mouse, Screen
pToken := Gdip_Startup()

global magWinSide := 256, winName, winX, winY, winW, winH
global srcPrintFrame, destPrintFrame, destFullFrame

InputBox, winName,, Please Enter The Window Title
winSetup()
activate()

winSetup()
{	
	WinGet, prnSrcID, ID, % winName
	WinGetPos, winX, winY, winW, winH, % winName
	
	Gui, PnCfgMain: +AlwaysOnTop 
	
	Gui, PnCfgMain: Show, % "x" 0 " y" 0 " w" magWinSide " h" magWinSide + 400, PaneConfigMain
		
	Gui, PnCfgMain: Add, Button, % "y+" magWinSide " gactivate Default ", OK 

	WinGet, pnCfgMainID, ID, PaneConfigMain
	WinGet pnCfgMainID, ID, PaneConfigMain
	Gui, PnCfgSrc: -Border -Caption
	
	Gui, PnCfgSrc: Show , % "w" winW " h" winH " x" 0 " y" 0, PaneConfigSource
		
	WinGet pnCfgSrcID, ID, PaneConfigSource

	srcPrintFrame := GetDC(prnSrcID)
	destPrintFrame := GetDC(pnCfgMainID)
	destFullFrame := GetDC(pnCfgSrcID)
	
	StretchBlt(destFullFrame, 0, 0, winW, winH, srcPrintFrame, 0, 0, winW, winH, 0xCC0020)
	
}

activate()
{
	zoom := 16
	zSide := magWinSide / zoom
	
	Loop
	{
		MouseGetPos, x, y
		x -= zSide/2
		y -= zSide/2
		If (x=x_old) && (y=y_old)
			Continue
		x_old:=x, y_old:=y
		
		StretchBlt(destPrintFrame, 0, 0, magWinSide, magWinSide, srcPrintFrame, x, y, zSide, zSide, 0xCC0020)
			
		crshDIBS := CreateDIBSection(magWinSide, magWinSide)
		crshRegObj := SelectObject(destPrintFrame, crshDIBS)
		destGpxDCP := Gdip_GraphicsFromHDC(destPrintFrame)
		pen1 := Gdip_CreatePen(0x660000ff, 10)
				
		Gdip_DrawLine(destGpxDCP, pen1, 0, magWinSide/2, magWinSide, magWinSide/2)
		Gdip_DrawLine(destGpxDCP, pen1, magWinSide/2, 0, magWinSide/2, magWinSide)
		
		GetKeyState, state, LButton
		if state = D
		{
			break
		}
	}
}

PnCfgMainGuiClose()
{
	ExitApp
}



Gdip_Shutdown(pToken)
Last edited by clow_yakayi on 25 Oct 2017, 08:51, edited 5 times in total.
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: Gui Crosshair, Gui Close Function

24 Oct 2017, 13:48

How come no one is responding. Am I missing something? What should I do
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Gui Crosshair, Gui Close Function

24 Oct 2017, 21:42

I don't have the GDIP library so I'm probably not the best person to answer this. In fact, I don't have a particular answer. But I wonder about what this "zoomAct" variable is doing. You set it to be 1, and everytime you do a left click -- assuming the script gets to that point again after attempting to restart it -- then zoomAct decreases by 1. So it goes 1, 0 (for sure), and then -1, -2, -3 (maybe).

You may be able to avoid or minimize flickering by putting in your loop a check if the mouse has moved since last time. It can be done something like this:

Code: Select all

Loop
{
MouseGetPos, x, y
If (x=x_old) && (y=y_old)
   Continue ; it will skip past this if either x or y have changed from last time
x_old:=x, y_old:=y ; and we capture their values right away
; ...
}
This should make the crosshair be redrawn only when the mouse moves, which should be a little less annoying.

(As an aside, you can post code on the forums by using code tags.)

For doing a GuiClose, it looks like you should have it just fine: https://autohotkey.com/docs/commands/Gui.htm#Labels

What you have there should work just fine. Defining a GuiClose() function, without a GuiClose: label is all you need. You shouldn't need to include anything like the OnExit command to point to the GuiClose() function. It seems the GuiClose() function (and label) would be reserved names that are triggered automatically, if they exist, upon the Gui being closed.
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: Gui Crosshair, Gui Close Function

24 Oct 2017, 22:09

Thank you for the rare response ^^. I understand the same about GuiClose, but the x button does not initiate the function. I shall try that with a flicker effect. and i will post my code in a second. and i think zoomAct is just no longer something i use since i have functions
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: Gui Crosshair, Gui Close Function

24 Oct 2017, 23:10

Ok the flicker stops when mouse movement stops, but flickers during movement. and removed zoomAct. It was redundant
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: Gui Crosshair, Gui Close Function

24 Oct 2017, 23:25

Ok I figured out the close issue. GuiClose needs to have the name before GuiClose
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: Gui Crosshair, Gui Close Function

24 Oct 2017, 23:28

Well, you figured it out as I was typing this up. So glad you did there. Skip ahead! A little more thinking on the GUIClose() issue. You need to prefix it with the GUI name. You're using PnCfgMain for the GUI name, so you should have the PnCfgMainGuiClose() function. I just tested it with this:

Code: Select all

^1::
Gui, Brandi:New
Gui, Brandi:Default
Gui, Add, Edit, vcat1
Gui, Add, Edit, vcat2
Gui, Add, Edit, vcat3
Gui, Add, Edit, vcat4
Gui, Add, Edit, vcat5
Gui, Add, Button, gCatSearch, Search
Gui, Add, Text, r20 w700 vResults
Gui, Show
return

CatSearch:
return

BrandiGuiClose(){
MsgBox Brandi Closed
}

GuiClose(){
MsgBox Unnamed GUI
}
Took me a while to realize that as I don't normally name my GUIs.

More in regards to the flickering, you may consider moving the GUI around - maybe, again, unfamiliar with the GDIP functions - instead of redrawing the lines or what is going on. But I'd still expect flickering from movement, but possibly less so? Another possible solution is to slow the update period so it does not flicker as fast. Throwing in a short Sleep in your loop.

But that still leaves the issue of why your function isn't restarting after you stop it... Hmmmmmmm, how does it launch? Does it wait for the activate() function to be called by you pressing the button in the GUI? If it launches on it's own, is it at all possible that when you are clicking the GUI button that the script moves so fast it gets to the GetKeyState command and then ends the loop? You could probably put in a Tooltip before the Break for the sake of checking to give you visual notice that the code did indeed execute.

If that tooltip weren't to appear, it would sound like the activate() function isn't being executed. I haven't used functions as my target labels with glabels before. It says you should be able to use it in that documentation link though. And in my testing you can...

Beyond that, I'm at a loss and it may require someone who knows about this library to spot an error.
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: Gui Crosshair, Gui Close Function

25 Oct 2017, 00:05

Lol why don't you try running my script, https://github.com/Clowcadia/CZoom. Just go there and get both the ahk files. one is GDIP, its good to have, once i got it i got excited with programming possibilities. Easy read as well.

My script should operate like this:
When you launch it ask for source window, works on game windows best(calculator seams to not work with my app), then once you give the proper window name it opens 3 guis. One a copy of the whole window, 2nd is the panel for my app. Inside the panel there is a frame to hold the maximized refreshing image of where the mouse is/should be of the chosen window. It uses the same bitmap that's a copy of the real chosen window just used differently in a different gui. And is meant to have cross hairs while the frame is active. There is a button OK to initiate that frame if it is stopped. When you click left mouse button on the full window copy gui the zoom frame freezes till you click OK button.

How it actually works:
gets name of the window(no validation yet), when name is correct if fills the full copy gui and initiates the zoom frame with cross hair like it should. reacts to left click appropriately. But when its initiated again it fails to draw the cross hairs in the zoom frame.

Unfortunately those who know GDIP seams to not be around to help me understand more about the mechanics behind building, Device Contexts, Bitmaps, Graphics, Pens and such. Because do to specific functions and refreshing loops that I have. I am not sure if I should create any object once or and delete them at the end, or should I create and delete those objects for every refresh.
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: Gui Crosshair, Gui Close Function

25 Oct 2017, 08:54

You better put your question in the gdip forum to find help from experts , all gdip freaks are gathering there when they are bored .......

https://autohotkey.com/boards/viewtopic.php?f=6&t=6517
soundcloud.com/user-32706894

Return to “Ask For Help”

Who is online

Users browsing this forum: AHKStudent, CheshireCat, flyingDman, Google [Bot], Gully and 71 guests