Jump to content
Sign in to follow this  
RealisT

Redrawing over old rectangles

Recommended Posts

RealisT

I've put together a decent experimental script that creates something like the "Selection Rectangle" during a mouse drag. It's fine except for one annoying little quirk.

Instead of only the most current rectangle being displayed at any time, all of the previous renderings of the rectangle (drawn during the mouse drag) persist. I cannot get rid of these whether by redrawing or by _GDIPlus_GraphicsDispose(). (See image, attached)

How do I keep redrawing my rectangle while removing the previous draws ?

(See code below. Most directly relevant parts in purple.)

Thanks

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#include <GDIPlus.au3>

#include <GuiConstantsEx.au3>

AutoItSetOption("WinTitleMatchMode", 2)

AutoItSetOption ("PixelCoordMode", 1)

AutoItSetOption ("MouseCoordMode", 2)

AutoItSetOption("MustDeclareVars", 1) ; MUST DECLARE VARIABLES

Global $blueGraphic, $blueBrush

Global $winGUIhandle

Global $msg, $mainForm, $currPos, $selGraphic1, $mainFormName

Global $penGraphic, $penGraphic, $selPen, $selBoxGraphic

Global $currPos, $startX, $startY, $midX, $midY, $endX, $endY, $topX, $topY

Global $dragInProgress = 0

$mainFormName = "Drag Rectangle Test"

$mainForm = GUICreate($mainFormName, 800, 600, 150, 100)

GUISetBkColor(0xFFFFFF)

$winGUIhandle = WinGetHandle($mainFormName)

_GDIPlus_Startup ()

$penGraphic = _GDIPlus_GraphicsCreateFromHWND ($winGUIhandle)

$selPen = _GDIPlus_PenCreate ()

_GDIPlus_PenSetColor($selPen, 0xFF999999)

_GDIPlus_PenSetDashStyle($selPen, $GDIP_DASHSTYLEDOT)

$blueBrush = _GDIPlus_BrushCreateSolid(0xFF6396FF)

GUISetState()

; Run the GUI until the dialog is closed

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $GUI_EVENT_PRIMARYDOWN

$dragInProgress = 1

; BEGIN DRAG ************************

$currPos = MouseGetPos ()

$startX = $currPos[0]

$startY = $currPos[1]

Case $msg = $GUI_EVENT_MOUSEMOVE ; Dragging Piece

If $dragInProgress = 1 Then

; DRAG IN PROGRESS **************

$currPos = MouseGetPos ()

$midX = $currPos[0]

$midY = $currPos[1]

If $midX >= $startX Then

$topX = $startX

Else

$topX = $midX

EndIf

If $midY >= $startY Then

$topY = $startY

Else

$topY = $midY

EndIf

$selBoxGraphic = _GDIPlus_GraphicsCreateFromHWND($winGUIhandle)

_Rect1($topX, $topY, Abs($midX - $startX), Abs($midY - $startY))

EndIf

Case $msg = $GUI_EVENT_PRIMARYUP

$dragInProgress = 0

; END DRAG *************************

$currPos = MouseGetPos ()

$endX = $currPos[0]

$endY = $currPos[1]

If $endX >= $startX Then

$topX = $startX

Else

$topX = $endX

EndIf

If $endY >= $startY Then

$topY = $startY

Else

$topY = $endY

EndIf

$blueGraphic = _GDIPlus_GraphicsCreateFromHWND($winGUIhandle)

_GDIPlus_GraphicsFillRect($blueGraphic, $topX, $topY, Abs($endX - $startX), Abs( $endY - $startY), $blueBrush)

EndSelect

Wend

Func _Rect1($rLeft1, $rTop1, $rWidth, $rHeight)

; Draw a Selection Drag Rectangle

$selGraphic1 = _GDIPlus_GraphicsCreateFromHWND ($winGUIhandle)

_GDIPlus_GraphicsDrawRect($selGraphic1, $rLeft1, $rTop1, $rWidth, $rHeight, $selPen)

EndFunc ;==>_Rect

; Clean up resources

_GDIPlus_PenDispose ($selPen)

_GDIPlus_BrushDispose ($blueBrush)

_GDIPlus_GraphicsDispose ($penGraphic)

_GDIPlus_GraphicsDispose ($selBoxGraphic)

_GDIPlus_GraphicsDispose ($blueGraphic)

_GDIPlus_ShutDown ()

Exit

post-29806-1201014126_thumb.jpg

Share this post


Link to post
Share on other sites
smashly

Hi, use _WinAPI_InvalidateRect() to erase the background before before drawing again.

Example of where to add it in your code:

Add This to the top of your script as an include.

#Include <WinAPI.au3>oÝ÷ Ø]¶ºw-åG¬¡«­¢+Ø
ÍÀÌØíµÍôÀÌØíU%}Y9Q}5=UM5=YìÉ¥¹A¥($$%%ÀÌØíÉ%¹AɽÉÍÌôÄQ¡¸($$$$(ìI%8AI=IML¨¨¨¨¨¨¨¨¨¨¨¨¨¨(ÀÌØíÕÉÉA½Ìô5½ÕÍÑA½Ì ¤(ÀÌØíµ¥`ôÀÌØíÕÉÉA½ÍlÁt(ÀÌØíµ¥dôÀÌØíÕÉÉA½ÍlÅt(%ÀÌØíµ¥`ÐìôÀÌØíÍÑÉÑ`Q¡¸($$$$$ÀÌØíѽÁ`ôÀÌØíÍÑÉÑ`($$$%±Í(ÀÌØíѽÁ`ôÀÌØíµ¥`(¹%(%ÀÌØíµ¥dÐìôÀÌØíÍÑÉÑdQ¡¸($$$$$ÀÌØíѽÁdôÀÌØíÍÑÉÑd($$$%±Í(ÀÌØíѽÁdôÀÌØíµ¥d(¹%($$$$($$$%}]¥¹A%}%¹Ù±¥ÑIÐ ÀÌØíÝ¥¹U%¡¹±°À°QÉÕ¤ì%¹Ù±¥ÑIСÉ($$$$($$$$ÀÌØíͱ  ½áÉÁ¡¥ô}%A±ÕÍ}ÉÁ¡¥Í
ÉÑɽµ!]9 ÀÌØíÝ¥¹U%¡¹±¤($$$%}IÐÄ ÀÌØíѽÁ`°ÀÌØíѽÁd°Ì ÀÌØíµ¥`´ÀÌØíÍÑÉÑ`¤°Ì ÀÌØíµ¥d´ÀÌØíÍÑÉÑd¤¤($$%¹%

Cheers

Share this post


Link to post
Share on other sites
RealisT

Works great. Thank you!

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  

×