Jump to content

WinApi Drawing "on top"


 Share

Recommended Posts

Hey there,

I've made a basic WinApi script to draw rectangles and text on the screen for use as a debug tool, the problem I have is that screen refreshes from other windows will overwrite the lines/text almost instantaneously. Is there any way to make the lines stay "on top" until manually cleared?

A portion of the script I'm using to draw the lines is (PS: moderators, I do not like the updated post editor):

#Include <WinAPI.au3>
#Include <WindowsConstants.au3>
 
_DrawRectEx(1, 1, 100, 100, 2, 0xFF)
 
While 1
    Sleep(10)
Wend
 
Func _DrawRectEx($LeftValue, $TopValue, $RightValue, $BottomValue, $RectWidth, $RectColour)
    ; Draws a rectangle around the given area, crossed out.
    Local $hDC, $hPen, $obj_orig
    $hWnd = WinGetHandle("[ACTIVE]", "")
    $hDC = _WinAPI_GetWindowDC($hWnd)
    $hPen = _WinAPI_CreatePen($PS_SOLID, $RectWidth, $RectColour)
    $obj_orig = _WinAPI_SelectObject($hDC, $hPen)
 
    _WinAPI_MoveTo($hDC, $LeftValue, $TopValue)  ; move pen to the top-left
    _WinAPI_LineTo($hDC, $LeftValue, $BottomValue) ; draw line to the bottom left
    _WinAPI_LineTo($hDC, $RightValue, $BottomValue) ; draw line to the bottom right
    _WinAPI_LineTo($hDC, $RightValue, $TopValue) ; draw line to the top right
    _WinAPI_LineTo($hDC, $LeftValue, $TopValue) ; draw line to the top left
    _WinAPI_LineTo($hDC, $RightValue, $BottomValue) ; draw line to the bottom right (diagonal)
    _WinAPI_MoveTo($hDC, $RightValue, $TopValue)  ; move pen to the top right
    _WinAPI_LineTo($hDC, $LeftValue, $BottomValue) ; draw line to the bottom left (diagonal)
    Return $hDC
EndFunc   ;==>DrawRectEx
Link to comment
Share on other sites

Cool(x2) ... started something like that some time ago. But never got that far with it.

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

#Include <WinAPI.au3>
#Include <WindowsConstants.au3>
Global $hDC, $hPen, $hWnd ; Needded to Dispose later
HotKeySet("{ESC}", "_Exit")
$GUI = GuiCreate("",@DesktopWidth,@DesktopHeight,0, 0,$WS_POPUP, bitor($WS_EX_LAYERED,$WS_EX_TRANSPARENT))
GuiSetBkColor(0x123456)
_WinAPI_SetLayeredWindowAttributes($GUI,0x123456,255,0x01)
WinSetOnTop($GUI, "", 1)
GuiSetState()
_DrawRectEx($GUI, @DesktopWidth/2-50, @DesktopHeight/2-50, @DesktopWidth/2+50, @DesktopHeight/2+50, 2, 0xFF)
While 1
    Sleep(10)
Wend
Func _DrawRectEx($hGUI, $LeftValue, $TopValue, $RightValue, $BottomValue, $RectWidth, $RectColour)
    ; Draws a rectangle around the given area, crossed out.
    Local $obj_orig
    $hWnd = $hGUI
    $hDC = _WinAPI_GetWindowDC($hWnd)
    $hPen = _WinAPI_CreatePen($PS_SOLID, $RectWidth, $RectColour)
    $obj_orig = _WinAPI_SelectObject($hDC, $hPen)
    _WinAPI_MoveTo($hDC, $LeftValue, $TopValue)  ; move pen to the top-left
    _WinAPI_LineTo($hDC, $LeftValue, $BottomValue) ; draw line to the bottom left
    _WinAPI_LineTo($hDC, $RightValue, $BottomValue) ; draw line to the bottom right
    _WinAPI_LineTo($hDC, $RightValue, $TopValue) ; draw line to the top right
    _WinAPI_LineTo($hDC, $LeftValue, $TopValue) ; draw line to the top left
    _WinAPI_LineTo($hDC, $RightValue, $BottomValue) ; draw line to the bottom right (diagonal)
    _WinAPI_MoveTo($hDC, $RightValue, $TopValue)  ; move pen to the top right
    _WinAPI_LineTo($hDC, $LeftValue, $BottomValue) ; draw line to the bottom left (diagonal)
    Return $hDC
EndFunc   ;==>DrawRectEx
Func _Exit()
; You Forgot to releace resources... read the help file about it in the remarks
; section of _winapi_penCreate and _WinApi_GetWindowDC
_WinAPI_DeleteObject($hPen)
_WinAPI_ReleaseDC($hWnd, $hDC)
Exit
EndFunc

By the way... Nice Code... i use the WS_EX_LAYERED style to use a color to be transparent using the _WinAPI_SetLayeredWindowAttributes function, the WS_EX_TRANPARENT style to avoid the GUI reciving any click, so you can click to any thing behind the tranparent GUI

Link to comment
Share on other sites

@monoscout999 - very much appreciated!! I'll try it out when I get home.

I had the resource-release in an "exit" function, called when ESC was hit, my bad - I forgot to paste that part of the code in my example...

Edited by Steve0
Link to comment
Share on other sites

Here a version to mark areas on desktop

#include <Misc.au3>
#Include <WinAPI.au3>
#Include <WindowsConstants.au3>
 
HotKeySet("{ESC}", "_Exit")
 
Global Const $hFullScreen = WinGetHandle("Program Manager")
Global Const $aFullScreen = WinGetPos($hFullScreen)
Global Const $hGUI = GUICreate("Desktop Marker by UEZ 2011", $aFullScreen[2], $aFullScreen[3], $aFullScreen[0], $aFullScreen[1], $WS_POPUP, BitOR($WS_EX_LAYERED,$WS_EX_TRANSPARENT))
GuiSetBkColor(0xABCDEF)
_WinAPI_SetLayeredWindowAttributes($hGUI, 0xABCDEF, 0xA0)
WinSetOnTop($hGUI, "", 1)
GuiSetState()
 
Global Const $hDC = _WinAPI_GetWindowDC($hGUI)
Global Const $hPen = _WinAPI_CreatePen($PS_SOLID, 20, 0x00FFFF)
Global Const $obj_orig = _WinAPI_SelectObject($hDC, $hPen)
 
Global Const $user32_dll = DllOpen("user32.dll")
Global $mx, $mxo, $my, $myo
 
While Sleep(50)
$mxo = MouseGetPos(0) + Abs($aFullScreen[0])
$myo = MouseGetPos(1) + Abs($aFullScreen[1])
While _IsPressed("11", $user32_dll) * Sleep(10)
$mx = MouseGetPos(0) + Abs($aFullScreen[0])
$my = MouseGetPos(1) + Abs($aFullScreen[1])
_WinAPI_DrawLine($hDC, $mx, $my, $mxo, $myo)
$mxo = $mx
$myo = $my
WEnd
Wend
 
Func _Exit()
DllClose($user32_dll)
_WinAPI_SelectObject($hDC, $obj_orig)
_WinAPI_DeleteObject($hPen)
_WinAPI_ReleaseDC($hGUI, $hDC)
Exit
EndFunc

Hold down the Ctrl key to draw on screen.

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

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...