Jump to content
Sign in to follow this  
Xtian

Draw a Line?

Recommended Posts

Xtian

I just wanted to draw a line when i hold and click left mouse button but i can't make it work. same with mspaint

:graduated:

help please.

Thank you.

If possible i want to make it work using this code, :( i want to make it simple as it can be.

#include <GUIConstantsEx.au3>

#Include <Misc.au3>

Opt("GuiOnEventMode",1)

Opt("MouseCoordMode",0)

GUICreate("Test",500,500)

GUISetOnEvent($gui_event_close,"Quit")

GuiSetCursor(3,1)

GUISetState()

$dll = DllOpen("user32.dll")

While 1

Sleep(15)

if _IsPressed("01",$dll) then

Do

$mouse = MouseGetPos()

$x = $mouse[0]

$y = $mouse[1]

$draw = GUICtrlCreateGraphic($x,$y,500,500)

$drawline = GUICtrlSetGraphic($draw,$GUI_GR_Line,$x,0)

Until not _IsPressed("01",$dll)

EndIf

WEnd

DllClose($dll)

Func Quit()

Exit

EndFunc


Sorry for being noob but... thanks to you im learned something new.

Share this post


Link to post
Share on other sites
Yashied

Draw by using GUICtrlCreateGraphic() not the best idea.

#Include <ColorPicker.au3>
#Include <GUIConstantsEx.au3>
#Include <WinAPIEx.au3>
#Include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global Const $STM_SETIMAGE = 0x0172
Global Const $STM_GETIMAGE = 0x0173

Global $hBitmap = _WinAPI_CreateSolidBitmap(0, -1, 600, 600)
Global $hCursor = _WinAPI_LoadCursorFromFile(@ScriptDir & '\Pen.cur')
Global $hForm, $Msg, $Info, $Pic, $hPic, $Picker
Global $Color = 0x00AEFF, $Draw = False, $In = True
Global $Pos1[2] = [-1, -1], $Pos2 = $Pos1

$hForm = GUICreate('MyPaint', 600, 640)
$Picker = _GUIColorPicker_Create('', 519, 607, 74, 26, $Color, BitOR($CP_FLAG_DEFAULT, $CP_FLAG_CHOOSERBUTTON, $CP_FLAG_ARROWSTYLE), 0, -1, -1, 0)
$Pic = GUICtrlCreatePic('', 0, 0, 600, 600, 0)
$hPic = GUICtrlGetHandle(-1)
_Draw()
GUIRegisterMsg($WM_SETCURSOR, 'WM_SETCURSOR')
GUISetState()

While 1
    $Msg = GUIGetMsg()
    Switch $Msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Picker
            $Color = _GUIColorPicker_GetColor($Picker)
    EndSwitch
    $Info = GUIGetCursorInfo()
    If @error Then
        ContinueLoop
    EndIf
    If $Info[2] Then
        If $Draw Then
            $Pos1 = $Pos2
            $Pos2 = $Info
            If ($Pos1[0] <> $Pos2[0]) Or ($Pos1[1] <> $Pos2[1]) Then
                _Draw()
            EndIf
        Else
            $Pos1 = $Info
            $Pos2 = $Info
            If $Info[4] = $Pic Then
                If $In Then
                    $Draw = 1
                    _Draw()
                EndIf
            Else
                $In = 0
            EndIf
        EndIf
    Else
        If $Info[4] = $Pic Then
            _WinAPI_SetCursor($hCursor)
        EndIf
        $Draw = 0
        $In = 1
    EndIf
WEnd

Func _Draw()

    Local $hDC, $hMemDC, $hDup, $hPen, $hPenSv, $hPrev

    $hDC = _WinAPI_GetDC($hPic)
    $hMemDC = _WinAPI_CreateCompatibleDC($hDC)
    $hPrev = _WinAPI_SelectObject($hMemDC, $hBitmap)
    $hPen = _WinAPI_SelectObject($hMemDC, _WinAPI_GetStockObject($DC_PEN))
    _WinAPI_SetDCPenColor($hMemDC, $Color)
    If ($Pos1[0] = $Pos2[0]) And ($Pos1[1] = $Pos2[1]) Then
        _WinAPI_SetPixel($hMemDC, $Pos1[0], $Pos1[1], $Color)
    Else
        _WinAPI_DrawLine($hMemDC, $Pos1[0], $Pos1[1], $Pos2[0], $Pos2[1])
    EndIf
    _WinAPI_ReleaseDC($hPic, $hDC)
    _WinAPI_SelectObject($hMemDC, $hPrev)
    _WinAPI_DeleteObject(_WinAPI_SelectObject($hDC, $hPen))
    _WinAPI_DeleteDC($hMemDC)
    $hDup = _WinAPI_CopyBitmap($hBitmap)
    _WinAPI_DeleteObject(_SendMessage($hPic, $STM_SETIMAGE, 0, $hDup))
    $hPrev = _SendMessage($hPic, $STM_GETIMAGE)
    If $hPrev <> $hDup Then
        _WinAPI_DeleteObject($hDup)
    EndIf
EndFunc   ;==>_Draw

Func WM_SETCURSOR($hWnd, $iMsg, $wParam, $lParam)
    Switch $hWnd
        Case $hForm

            Local $Info = GUIGetCursorInfo()

            If (Not @error) And ($Info[4] = $Pic) And ($In) Then
                _WinAPI_SetCursor($hCursor)
                Return 0
            EndIf
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_SETCURSOR

Pen.cur

Edited by Yashied

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  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.