Sign in to follow this  
Followers 0
adamski

Double Buffered GDI with Buttons

3 posts in this topic

I have been experimenting with monoceres excellent double buffering template script. All the GDI drawing is fine but I am unable to put any functioning buttons on the gui window. They are displayed but do nothing when clicked. The (very) simple script demonstrates this.

I'd be grateful for a little help. Did I make a simple mistake or is the implementation wrong? I was wondering if there was a way to just make regions of the window a gdi graphic rather than the whole thing - but I can't figure out how (if its possible).

Additionally, is there a way to get a handle on the GraphicsFillRect so I can check if the mouse is over it?

Thankyou for any pointers

#include <GUIConstants.au3>
#include <GDIplus.au3>

Global Const $width=600
Global Const $height=400
GLobal $title="GDI+"


; Build your GUI here
Opt("GUIOnEventMode",1)
$hwnd=GUICreate($title,$width,$height)
GUISetOnEvent($GUI_EVENT_CLOSE,"close")

; BUTTONS
$ChangeRedButton = GuiCtrlCreateButton("Red",10,10,80)
$ChangeBlueButton = GuiCtrlCreateButton("Blue",100,10,80)

GUISetState()

; Load your GDI+ resources here:
_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap=_GDIPlus_BitmapCreateFromGraphics($width,$height,$graphics)
$backbuffer=_GDIPlus_ImageGetGraphicsContext($bitmap)


$Brush = _GDIPlus_BrushCreateSolid(0xFF00FF00)
_GDIPlus_GraphicsFillRect($backbuffer, 50, 100, 150, 150, $Brush)

_GDIPlus_GraphicsFillRect($backbuffer, 350, 100, 150, 150, $Brush)
        
Do
;~   _GDIPlus_GraphicsClear($backbuffer, 0x00000000)

 ; Draw your GDI+ scene onto $backbuffer here


; Change Background
    $msg = GUIGetMsg()
    If $msg = $ChangeRedButton Then
        _GDIPlus_GraphicsClear($backbuffer, 0x00000000)
        $Brush = _GDIPlus_PenCreate(0xFFFF0000, 5,  2)
        _GDIPlus_GraphicsFillRect($backbuffer, 350, 100, 150, 150, $Brush)
    ElseIf $msg = $ChangeBlueButton Then
        _GDIPlus_GraphicsClear($backbuffer, 0x00000000)
        $Brush = _GDIPlus_PenCreate(0xFF0000FF, 5,  2)
        _GDIPlus_GraphicsFillRect($backbuffer, 50, 100, 150, 150, $Brush)
    EndIf



    _GDIPlus_GraphicsDrawImageRect($graphics,$bitmap,0,0,$width,$height)
    Sleep(10)
Until False


Func close()
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_Shutdown()
    Exit
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Hi!

First of all, my template uses OnEventMode, read about it in the help file :)

It's very easy to create a gdi+ graphics object that just draws on a specific area, what you do is to create a simple control and get the handle from it and draw on the control. A great control to use is an empty label, like this:

#include <GUIConstants.au3>
#include <GDIplus.au3>

Global Const $width=600
Global Const $height=400
GLobal $title="GDI+"


; Build your GUI here
Opt("GUIOnEventMode",1)
$hwnd=GUICreate($title,$width,$height)
GUISetOnEvent($GUI_EVENT_CLOSE,"close")
$label=GUICtrlCreateLabel("",50,50,500,300)
GUISetState()

; Load your GDI+ resources here:
_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND(ControlGetHandle($hwnd,"",$label))
$bitmap=_GDIPlus_BitmapCreateFromGraphics($width,$height,$graphics)
$backbuffer=_GDIPlus_ImageGetGraphicsContext($bitmap)

        
Do
    _GDIPlus_GraphicsClear($backbuffer)



    _GDIPlus_GraphicsDrawImageRect($graphics,$bitmap,0,0,$width,$height)
    Sleep(10)
Until False


Func close()
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_Shutdown()
    Exit
EndFunc

There's no way to get a "handle" to something you draw with GDI+, just think about stuff you draw as a thin layer of color that is just for display. If you want to check for mouse hover you can always get the mouse position wih MouseGetPos() and then compare the values to the location you draw the rect on.

:o

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

That clarified everything for me and all is working well.

Many thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0