Jump to content

How do I delete a rectangle?


Go to solution Solved by JohnOne,

Recommended Posts

I found a >function by Malkey that draws a rectangle onto the screen.  Works great.  I need to delete that rect.  Is that possible?  If so, how?  I searched all over and tried several things but no joy.  Any ideas, hints, and or tips will be greatly appreciated.

I modifed the func and so here it is just in case:

Func _WinAPI_DrawRect(const $l_zoom_level, const $x, const $y, Const $pen_style = $PS_SOLID)
  ; Author: Malkey
  ; http://www.autoitscript.com/forum/topic/135728-how-to-i-draw-one-rectangle-on-screen/?p=947819
 
  Local Static $zoom_width = $main_width - 4
 
  local const $zoom_by_two = Int($l_zoom_level / 2)
 
  local const $left = $x - $zoom_by_two
 
  local const $top = $y - $zoom_by_two
 
  local const $right = $left + $l_zoom_level
 
  local const $bottom = $top + $l_zoom_level
 
  local static $tRect = DllStructCreate($tagRECT)

  $tRect.Left   = $left
  $tRect.Top    = $top
  $tRect.Right  = $right
  $tRect.Bottom = $bottom

  local static $hDC = _WinAPI_GetWindowDC(0)

  local const $hPen = _WinAPI_CreatePen($pen_style, 2, 0xFF0000)
 
  DllCall($user32_dll, "int", "FrameRect", "handle", $hDC, "struct*", $tRect, "handle", $hPen) ; _WinAPI_FrameRect
EndFunc
Edited by jaberwacky
Link to comment
Share on other sites

Probably need to use _WinAPI_DeleteObject() on _WinAPI_CreatePen()  and _WinAPI_ReleaseDC() on _WinAPI_GetWindowDC.

It is also possible you may have to redraw screen.

I runnable example would help.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

At the same thread there are many examples almost all with realese (DC) cleaning the Rect

saludos

Link to comment
Share on other sites

Probably need to use _WinAPI_DeleteObject() on _WinAPI_CreatePen()  and _WinAPI_ReleaseDC() on _WinAPI_GetWindowDC.

It is also possible you may have to redraw screen.

I runnable example would help.

Ok, I tried the release and delete stuff so I may have to refresh the screen.  Thanks I will look into that!

Here is a runnable example (the whole thing): [REDACTED]

Move the mouse around and click F4 to see a red rectangle.

The rects disappear when I bring focus to the window but I want them disappear at the end of a timer or maybe a hotkey I don't know yet.

Edited by jaberwacky
Link to comment
Share on other sites

Ok, what I'm thinking is that I will need to either screenshot the desktop or overlay a transparent gui over it and then use GDI+ to draw and erase the rectangles?

Link to comment
Share on other sites

Ok, so I found this example in the helpfile.

#include <WinAPI.au3>
#include <WindowsConstants.au3>

ShowCross(@DesktopWidth / 2, @DesktopHeight / 2, 20, 2, 0xFF, 3000)

Func ShowCross($start_x, $start_y, $length, $width, $color, $time)
    Local $hDC, $hPen, $obj_orig

    $hDC = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
    $hPen = _WinAPI_CreatePen($PS_SOLID, $width, $color)
    $obj_orig = _WinAPI_SelectObject($hDC, $hPen)

    _WinAPI_DrawLine($hDC, $start_x - $length, $start_y, $start_x - 5, $start_y) ; horizontal left
    _WinAPI_DrawLine($hDC, $start_x + $length, $start_y, $start_x + 5, $start_y) ; horizontal right
    _WinAPI_DrawLine($hDC, $start_x, $start_y - $length, $start_x, $start_y - 5) ; vertical up
    ;   _WinAPI_DrawLine($hDC, $start_x, $start_y + $length, $start_x, $start_y + 5) ; vertical down
    _WinAPI_MoveTo($hDC, $start_x, $start_y + $length)
    _WinAPI_LineTo($hDC, $start_x, $start_y + 5)

    Sleep(3000) ; show cross over screen for defined seconds

    ; refresh desktop (clear cross)
    _WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_UPDATENOW + $RDW_ERASENOW + $RDW_INVALIDATE + $RDW_ALLCHILDREN)
  
    Sleep(3000)
  
    ; clear resources
    _WinAPI_SelectObject($hDC, $obj_orig)
    _WinAPI_DeleteObject($hPen)
    _WinAPI_ReleaseDC(0, $hDC)
