Jump to content

Simple drawing question


Recommended Posts

Hi,

I want to draw a box on my screen but i don't manage to make it stay on the screen. When the box is finished drawind, it disappears !

What sould i do ?

Schlumpf

PS : a draw box code

Func DrawBox($firstPos_x, $firstPos_y, $secondPos_x, $secondPos_y)

If $firstPos_x = "" Or $secondPos_x = "" Then

Return

EndIf

$hd = DllCall("user32.dll", "int", "GetDC", "hwnd", 0)

$pen = DllCall("gdi32.dll", "int", "CreatePen", "int", 0, "int", $LineWidth, "int", $LineColor)

DllCall("gdi32.dll", "int", "SelectObject", "int", $hd[0], "int", $pen[0])

DllCall("GDI32.dll", "int", "MoveToEx", "hwnd", $hd[0], "int", $firstPos_x, "int", $firstPos_y, "int", 0)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $firstPos_x, "int", $secondPos_y)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $secondPos_x, "int", $secondPos_y)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $secondPos_x, "int", $firstPos_y)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $firstPos_x, "int", $firstPos_y)

DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "int", $hd[0])

EndFunc ;==>Get_drawing

Link to comment
Share on other sites

Hi,

I want to draw a box on my screen but i don't manage to make it stay on the screen. When the box is finished drawind, it disappears !

What sould i do ?

Schlumpf

PS : a draw box code

Func DrawBox($firstPos_x, $firstPos_y, $secondPos_x, $secondPos_y)

If $firstPos_x = "" Or $secondPos_x = "" Then

Return

EndIf

$hd = DllCall("user32.dll", "int", "GetDC", "hwnd", 0)

$pen = DllCall("gdi32.dll", "int", "CreatePen", "int", 0, "int", $LineWidth, "int", $LineColor)

DllCall("gdi32.dll", "int", "SelectObject", "int", $hd[0], "int", $pen[0])

DllCall("GDI32.dll", "int", "MoveToEx", "hwnd", $hd[0], "int", $firstPos_x, "int", $firstPos_y, "int", 0)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $firstPos_x, "int", $secondPos_y)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $secondPos_x, "int", $secondPos_y)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $secondPos_x, "int", $firstPos_y)

DllCall("GDI32.dll", "int", "LineTo", "hwnd", $hd[0], "int", $firstPos_x, "int", $firstPos_y)

DllCall("user32.dll", "int", "ReleaseDC", "hwnd", 0, "int", $hd[0])

EndFunc ;==>Get_drawing

This script works for me:

#include <GUIConstants.au3>
Global $user = DllOpen("user32.dll")
Global $gdi = DllOpen("gdi32.dll")
Global $hDC, $Pen

$hWnd = GUICreate("box", 600, 400)
$hDC = DllCall($user, "int", "GetDC", "hwnd", $hWnd)
$hDC = $hDC[0]
$Pen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 4, "int",
0x00AAFF)
$Pen = $Pen[0]
DllCall($gdi, "int", "SelectObject", "int", $hDC, "int", $Pen)

GUISetState(@SW_SHOW)

$x1 = 10
$x2 = 590
$y1 = 10
$y2 = 390
DrawBox($x1, $y1, $x2, $y2)

While 1
$Msg = GUIGetMsg()
if $Msg = $GUI_EVENT_CLOSE then ExitLoop
WEnd

DllCall($user, "int", "ReleaseDC", "hwnd", 0, "int", $hDC)
DllClose($user)
DllClose($gdi)

Func DrawBox($x1, $y1, $x2, $y2)
if $x1 = '' or $x2 = '' then return
DllCall($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $x1, "int",
$y1, "int", 0)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y2)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y2)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y1)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y1)
EndFunc

See also original message here

Valery

The point of world view

Link to comment
Share on other sites

Yes, it works but i want to draw a box on another windows not created by Autoit. I use WinGetHandle("WORD","") to get the handle, i get it but it still disappears after drawing.

Any idea ? Maybe possible to make a transparent window ?

Schlumpf

Link to comment
Share on other sites

Yes, it works but i want to draw a box on another windows not created by Autoit. I use WinGetHandle("WORD","") to get the handle, i get it but it still disappears after drawing.

Any idea ? Maybe possible to make a transparent window ?

Schlumpf

Add

While 1

WEnd

At the end, so the script doesn't end when it's done. ?

EDIT: If you want the box to stay up, you have to put it in a While loop

BTW, you should probably have the $LineWidth and $LineColor be defined within the function

Edited by idusy
Link to comment
Share on other sites

Maybe possible to make a transparent window ?

It's possible, too:

#include <GUIConstants.au3>
Global $Transp_Flag = 100
Global $user = DllOpen("user32.dll")
Global $gdi = DllOpen("gdi32.dll")
Global $hDC, $Pen

HotKeySet("{Esc}", "On_Escape")

$hWnd = GUICreate("box", 600, 400, -1,  -1, $WS_POPUP)
WinSetTrans ($hWnd, "", $Transp_Flag)
$hDC = DllCall($user, "int", "GetDC", "hwnd", $hWnd)
$hDC = $hDC[0]
$Pen = DllCall($gdi, "int", "CreatePen", "int", 0, "int", 4, "int", 0x00AAFF)
$Pen = $Pen[0]
DllCall($gdi, "int", "SelectObject", "int", $hDC, "int", $Pen)

GUISetState(@SW_SHOW)

$x1 = 10
$x2 = 590
$y1 = 10
$y2 = 390
DrawBox($x1, $y1, $x2, $y2)

While 1
 $Msg = GUIGetMsg()
 if $Msg = $GUI_EVENT_CLOSE then ExitLoop
WEnd
On_Exit()


func On_Escape()
 DllCall($user, "int", "ReleaseDC", "hwnd", 0, "int", $hDC)
 DllClose($user)
 DllClose($gdi)
  exit
endfunc

Func DrawBox($x1, $y1, $x2, $y2)
if $x1 = '' or $x2 = '' then return
DllCall($gdi, "int", "MoveToEx", "hwnd", $hDC, "int", $x1, "int", $y1, "int", 0)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y2)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y2)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x2, "int", $y1)
DllCall($gdi, "int", "LineTo", "hwnd", $hDC, "int", $x1, "int", $y1)
EndFunc

Valery

The point of world view

Link to comment
Share on other sites

What you are trying to do will be better accomplished using 32-bit color data and layered windows. If you borrow another programs device context, they will repaint their own image when they realize that there is something wrong. If you have the layered window above say, Microsoft Word, and the window is done correctly, you will not have any repaint issues because Word will not own the device context above itself.

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