Clearing a drawing made with Gdip ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Clearing a drawing made with Gdip ?

18 Sep 2021, 17:41

Here is a stripped down version of code, all I am trying to do is to click the button again and have the drawing cleared and the red line drawn again. I spent hours trying so many things that it would take a day just to share every possible combo I tried. I reviewed tics both posts, I reviewed at least 10 threads, nothing worked for me.

Code: Select all

#Include, Gdip.ahk

#SingleInstance, Force
SetBatchlines, -1
GDIP_StartUp()
gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE +BackgroundTrans hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 
pBitmap := Gdip_CreateBitmap( 400 , 400)
G := Gdip_GraphicsFromImage( pBitmap )
Gui, 1:Show, w400 h400, Demo
return

again:
; what code is needed here or within the function to remove the red line (drawing) and draw again. I tried graphicsClear and some other methods but nothing worked for me
drawOne( PicHwnd , pBitmap, G)
return

drawOne( hwnd , pBitmap, G){
	Gdip_SetSmoothingMode( G , 2 )
	Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
	Gdip_DrawLine( G , Pen , 49, 49, 300, 175)
	hBitmap := Gdip_CreateHBITMAPFromBitmap( pBitmap )
	SetImage( hwnd , hBitmap )
	; what code is added here that makes sense? I see some have Gdip_DeletePen( Pen ), DisposeImage etc?
}
GuiClose:
esc:
Gdip_Shutdown(pToken)
ExitApp
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 18:34

Before you draw something new, call this

Gdip_GraphicsClear( G )

You have a few other things that can be modified too, give me a few mins and I'll try to get you something.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 18:37

You also might want to check this out.

viewtopic.php?f=6&t=61911&p=281049

I use that pretty much every time I work with gdip.

I have been meaning to update it with a search filter, but that is super boring for me so I don't know if/when I will ever do it.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 18:43

Hellbent wrote:
18 Sep 2021, 18:34
Before you draw something new, call this

Gdip_GraphicsClear( G )

You have a few other things that can be modified too, give me a few mins and I'll try to get you something.
Thanks, I tried that but its not removing the red line, I tried it in the function and even before calling the func.

Code: Select all

#Include, Gdip.ahk

#SingleInstance, Force
SetBatchlines, -1
GDIP_StartUp()
gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE +BackgroundTrans hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 
pBitmap := Gdip_CreateBitmap( 400 , 400)
G := Gdip_GraphicsFromImage( pBitmap )
Gui, 1:Show, w400 h400, Demo
return

again:
Gdip_GraphicsClear( G )
sleep, 1000
; what code is needed here or within the function to remove the red line (drawing) and draw again. I tried grahicsClear and some other methos but nothing worked for me
drawOne( PicHwnd , pBitmap, G)
return

drawOne( hwnd , pBitmap, G){
	Gdip_GraphicsClear( G )
	sleep, 1000
	Gdip_SetSmoothingMode( G , 2 )
	Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
	Gdip_DrawLine( G , Pen , 49, 49, 300, 175)
	hBitmap := Gdip_CreateHBITMAPFromBitmap( pBitmap )
	SetImage( hwnd , hBitmap )
	; what code is added here that makes sense? I see some have Gdip_DeletePen( Pen ), DisposeImage etc?
}
GuiClose:
esc:
Gdip_Shutdown(pToken)
ExitApp
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 18:54

AHKStudent wrote:
18 Sep 2021, 18:43
Hellbent wrote:
18 Sep 2021, 18:34
Before you draw something new, call this

Gdip_GraphicsClear( G )

You have a few other things that can be modified too, give me a few mins and I'll try to get you something.
Thanks, I tried that but its not removing the red line, I tried it in the function and even before calling the func.

Code: Select all

#Include, Gdip.ahk

#SingleInstance, Force
SetBatchlines, -1
GDIP_StartUp()
gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE +BackgroundTrans hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 
pBitmap := Gdip_CreateBitmap( 400 , 400)
G := Gdip_GraphicsFromImage( pBitmap )
Gui, 1:Show, w400 h400, Demo
return

again:
Gdip_GraphicsClear( G )
sleep, 1000
; what code is needed here or within the function to remove the red line (drawing) and draw again. I tried grahicsClear and some other methos but nothing worked for me
drawOne( PicHwnd , pBitmap, G)
return

