Jump to content

how get Mouse Get Pos to draw a line


Alex1986
 Share

Recommended Posts

Hi

i want Get Mouse Pos Form Two Point To Draw Line with use GDI

Example : if KeyDown  CTRL then  $BEGINPoint = MouseGetPos , if  KeyUp CTRL $ENDPoint = MouseGetPos  , Draw Line $BEGINPoint  To $ENDPoint

My Code :

#include <WindowsConstants.au3>
#include <WinAPI.au3>
#Include <GDIPlus.au3>
#include <Misc.au3>

Local $PointA , $PointB , $Cunt = 0

_GDIPlus_Startup ()
$hDC = _WinAPI_GetWindowDC(0)
$hGraphic = _GDIPlus_GraphicsCreateFromHDC($hDC)
$Color = 0xFF000000
$hPen = _GDIPlus_PenCreate($Color,2)

While 1
    
If $PointA + $PointB = 0 And _IsPressed(43) Then ; 43 = Key C
$PointA = MouseGetPos()
$Cunt = 1
EndIf

if $Cunt = 1 Then
    If _IsPressed(43) Then
$PointB = MouseGetPos()
 _GDIPlus_GraphicsDrawLine($hGraphic, $PointA[0] , $PointB[0] , $PointA[1] , $PointB[1], $hPen)
        $PointA = 0
        $PointB = 0
        $Cunt = 0
    EndIf
EndIf

WEnd

_WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE+$RDW_ALLCHILDREN)
_WinAPI_ReleaseDC(0, $hDC)
_GDIPlus_Shutdown()

 

 

Edited by Alex1986
Link to comment
Share on other sites

Alex1986,

You may be able to adapt this from UEZ...

#Include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

_GDIPlus_Startup()

Global Const $HBITMAP = _ScreenCapture_Capture("", 0, 0, @DesktopWidth, @DesktopHeight, 0)

Global Const $hGUI = GUICreate("Mark screen by UEZ", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP)
GUISetState(@SW_SHOW, $hGUI)

Global Const $hGraphic_Bg = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($HBITMAP)
_GDIPlus_GraphicsDrawImage($hGraphic_Bg, $hBmp, 0, 0)
_WinAPI_DeleteObject($HBITMAP)

Global Const $pen_size = 1
Global Const $hPen = _GDIPlus_PenCreate(0xff000000, $pen_size)

GUISetOnEvent(-3, "_Exit")
OnAutoItExitRegister("_Exit")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "Draw", $hGUI)

Global Const $om = MouseGetCursor()

While Sleep(100000)
WEnd



Func Draw()
    Local $aMC, $mxo, $myo

    $aMC = GUIGetCursorInfo($hGUI)
    Sleep(50)
    Do
        GUISetCursor(0, 1, $hGUI)
        $mxo = $aMC[0]
        $myo = $aMC[1]
        $aMC = GUIGetCursorInfo($hGUI)
        If $mxo <> $aMC[0] Or $myo <> $aMC[1] Then
            _GDIPlus_GraphicsDrawLine($hGraphic_Bg, $aMC[0], $aMC[1], $mxo, $myo, $hPen)
            $mxo = $aMC[0]
            $myo = $aMC[1]
        EndIf

    Until Not $aMC[2]
EndFunc



Func _Exit()
    GUISetCursor($om, 1, $hGUI)
    GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "")
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic_Bg)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Alex1986,

You may be able to adapt this from UEZ...

#Include <ScreenCapture.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

_GDIPlus_Startup()

Global Const $HBITMAP = _ScreenCapture_Capture("", 0, 0, @DesktopWidth, @DesktopHeight, 0)

Global Const $hGUI = GUICreate("Mark screen by UEZ", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP)
GUISetState(@SW_SHOW, $hGUI)

Global Const $hGraphic_Bg = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hBmp = _GDIPlus_BitmapCreateFromHBITMAP($HBITMAP)
_GDIPlus_GraphicsDrawImage($hGraphic_Bg, $hBmp, 0, 0)
_WinAPI_DeleteObject($HBITMAP)

Global Const $pen_size = 1
Global Const $hPen = _GDIPlus_PenCreate(0xff000000, $pen_size)

