Sign in to follow this  
Followers 0
daddydave

Drawing Rectangle, only seeing two sides

6 posts in this topic

#1 ·  Posted (edited)

I'm sure this is probably more of a WinAPI question than an AutoIt3 question, but I'm hoping it's something simple .

I am trying to draw a rectangle using _GDIPlus_GraphicsDrawRect, but I only see the right and bottom sides of the rectangle. What am I doing wrong?

All the examples with _GDIPlus_GraphicsDrawRect that I have seen involve creating a new image. Is that what I must do?

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#include <WinAPI.au3>

Opt('MustDeclareVars', 1)

_Main() 

Func _Main()
    Local $hGUI, $hWnd, $hGraphic, $hDC, $hPen, $iHeight, $iWidth, 

; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    
;; Get dimensions
    $iWidth = _WinAPI_GetClientWidth($hWnd)
    $iHeight = _WinAPI_GetClientHeight($hWnd)
    
    MsgBox("", "", String($iWidth))
    MsgBox("", "", String($iHeight))
    
    $hDC = _WinAPI_GetWindowDC($hWnd)
    GUISetState()

    _GDIPlus_Startup()
    
    
    
    $hGraphic = _GDIPlus_GraphicsCreateFromHDC($hDC)
    
;; Outline window in red
    $hPen = _GDIPlus_PenCreate(0xC4FF0000, 2)
    $hBrush = _GDIPlus_BrushCreateSolid(0xffFF0000)
    
    _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $iWidth, $iHeight, $hPen)

; Clean up resources
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_ReleaseDC($hWnd, $hDC)
    _GDIPlus_Shutdown()

EndFunc  ;==>_Main
Edited by daddydave

Share this post


Link to post
Share on other sites



Hi,

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hWnd, $hGraphic, $hPen, $iWidth, $iHeight

    $hWnd = GUICreate("GDI+", 400, 300)
   
    $iWidth = _WinAPI_GetClientWidth($hWnd)
    $iHeight = _WinAPI_GetClientHeight($hWnd)
    
    MsgBox("", "", "Client Width: " & $iWidth & @LF & "Client Height: " & $iHeight)

    GUISetState()

    _GDIPlus_Startup()
    
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    
    $hPen = _GDIPlus_PenCreate(0xC4FF0000, 2)

    _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $iWidth, $iHeight, $hPen)
    
    Do
    Until GUIGetMsg() = -3  
    

    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)

    _GDIPlus_Shutdown()

EndFunc  ;==>_Main

Cheers

Share this post


Link to post
Share on other sites

smashly

Hi! Thanks for example. One question, if i will minimize window and then restore his (any repaint behavior), that drawed line is disappear. How avoid from this behaviour?

Share this post


Link to post
Share on other sites

Thank you so much, smashly!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi rasim,

You can do it multiple ways depending on your needs, but if your just after to update the graphic on th WM_Paint msg then..

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

Global $hWnd, $hGraphic, $hPen, $iWidth, $iHeight

_Main()

Func _Main()
    $hWnd = GUICreate("GDI+", 400, 300)
   
    $iWidth = _WinAPI_GetClientWidth($hWnd)
    $iHeight = _WinAPI_GetClientHeight($hWnd)
    
    MsgBox("", "", "Client Width: " & $iWidth & @LF & "Client Height: " & $iHeight)

    GUISetState()
    
    GUIRegisterMsg($WM_PAINT, "MY_WM_PAINT")
    
    _GDIPlus_Startup()
    
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hWnd)
    
    $hPen = _GDIPlus_PenCreate(0xC4FF0000, 2)

    _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $iWidth, $iHeight, $hPen)
    
    Do
    Until GUIGetMsg() = -3  
    
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic)

    _GDIPlus_Shutdown()

EndFunc  ;==>_Main

Func MY_WM_PAINT($hWnd, $Msg)
    _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_UPDATENOW)
    _GDIPlus_GraphicsDrawRect($hGraphic, 0, 0, $iWidth, $iHeight, $hPen)
    _WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_VALIDATE)
    Return $GUI_RUNDEFMSG
EndFunc

Cheers

Edit: your welcome daddydave, I seemed to have posted while you were posting :)

Edited by smashly

Share this post


Link to post
Share on other sites

smashly

Thanks! I think this part of code don`t need, because window is redrawed automatically:

_WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_UPDATENOW)
_WinAPI_RedrawWindow($hWnd, 0, 0, $RDW_VALIDATE)

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