drawOne( hwnd , pBitmap, G){
	Gdip_GraphicsClear( G )
	sleep, 1000
	Gdip_SetSmoothingMode( G , 2 )
	Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
	Gdip_DrawLine( G , Pen , 49, 49, 300, 175)
	hBitmap := Gdip_CreateHBITMAPFromBitmap( pBitmap )
	SetImage( hwnd , hBitmap )
	; what code is added here that makes sense? I see some have Gdip_DeletePen( Pen ), DisposeImage etc?
}
GuiClose:
esc:
Gdip_Shutdown(pToken)
ExitApp
Remove +BackgroundTrans from the picture
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?  Topic is solved

18 Sep 2021, 18:57

@AHKStudent

Trying working through this and see if you have any questions. (random() was only for testing on my end)

Code: Select all

#Include <My Altered GDIP lib>
#SingleInstance, Force
SetBatchlines, -1

GDIP_StartUp()

gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 

MyBitmapObject := {}
MyBitmapObject.pBitmap := Gdip_CreateBitmap( 400 , 400)
MyBitmapObject.G := Gdip_GraphicsFromImage( MyBitmapObject.pBitmap )
MyBitmapObject.Smoothing := 2
Gdip_SetSmoothingMode( MyBitmapObject.G , MyBitmapObject.Smoothing ) 
MyBitmapObject.Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
MyBitmapObject.ControlHwnd := PicHwnd

Gui, 1:Show, w400 h400, Demo
return

