GDIP magnifier

Post your working scripts, libraries and tools
scuttlegun

GDIP magnifier

07 Mar 2017, 10:28

I'm sure this is re-inventing the wheel on here, but I couldn't find something that did exactly what I wanted so made this. Much thanks to tic for his great library, hope this can help someone else!

Code: Select all

#LTrim
#NoEnv
#SingleInstance Force
SetBatchLines, -1
CoordMode, Mouse

#Include C:\Portable\AutoHotkey\Lib\Gdip.ahk ;; https://autohotkey.com/boards/viewtopic.php?t=6517

if (!pToken := Gdip_Startup())
  ExitApp

activation_key := "CapsLock"

zoom_step := 1.1

display_width  := 300
display_height := 200

capture_width  := display_width
capture_height := display_height

Hotkey, %activation_key%, zoom
OnExit, Exit
return

#if (GetKeyState(activation_key, "P"))

WheelUp::capture_width /= zoom_step, capture_height /= zoom_step
WheelDown::capture_width *= zoom_step, capture_height *= zoom_step

zoom:
  Gui, -Caption +E0x00080020 +LastFound +AlwaysOnTop +ToolWindow +OwnDialogs
  Gui, Show, NA
  hwnd := WinExist()
  hbm := CreateDIBSection(display_width, display_height)
  hdc := CreateCompatibleDC()
  obm := SelectObject(hdc, hbm)
  G := Gdip_GraphicsFromHDC(hdc)
  Gdip_SetInterpolationMode(G, 7)
  pPen := Gdip_CreatePen(0xFF000000,1)
  pBrush := Gdip_BrushCreateHatch(0xFFFFFFFF, 0xFF000000, 38) ;; HatchStyleDiagonalBrick
  Loop
  {
    MouseGetPos, mX, mY, mWin
    WinGetPos, X, Y, W, H, ahk_id %mWin%
    pBitmap := Gdip_BitmapFromHwnd2(mWin, x1 := mX-X-capture_width/2, y1 := mY-Y-capture_height/2, capture_width, capture_height)
    G2 := Gdip_GraphicsFromImage(pBitmap)
    if (x1 < 0)
      Gdip_FillRectangle(G2, pBrush, 0, 0, -x1, capture_height)
    if (y1 < 0)
      Gdip_FillRectangle(G2, pBrush, 0, 0, capture_width, -y1)
    if (W < x1+capture_width)
      Gdip_FillRectangle(G2, pBrush, -x1+W, 0, capture_width-x1+W, capture_height)
    if (H < y1+capture_height)
      Gdip_FillRectangle(G2, pBrush, 0, -y1+H, capture_width, capture_height-y1+H)
    Gdip_DeleteGraphics(G2)
    Gdip_DrawImage(G, pBitmap, 0, 0, display_width, display_height, 0, 0, capture_width, capture_height)
    Gdip_DrawRectangle(G, pPen, 0, 0, display_width-1, display_height-1)
    UpdateLayeredWindow(hwnd, hdc, mX-display_width/2, mY-display_height/2, display_width, display_height)
    Gdip_DisposeImage(pBitmap)
  }
  Until (!GetKeyState(activation_key, "P"))
  Gdip_DeleteBrush(pBrush)
  Gdip_DeletePen(pPen)
  SelectObject(hdc, obm)
  DeleteObject(hbm)
  DeleteDC(hdc)
  Gdip_DeleteGraphics(G)
  Gui, Destroy
return

Exit:
  Gdip_Shutdown(pToken)
  ExitApp
Return

Gdip_BitmapFromHwnd2(hWnd, x=0,y=0, w=0,h=0) {
  if (!w || !h)
    WinGetPos,,, w,h, ahk_id %hWnd%
  hhdc := GetDCEx(hWnd, 3)
  chdc := CreateCompatibleDC()
  hbm := CreateDIBSection(w,h, chdc)
  obm := SelectObject(chdc, hbm)
  BitBlt(chdc, 0,0, w,h, hhdc, x,y)
  ReleaseDC(hhdc)
  pBitmap := Gdip_CreateBitmapFromHBITMAP(hbm)
  SelectObject(chdc, obm)
  DeleteObject(hbm)
  DeleteDC(hhdc)
  DeleteDC(chdc)
  return pBitmap
}
Helgef
Posts: 4467
Joined: 17 Jul 2016, 01:02
Contact:

Re: GDIP magnifier

07 Mar 2017, 17:15

It works smoothly :thumbup:
The next step would be to make it work on the entire screen, rather than the window under the mouse.

Thanks for sharing.
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: GDIP magnifier

12 Mar 2017, 02:55

Image

Without interpolation it seems slightly better in high manification.
Very nice ( and surpise it is smooth).
soundcloud.com/user-32706894
carno
Posts: 212
Joined: 20 Jun 2014, 16:48

Re: GDIP magnifier

16 Mar 2017, 14:50

Thanks! One of the best.
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: GDIP magnifier

17 Mar 2017, 17:05

Thank you, scuttlegun, this an excellent script indeed.
burque505
Posts: 1397
Joined: 22 Jan 2017, 19:37

Re: GDIP magnifier

29 Mar 2017, 11:41

I switched to 64-bit AHK and it wouldn't run correctly, so I added Joe Glines's code from https://autohotkey.com/boards/viewtopic.php?f=6&t=12088 at the top:

Code: Select all

if((A_PtrSize=8&&A_IsCompiled="")||!A_IsUnicode){ ;32 bit=4  ;64 bit=8
    SplitPath,A_AhkPath,,dir
    if(!FileExist(correct:=dir "\AutoHotkeyU32.exe")){
	    MsgBox error
	    ExitApp
    }
    Run,"%correct%" "%A_ScriptName%",%A_ScriptDir%
    ExitApp
    return
}
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: GDIP magnifier

