How to save HDC in memory? So i can quickly restore HDC from memory. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

How to save HDC in memory? So i can quickly restore HDC from memory.

Post by tuzi » 30 Nov 2021, 03:08

i tried this, save hdc to hdc2, but faild, only show black.

Code: Select all

  hdc2    := CreateCompatibleDC()
  hbm2    := Gdip_CreateCompatibleBitmap(hdc2, 100, 100)
  obm2    := SelectObject(hdc2, hbm2)
  BitBlt(hdc2, 0, 0, 100, 100, hdc, 0, 0)
Here is the full test code. Need Gdip_All.ahk

Code: Select all

  Gui, +HwndhWin +E0x80000
  Gui, Show
	
  pToken  := Gdip_Startup()
  hdc     := CreateCompatibleDC()
  hbm     := CreateDIBSection(100, 100)
  obm     := SelectObject(hdc, hbm)
  
  ; draw something in hdc
  G       := Gdip_GraphicsFromHDC(hdc)
  pPen    := Gdip_CreatePen(0xffff0000, 3)
  Gdip_DrawEllipse(G, pPen, 0, 0, 80, 80)
  Gdip_DeletePen(pPen)
  
  ; display hdc
  UpdateLayeredWindow(hWin, hdc, A_ScreenWidth//2, A_ScreenHeight//2, 100, 100)
  
  ; i want to save hdc to hdc2, so i can quickly restore hdc from hdc2 later
  hdc2    := CreateCompatibleDC()
  hbm2    := Gdip_CreateCompatibleBitmap(hdc2, 100, 100)
  obm2    := SelectObject(hdc2, hbm2)
	
  ; try to copy hdc to hdc2, but it faild
  BitBlt(hdc2, 0, 0, 100, 100, hdc, 0, 0)
  
  gosub, displayDC
  
return

displayDC:
	w:=500, h:=150
	Gui, 2:+HwndhWin2
	Gui, 2:Show, w%w% h%h% x0 y0
	
	; source_hdc := hdc         ; success
	source_hdc := hdc2          ; only black
	dest_hdc   := GetDC(hWin2)
  
  BitBlt(dest_hdc, 0, 0, w, h, source_hdc, 0, 0)
return

2GuiClose:
	Gdip_Shutdown(pToken)
	Gdip_DeleteGraphics(G)
	SelectObject(hdc, obm)
	DeleteDC(hdc)
	DeleteObject(hbm)
	
  ExitApp
return

Gdip_CreateCompatibleBitmap(hdc, w, h) {
  return DllCall("gdi32\CreateCompatibleBitmap", "UPtr", hdc, "Int", w, "Int", h)
}

#Include Gdip_All.ahk

teadrinker
Posts: 4326
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to save HDC in memory? So i can quickly restore HDC from memory.

Post by teadrinker » 30 Nov 2021, 06:22

Try hbm2 := CreateDIBSection(100, 100).

User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: How to save HDC in memory? So i can quickly restore HDC from memory.

Post by SpeedMaster » 30 Nov 2021, 06:32

Try hbm2 := Gdip_CreateCompatibleBitmap(hdc, 100, 100)

tuzi
Posts: 223
Joined: 27 Apr 2016, 23:40

Re: How to save HDC in memory? So i can quickly restore HDC from memory.  Topic is solved

Post by tuzi » 30 Nov 2021, 08:21

teadrinker wrote:
30 Nov 2021, 06:22
Try hbm2 := CreateDIBSection(100, 100).
SpeedMaster wrote:
30 Nov 2021, 06:32
Try hbm2 := Gdip_CreateCompatibleBitmap(hdc, 100, 100)
Thanks teadrinker and SpeedMaster, you are both right answer! :thumbup: :thumbup:
I can only choose one answer, so i quote them all, and choose this one hope it can help more people.

Here is full code.

Code: Select all

  Gui, +HwndhWin +E0x80000
  Gui, Show
	
  pToken  := Gdip_Startup()
  hdc     := CreateCompatibleDC()
  hbm     := CreateDIBSection(100, 100)
  obm     := SelectObject(hdc, hbm)
  
  ; draw something in hdc
  G       := Gdip_GraphicsFromHDC(hdc)
  pPen    := Gdip_CreatePen(0xffffff00, 3)
  Gdip_DrawEllipse(G, pPen, 0, 0, 80, 80)
  Gdip_DeletePen(pPen)
  
  ; copy hdc to hdc2
  hdc2    := CreateCompatibleDC()
  hbm2    := CreateCompatibleBitmap(hdc, 100, 100)
  ; hbm2    := CreateDIBSection(100, 100)  ; it also works
  obm2    := SelectObject(hdc2, hbm2)
  BitBlt(hdc2, 0, 0, 100, 100, hdc, 0, 0)
	
  ; draw something in hdc again, so you can see the difference between hdc and hdc2
  pPen    := Gdip_CreatePen(0xffffff00, 3)
  Gdip_DrawRectangle(G, pPen, 0, 0, 80, 80)
  Gdip_DeletePen(pPen)
  
  ; display hdc
  UpdateLayeredWindow(hWin, hdc, A_ScreenWidth//2, A_ScreenHeight//2, 100, 100)
  
  gosub, displayHDC2
  
return

displayHDC2:
	w:=500, h:=150
	Gui, 2:+HwndhWin2
	Gui, 2:Show, w%w% h%h% x0 y0
	
	source_hdc := hdc2
	dest_hdc   := GetDC(hWin2)
  
  ; display hdc2
  BitBlt(dest_hdc, 0, 0, w, h, source_hdc, 0, 0)
return

2GuiClose:
	Gdip_DeleteGraphics(G)
  
	SelectObject(hdc, obm)
	DeleteObject(hbm)
	DeleteDC(hdc)
	
	SelectObject(hdc2, obm2)
	DeleteObject(hbm2)
	DeleteDC(hdc2)
  
  ReleaseDC(dest_hdc)
	
	Gdip_Shutdown(pToken)
  ExitApp
return

CreateCompatibleBitmap(hdc, w, h) {
  return DllCall("gdi32\CreateCompatibleBitmap", "UPtr", hdc, "Int", w, "Int", h)
}

#Include <Gdip_All>

Post Reply

Return to “Ask for Help (v1)”