again:
	Gdip_GraphicsClear( MyBitmapObject.G )
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject.pBitmap )
	SetImage( MyBitmapObject .ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

drawOne( obj ){
	Gdip_DrawLine( obj.G , obj.Pen , Random(1,300), Random(1,300), 300, 175)
}

Random(Min,Max){
	local out
	Random, out, min , max
	return out
}

GuiClose:
esc:
Gdip_DeleteGraphics( MyBitmapObject.G )
Gdip_DisposeImage( MyBitmapObject.pBitmap )
Gdip_DeletePen( MyBitmapObject.Pen )
Gdip_Shutdown(pToken)
ExitApp
**EDIT** Added a few things to GuiClose:
Last edited by Hellbent on 19 Sep 2021, 08:44, edited 2 times in total.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 19:05

Hellbent wrote:
18 Sep 2021, 18:57
@AHKStudent

Trying working through this and see if you have any questions. (random() was only for testing on my end)

Code: Select all

#Include <My Altered GDIP lib>
#SingleInstance, Force
SetBatchlines, -1

GDIP_StartUp()

gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 

MyBitmapObject := {}
MyBitmapObject.pBitmap := Gdip_CreateBitmap( 400 , 400)
MyBitmapObject.G := Gdip_GraphicsFromImage( MyBitmapObject.pBitmap )
MyBitmapObject.Smoothing := Gdip_SetSmoothingMode( MyBitmapObject.G , 2 ) 
MyBitmapObject.Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
MyBitmapObject.ControlHwnd := PicHwnd

Gui, 1:Show, w400 h400, Demo
return

again:
	Gdip_GraphicsClear( MyBitmapObject.G )
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject .pBitmap )
	SetImage( MyBitmapObject .ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

drawOne( obj ){
	Gdip_DrawLine( obj.G , obj.Pen , Random(1,300), Random(1,300), 300, 175)
}

Random(Min,Max){
	local out
	Random, out, min , max
	return out
}

GuiClose:
esc:
Gdip_DeleteGraphics( MyBitmapObject.G )
Gdip_DisposeImage( MyBitmapObject.pBitmap )
Gdip_DeletePen( MyBitmapObject.Pen )
Gdip_Shutdown(pToken)
ExitApp
**EDIT** Added a few things to GuiClose:
this is working perfect, ill take time to study this, thank you
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 19:08

AHKStudent wrote:
18 Sep 2021, 19:05

this is working perfect, ill take time to study this, thank you
i made a few edits, get a new copy
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 19:14

Hellbent wrote:
18 Sep 2021, 19:08
AHKStudent wrote:
18 Sep 2021, 19:05

this is working perfect, ill take time to study this, thank you
i made a few edits, get a new copy
tested more carefully, it seems not to remove the red line before it draws the new one, i added a sleep to test

Code: Select all

#Include <My Altered GDIP lib>
#SingleInstance, Force
SetBatchlines, -1

GDIP_StartUp()

gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 

MyBitmapObject := {}
MyBitmapObject.pBitmap := Gdip_CreateBitmap( 400 , 400)
MyBitmapObject.G := Gdip_GraphicsFromImage( MyBitmapObject.pBitmap )
MyBitmapObject.Smoothing := Gdip_SetSmoothingMode( MyBitmapObject.G , 2 ) 
MyBitmapObject.Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
MyBitmapObject.ControlHwnd := PicHwnd

Gui, 1:Show, w400 h400, Demo
return

again:
	Gdip_GraphicsClear( MyBitmapObject.G )
	sleep, 3000
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject.pBitmap )
	SetImage( MyBitmapObject .ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

drawOne( obj ){
	Gdip_DrawLine( obj.G , obj.Pen , Random(1,300), Random(1,300), 300, 175)
}

Random(Min,Max){
	local out
	Random, out, min , max
	return out
}

GuiClose:
esc:
Gdip_DeleteGraphics( MyBitmapObject.G )
Gdip_DisposeImage( MyBitmapObject.pBitmap )
Gdip_DeletePen( MyBitmapObject.Pen )
Gdip_Shutdown(pToken)
ExitApp
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 19:16

@Hellbent

I see what was missing, an update after clearing, does this make sense?

Code: Select all

#Include <My Altered GDIP lib>
#SingleInstance, Force
SetBatchlines, -1

GDIP_StartUp()

gui, color, white
Gui, 1:+AlwaysOnTop
Gui, 1:Add, Picture, xm ym 0xE hwndPicHwnd
Gui, 1:Add, Button, x100 y350 gagain, Earse and draw again 

MyBitmapObject := {}
MyBitmapObject.pBitmap := Gdip_CreateBitmap( 400 , 400)
MyBitmapObject.G := Gdip_GraphicsFromImage( MyBitmapObject.pBitmap )
MyBitmapObject.Smoothing := Gdip_SetSmoothingMode( MyBitmapObject.G , 2 ) 
MyBitmapObject.Pen := Gdip_CreatePen( "0xFFd50000" , 5 ) 		
MyBitmapObject.ControlHwnd := PicHwnd

Gui, 1:Show, w400 h400, Demo
return

again:
	Gdip_GraphicsClear( MyBitmapObject.G )
	SetImage( MyBitmapObject .ControlHwnd , hBitmap )
	sleep, 3000
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject.pBitmap )
	SetImage( MyBitmapObject .ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

drawOne( obj ){
	Gdip_DrawLine( obj.G , obj.Pen , Random(1,300), Random(1,300), 300, 175)
}

Random(Min,Max){
	local out
	Random, out, min , max
	return out
}

GuiClose:
esc:
Gdip_DeleteGraphics( MyBitmapObject.G )
Gdip_DisposeImage( MyBitmapObject.pBitmap )
Gdip_DeletePen( MyBitmapObject.Pen )
Gdip_Shutdown(pToken)
ExitApp
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 19:23

AHKStudent wrote:
18 Sep 2021, 19:14

tested more carefully, it seems not to remove the red line before it draws the new one, i added a sleep to test
That has to do with what we talked about in pms. Look up the part about the graphics again.
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 19:25

AHKStudent wrote:
18 Sep 2021, 19:16
@Hellbent

I see what was missing, an update after clearing, does this make sense?
Yep. Just have to fix the typos. obj. key
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 20:48

@AHKStudent

Something to note.

In the case of this

Code: Select all

again:
	Gdip_GraphicsClear( MyBitmapObject.G )
	SetImage( MyBitmapObject.ControlHwnd , hBitmap )
	sleep, 3000
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject.pBitmap )
	SetImage( MyBitmapObject.ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

In the first call to "SetImage", the variable hBitmap is empty. As you can see it works fine in this case, but there are other methods available to change the background.

Just wanted to point out the null value there.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Clearing a drawing made with Gdip ?

18 Sep 2021, 23:23

Hellbent wrote:
18 Sep 2021, 20:48
@AHKStudent

Something to note.

In the case of this

Code: Select all

again:
	Gdip_GraphicsClear( MyBitmapObject.G )
	SetImage( MyBitmapObject.ControlHwnd , hBitmap )
	sleep, 3000
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject.pBitmap )
	SetImage( MyBitmapObject.ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

In the first call to "SetImage", the variable hBitmap is empty. As you can see it works fine in this case, but there are other methods available to change the background.

Just wanted to point out the null value there.
is it better to do with a 0

Code: Select all

SetImage( MyBitmapObject.ControlHwnd , 0 )
It works but is there a better way?

edit

how about

Code: Select all

again:
	if hBitmap {
	Gdip_GraphicsClear( MyBitmapObject.G )
	SetImage( MyBitmapObject.ControlHwnd , 0 )
	sleep, 1000
	}
	drawOne( MyBitmapObject )
	hBitmap := Gdip_CreateHBITMAPFromBitmap( MyBitmapObject.pBitmap )
	SetImage( MyBitmapObject.ControlHwnd , hBitmap )
	DeleteObject( hBitmap  )
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 117 guests