21 Oct 2017, 10:15

How could u see the exact pixels in the screen
and is that use windows dc and stretchbild, and how to use those instead
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: GDIP magnifier

21 Oct 2017, 13:42

Bump
clow_yakayi
Posts: 39
Joined: 15 Oct 2017, 10:15

Re: GDIP magnifier

21 Oct 2017, 18:25

OK Ive tried to incorporate what I learned from creating rectangles and classes. But I cant seem to combine the magnifier with a rectangle I don't understand .
I hope I am not being ignored

Code: Select all

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

zoom := New CZoom
zoom.open()
;zoom := 0

Class CZoom{
	;;;;;;;;;;;;;;;;;;;;;;;
	;CONSTRUCTOR ZOOM GUI
	;;;;;;;;;;;;;;;;;;;;;;;
	__New()
	{
		this.zoom := 8
		this.side := 160
		this.zoomSide := this.side / this.zoom
		this.srcCopy := 0xCC0020 ;raster operation code
		; Gui Zoom:+AlwaysOnTop -Border -Caption +ToolWindow +E0x00080020
		; Gui Zoom:Show, % "w" this.side " h" this.side " x" 0 "y" 0, Zoom
		;WinGet zoomID, ID, Zoom
		; WinSet Transparent, 255, Zoom 
		WinGet printSrcID, ID
		this.printSrcWin := GetDC(printSrcID)
		;this.zoomWin := GetDC(zoomID)
		
		Gui, Zoom: +ToolWindow +OwnDialogs +AlwaysOnTop +LastFound -Caption +E0x80000 
		Gui, Zoom: Show, NA
		WinGet zoomID, ID, Zoom
		;this.zoomWin := GetDC(zoomID)
		this.winID := WinExist() ;Gets the ID of +LastFound Win, Zoom
		
		EmptyMem()
	}
	
	;;;;;;;;;;;;;;;;;;;;;;;
	;DESTRUCTOR ZOOM GUI
	;;;;;;;;;;;;;;;;;;;;;;;
	__Delete()
	{
		; this.zoom := 0
		; this.side := 0
		; this.zoomSide := 0
		; this.srcCopy := 0
		; this.printSrcWin := 0
		; this.zoomWin := 0
		; VarSetCapacity(this.zoom, 0)
		; VarSetCapacity(this.side, 0)
		; VarSetCapacity(this.zoomSide, 0)
		; VarSetCapacity(this.srcCopy, 0)
		; VarSetCapacity(this.printSrcWin, 0)
		; VarSetCapacity(this.zoomWin, 0)
	}
	
	get()
	{
		;MsgBox % "zoom " this.zoom " side " this.side " zoom " this.zoomSide " printSrcWin " this.printSrcWin " zoomWin " this.zoomWin 
	}

	;;;;;;;;;;;;;;;;;;;;;;;
	;UPDATE ZOOM GUI
	;;;;;;;;;;;;;;;;;;;;;;;
	open()
	{
		; Loop
		; {
			; MouseGetPos x, y
			; Gui Zoom: +Lastfound
			; StretchBlt(this.zoomWin, 0, 0, this.side, this.side, this.printSrcWin, x, y, this.zoomSide, this.zoomSide, this.srcCopy)

			; zoomLocY := y - this.side   
			; zoomLocX := x - this.side
			; WinMove Zoom,,% zoomLocX, % zoomLocY

			; GetKeyState, state, Esc
			; if state = D
			; {
				; WinKill, Zoom
				; break
			; }
		; }
		
		
		Loop
		{
			
			MouseGetPos, cursX, cursY
			x := cursX- (this.side / 2)
			y := cursY- (this.side / 2)
			
			handleDevContext := CreateCompatibleDC()
			handleBitmap := CreateDIBSection(this.side, this.side)
			rectRegionObj := SelectObject(handleDevContext, handleBitmap)
			graphicDCPointer := Gdip_GraphicsFromHDC(handleDevContext)
			pen := Gdip_CreatePen(0x660000ff, 10)
			Gdip_DrawRectangle(graphicDCPointer, pen, -1, -1, this.side+1, this.side+1)
			
			StretchBlt(this.winID, 0, 0, this.side, this.side, this.printSrcWin, x, y, this.zoomSide, this.zoomSide, this.srcCopy)
			zoomLocY := y - this.side   
			zoomLocX := x - this.side
			WinMove Zoom,,% zoomLocX, % zoomLocY
			
			Gdip_DeletePen(pen)
			UpdateLayeredWindow(this.winID, handleDevContext, x, y, this.side, this.side)
			;UpdateLayeredWindow(this.zoomWin, handleDevContext, x, y, this.side, this.side)
			SelectObject(handleDevContext, rectRegionObj)
			DeleteObject(handleBitmap)
			DeleteDC(handleDevContext)
			DeleteObject(rectRegionObj)
			Gdip_DeleteGraphics(graphicDCPointer)
			DeleteObject(graphicDCPointer)
			
			GetKeyState, state, LButton
			if state = D
			{
				x := Floor(x)
				y := Floor(y)
				Gui, Zoom: Destroy
				break
			}
		}
		
		EmptyMem()
	}
}

Gdip_Shutdown(pToken)
User avatar
noname
Posts: 509
Joined: 19 Nov 2013, 09:15

Re: GDIP magnifier

22 Oct 2017, 06:32

I am not familiar with classes but here is the "classic" magnifier code using StretchBlt from Holomind hope it helps ..... ( it still works on win10 :o )

https://autohotkey.com/board/topic/1066 ... magnifier/
soundcloud.com/user-32706894

Return to “Scripts and Functions”

Who is online

Users browsing this forum: ewerybody, robodesign and 16 guests