GUISetOnEvent(-3, "_Exit")
OnAutoItExitRegister("_Exit")
GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "Draw", $hGUI)

Global Const $om = MouseGetCursor()

While Sleep(100000)
WEnd



Func Draw()
    Local $aMC, $mxo, $myo

    $aMC = GUIGetCursorInfo($hGUI)
    Sleep(50)
    Do
        GUISetCursor(0, 1, $hGUI)
        $mxo = $aMC[0]
        $myo = $aMC[1]
        $aMC = GUIGetCursorInfo($hGUI)
        If $mxo <> $aMC[0] Or $myo <> $aMC[1] Then
            _GDIPlus_GraphicsDrawLine($hGraphic_Bg, $aMC[0], $aMC[1], $mxo, $myo, $hPen)
            $mxo = $aMC[0]
            $myo = $aMC[1]
        EndIf

    Until Not $aMC[2]
EndFunc



Func _Exit()
    GUISetCursor($om, 1, $hGUI)
    GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "")
    _GDIPlus_PenDispose($hPen)
    _GDIPlus_GraphicsDispose($hGraphic_Bg)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI)
    Exit
EndFunc

kylomas

But this draw in GUI , i want draw direct in Screen without ScreenCapture .

Thank You .

Link to comment
Share on other sites

But this draw in GUI , i want draw direct in Screen without ScreenCapture .

Yes, that is where the "adapt" part comes in... 

Edit: I have this snippet...

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

Local $hDC, $hPen, $obj_orig, $x1 = 0, $x2 = @DesktopWidth, $y1 = 0, $y2 = 0

_line()

Func _line()
    $hDC = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
    $hPen = _WinAPI_CreatePen($PS_SOLID, 2, 0x00ff)
    $obj_orig = _WinAPI_SelectObject($hDC, $hPen)
    While $y1 < @DesktopHeight

        _WinAPI_DrawLine($hDC, $x1, $y1, $x2, $y1)
        _WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE + $RDW_ALLCHILDREN)
        $y1 += 1
    WEnd

    _WinAPI_SelectObject($hDC, $obj_orig)
    _WinAPI_DeleteObject($hPen)
    _WinAPI_ReleaseDC(0, $hDC)

EndFunc   ;==>_line

Maybe it's useful.  I probably should not have even responded to this thread as I never do this kind of thing. 

 

Edited by kylomas
additional info

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Well, when you directly draw on the desktop you risk to loose your drawing when the DC gets refreshed!

Here an modified version from kylomas' post #4

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

AutoItSetOption("GUIOnEventMode", 1)

Global $hGUI, $hDC, $hPen, $obj_orig
$hGUI = GUICreate("", @DesktopWidth, @DesktopHeight, 0, 0, $WS_POPUP)
WinSetTrans($hGUI, "", 1)
GUISetState()
_line()

Func _line()
    $hDC = _WinAPI_GetWindowDC(0) ; DC of entire screen (desktop)
    $hPen = _WinAPI_CreatePen($PS_SOLID, 2, 0x00ff)
    $obj_orig = _WinAPI_SelectObject($hDC, $hPen)
    GUISetOnEvent($GUI_EVENT_PRIMARYDOWN, "Draw", $hGUI)
    GUISetOnEvent(-3, "_Exit")
    Do
    Until Not Sleep(1000)
EndFunc   ;==>_line

Func Draw()
    Local $aMC, $mxo, $myo
    $aMC = GUIGetCursorInfo($hGUI)
    Do
        GUISetCursor(0, 1, 0)
        $mxo = $aMC[0]
        $myo = $aMC[1]
        $aMC = GUIGetCursorInfo($hGUI)
        If $mxo <> $aMC[0] Or $myo <> $aMC[1] Then
            _WinAPI_DrawLine($hDC, $aMC[0], $aMC[1], $mxo, $myo)
            _WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE)
            $mxo = $aMC[0]
            $myo = $aMC[1]
        EndIf
        Sleep(10)
    Until Not $aMC[2]
EndFunc

Func _Exit()
    _WinAPI_SelectObject($hDC, $obj_orig)
    _WinAPI_DeleteObject($hPen)
    _WinAPI_ReleaseDC(0, $hDC)
    Exit
EndFunc

 

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