EndFunc   ;==>ShowCross

It seems as though _WinAPI_RedrawWindow does not erase the cross mark after all.  Have I misunderstood the purpose of _WinAPI_RedrawWindow?

Link to comment
Share on other sites

It will erase the cross, if the cross is drawn over the desktop. If you're running it from SciTE, more than likely it's being drawn over the SciTE window, and the line is clearing the wrong window. Change the _WinAPI_RedrawWindow line to the line below if running it from SciTE.

_WinAPI_RedrawWindow(WinGetHandle("[class:SciTEWindow]"), 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

  • Solution

If BrewManNH suggestion works within scite, then you might have to write a function to enumerate all windows which have been drawn on.

Or simply all visible windows, and redraw them all.

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

Alritey, so there's no one shot set of functions which manage this then?  I can't just say to draw a rectangle on the screen and then simply call a function which basically says, "Hey, you know that rectangle you just drew?  Yeah, that one. Erase it, bud."

Ok, just occurred to me, I could make a func which remembers the pixel colors of the area where the lines of the rectangle will reside.  Draw the rectangle, and then call a function which will color those rectangle pixels back to the original colors.  Good idea?

Link to comment
Share on other sites

That would work if every pixel were the same color, otherwise you'll end up with another rectangle in a different color.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

If BrewManNH suggestion works within scite, then you might have to write a function to enumerate all windows which have been drawn on.

Or simply all visible windows, and redraw them all.

I tried this and it works well.  Thank you.

Func _WinAPI_DrawRect(Const $l_zoom_level, Const $x, Const $y)
  Local Const $zoom_by_two = Int($l_zoom_level / 2)

  Local Const $left = $x - $zoom_by_two

  Local Const $top = $y - $zoom_by_two

  _WinAPI_DrawLine($hDC, $x - $zoom_by_two, $y + $zoom_by_two, $x - $zoom_by_two, $y - $zoom_by_two) ; left
  _WinAPI_DrawLine($hDC, $x + $zoom_by_two, $y - $zoom_by_two, $x - $zoom_by_two, $y - $zoom_by_two) ; top
  _WinAPI_DrawLine($hDC, $x + $zoom_by_two, $y + $zoom_by_two, $x + $zoom_by_two, $y - $zoom_by_two) ; right
  _WinAPI_DrawLine($hDC, $x - $zoom_by_two, $y + $zoom_by_two, $x + $zoom_by_two, $y + $zoom_by_two) ; bottom

  Sleep(1000)

  Local Const $window_list = WinList()

  For $i = 1 To $window_list[0][0]
    _WinAPI_RedrawWindow($window_list[$i][1], 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)
  Next
EndFunc
Link to comment
Share on other sites

  • 4 weeks later...

From Au3Info, and running on Win 7, the desktop handle is

ControlGetHandle("[Class:WorkerW]", "", "SysListView321")

So on Win 7, this appears to work.

#include <WinAPI.au3>
#include <WindowsConstants.au3>

_WinAPI_DrawRect(20, 20, @DesktopWidth / 2, @DesktopHeight / 2, 0x0000FF)
Sleep(2000)

_WinAPI_RedrawWindow(ControlGetHandle("[Class:WorkerW]", "", "SysListView321"), 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)

Sleep(3000)


Func _WinAPI_DrawRect($start_x, $start_y, $iWidth, $iHeight, $iColor)
    Local $hDC = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
    Local $tRect = DllStructCreate($tagRECT)
    DllStructSetData($tRect, 1, $start_x)
    DllStructSetData($tRect, 2, $start_y)
    DllStructSetData($tRect, 3, $iWidth)
    DllStructSetData($tRect, 4, $iHeight)
    Local $hBrush = _WinAPI_CreateSolidBrush($iColor)

    _WinAPI_FrameRect($hDC, DllStructGetPtr($tRect), $hBrush)

    ; clear resources
    _WinAPI_DeleteObject($hBrush)
    _WinAPI_ReleaseDC(0, $hDC)
EndFunc   ;==>_WinAPI_DrawRect

 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...