Jump to content

learn GDI


Recommended Posts

hello to all,

for different purpose i need to learn how to drawn square around desktop.

In most recent task i need to drawn 4 (or more) square on the right of screen

and change color when a key is pressed.

anyone can post simple GDI example to paint squares around desktop ?

thank you,

m.

Link to comment
Share on other sites

Hello again,

taken from help and modify:

#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>

Opt('MustDeclareVars', 1)

_Main()

Func _Main()
    Local $hGUI, $hWnd, $hGraphic

; Create GUI
    $hGUI = GUICreate("GDI+", 400, 300)
    $hWnd = WinGetHandle("GDI+")
    GUISetState(@SW_HIDE, $hGUI)
;~  GUISetState()


; Fill a rectangle
    _GDIPlus_Startup ()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND ($hWnd)
    _GDIPlus_GraphicsFillRect($hGraphic, 10, 10, 100, 100)

; Loop until user exits
    Do
    Until GUIGetMsg() = $GUI_EVENT_CLOSE

; Clean up resources
    _GDIPlus_GraphicsDispose ($hGraphic)
    _GDIPlus_Shutdown ()

EndFunc  ;==>_Main

Not working for my problem. I want to obtain floating GDI square on dekstop,

but if i hide gui also square disappear.

GDI object need a gui to exist ?

Anyone can help ?

Link to comment
Share on other sites

I modify my old cross GDI script and obtain 'floating' square on dekstop

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


my_square(500,400,100)

func my_square($start_x, $start_y, $mylenght)
    _GDIPlus_Startup ()
    $hDC = _WinAPI_GetWindowDC(0)

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


    For $y = 0 to 3000

            _GDIPlus_GraphicsDrawLine($hGraphic, $start_x , $start_y, $start_x + $mylenght, $start_y, $hPen);oriz UP
            _GDIPlus_GraphicsDrawLine($hGraphic, $start_x  + $mylenght, $start_y, $start_x + $mylenght, $start_y + $mylenght, $hPen);right

            _GDIPlus_GraphicsDrawLine($hGraphic, $start_x , $start_y + $mylenght, $start_x + $mylenght, $start_y + $mylenght, $hPen);oriz down          
            _GDIPlus_GraphicsDrawLine($hGraphic, $start_x , $start_y, $start_x, $start_y + $mylenght, $hPen);left
            
            $Color += Hex(2)
            _GDIPlus_PenSetColor($hPen, $Color)
    Next


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

EndFunc

First step is done.

Now:

Is possible to fill color into it when key is pressed ?

anyone ?

m.

Link to comment
Share on other sites

hello to all,

for different purpose i need to learn how to drawn square around desktop.

In most recent task i need to drawn 4 (or more) square on the right of screen

and change color when a key is pressed.

anyone can post simple GDI example to paint squares around desktop ?

thank you,

m.

I have played around with your request and 'am sorry the script is relatively not simple.

I am going through a layered windows stage at the moment, again, trying to understand their workings.

You may be able to use some of the stuff in it, in your own script.

The squares are able to be dragged as a whole by the top of the top square when that top square is filled. Where the hidden title bar of the GUI would be.

In the script, you can change the number of squares to be displayed,

the size of the squares to be displayed: and,

the spacing - distance between the squares.

I hope this helps more than hinders.

;
#include <GDIPlus.au3>
#include <GuiConstantsEx.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
#include <Misc.au3>

Opt("MouseCoordMode", 1) ;1=absolute, 0=relative, 2=client
Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

Main()


Func Main()
    Local $iSqNum = 5 ; Enter number of square required.
    Local $GuiSizeX = 101 ; Enter side size of square required.
    Local $iSqSpacing = 5 ; Enter spacing size for between square.

    Local $iOpacity = 255
    Local $hGui, $hGraphic, $aPos, $aWPos, $hBrush, $hBrush1, $gdibitmap, $hBackbuffer
    Local $tRect, $tPoint, $pPoint
    Local $ScreenDc, $dc, $pSize, $tSize, $pSource, $tSource, $pBlend, $tBlend
    Local $iX1 = @DesktopWidth - 110, $iY1 = 40, $GuiSizeY = ($GuiSizeX * $iSqNum) + ($iSqNum - 1) * $iSqSpacing
    Local $hBitmap, $Pinsel, $WinTitle = "Layered Window"

    Dim $flg[$iSqNum]
    For $x = 0 To UBound($flg) - 1
        $flg[$x] = False ; When false the square will not fill with colour.
    Next

    $hGui = GUICreate($WinTitle, $GuiSizeX, $GuiSizeY, $iX1, $iY1, 0, BitOR($WS_EX_LAYERED, $WS_EX_TOPMOST))
    GUISetState()

    _GDIPlus_Startup()
    $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGui)
    $hBitmap = _GDIPlus_BitmapCreateFromGraphics($GuiSizeX, $GuiSizeY, $hGraphic)
    $hBackbuffer = _GDIPlus_ImageGetGraphicsContext($hBitmap)
    $ScreenDc = _WinAPI_GetDC($hGui)

    _AntiAlias($hBackbuffer)

    $Pinsel = _GDIPlus_PenCreate(0xFFFF0000, 2) ; Border outline colour, 0xFFFF0000 fully opaque Red
    $hBrush = _GDIPlus_BrushCreateSolid(0xFFFF8080) ; Fill colour

    ; Draw graphics, $hBackbuffer, onto Bitmap, $hBitmap.
    _GDIPlus_GraphicsDrawRect($hBackbuffer, 0, 0, 100, 100, $Pinsel)
    $gdibitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap); Needs to exist for 1st run in loop
    $dc = _WinAPI_CreateCompatibleDC($ScreenDc)
    _WinAPI_SelectObject($dc, $gdibitmap)

    ; Following parameters for _WinAPI_UpdateLayeredWindow()
    $tSize = DllStructCreate($tagSIZE)
    $pSize = DllStructGetPtr($tSize)
    DllStructSetData($tSize, "X", $GuiSizeX)
    DllStructSetData($tSize, "Y", $GuiSizeY)
    $tSource = DllStructCreate($tagPOINT)
    $pSource = DllStructGetPtr($tSource)
    $tBlend = DllStructCreate($tagBLENDFUNCTION)
    $pBlend = DllStructGetPtr($tBlend)
    DllStructSetData($tBlend, "Alpha", $iOpacity)
    DllStructSetData($tBlend, "Format", 1)
    $tPoint = DllStructCreate($tagPOINT)
    $pPoint = DllStructGetPtr($tPoint)
    DllStructSetData($tPoint, "X", 0)
    DllStructSetData($tPoint, "Y", 0)

    Do
        If _IsPressed("01") Then ;When mouse key clicked on square, this toggles fill/not fill on that square
            Do
                Sleep(10)
            Until Not _IsPressed("01"); Prevent accidental multiple clicks/key presses on square
            $aPos = MouseGetPos()
            $aWPos = WinGetPos($WinTitle)
            For $x = 0 To UBound($flg) - 1
                If _WinAPI_PtInRectEx($aPos[0], $aPos[1], $aWPos[0], $aWPos[1] + ($x * ($GuiSizeX + $iSqSpacing)), _
                        $GuiSizeX, $GuiSizeX) Then $flg[$x] = Not $flg[$x] ; toggles draw true/false for that square.
            Next
        EndIf

        _WinAPI_DeleteObject($gdibitmap)

        ;_GDIPlus_GraphicsClear sets background colour.  "0x00000000",  100% transparent black is used
        ; With colour, "0x01000000", Alpha channel is 01, desktop not accessible through transparent looking squares.
        ; Colour, "0xFF000000" is 100% opaque black (no transparency). Hex colour format is 0xAARRGGBB (Alpha, Red, Green, Blue.)
        _GDIPlus_GraphicsClear($hBackbuffer, "0x00000000");

        For $x = 0 To UBound($flg) - 1
            If $flg[$x] Then _GDIPlus_GraphicsFillRect($hBackbuffer, 0, $x * ($GuiSizeX + $iSqSpacing), $GuiSizeX - 1, $GuiSizeX - 1, $hBrush)
            _GDIPlus_GraphicsDrawRect($hBackbuffer, 0, $x * ($GuiSizeX + $iSqSpacing), $GuiSizeX - 1, $GuiSizeX - 1, $Pinsel)
        Next
        $gdibitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap)
        _WinAPI_SelectObject($dc, $gdibitmap)
        _WinAPI_UpdateLayeredWindow($hGui, $ScreenDc, 0, $pSize, $dc, $pSource, 0, $pBlend, 2)
        Sleep(10)
    Until _IsPressed("1B"); ESC key

    _GDIPlus_BrushDispose($hBrush)
    _GDIPlus_PenDispose($Pinsel)
    _GDIPlus_GraphicsDispose($hBackbuffer)
    _GDIPlus_GraphicsDispose($hGraphic)
    _WinAPI_ReleaseDC(0, $hGui)
    _WinAPI_DeleteObject($hBitmap)
    _WinAPI_DeleteDC($dc)
    _GDIPlus_Shutdown()
EndFunc   ;==>Main

Func _AntiAlias($hGraphics)
    Local $aResult
    $aResult = DllCall($ghGDIPDll, "int", "GdipSetSmoothingMode", "hwnd", $hGraphics, "int", 2)
    If @error Then Return SetError(@error, @extended, False)
    Return SetError($aResult[0], 0, $aResult[0] = 0)
EndFunc   ;==>_AntiAlias

Func _WinAPI_PtInRectEx($iX, $iY, $iLeft, $iTop, $iWidth, $iHeight)
    Local $aResult, $tagREC = "int Left;int Top;int Right;int Bottom"
    Local $tRect = DllStructCreate($tagREC)
    DllStructSetData($tRect, "Left", $iLeft)
    DllStructSetData($tRect, "Top", $iTop)
    DllStructSetData($tRect, "Right", $iLeft + $iWidth)
    DllStructSetData($tRect, "Bottom", $iTop + $iHeight)
    $aResult = DllCall("User32.dll", "int", "PtInRect", "ptr", DllStructGetPtr($tRect), "int", $iX, "int", $iY)
    If @error Then Return SetError(@error, 0, False)
    Return $aResult[0] <> 0
EndFunc   ;==>_WinAPI_PtInRectEx
;
Link to comment
Share on other sites

As usual excellent work.

Hard code for me, but now i'm reading. (studing)

Initial request is explained in this image

Posted Image

One of my script monitor mouse left button hold down,

and run things if mouse button is pressed for 2-4-5-6 seconds.

Sound advice user but i want some alerts on screen.

Can't find progress bar, so think to GDI. But is hard to understant and use for me.

Hope posted image can help and simplify request.

Thank you,

m.

Edited by myspacee
Link to comment
Share on other sites

Try to modify your script but it's a bit too hard for me.

so adjust mine to explain better my 'charge' GDI progress bar...

(hold down left mouse button to draw squares)

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

dim $f_number = 0


while 1

    If _IsPressed("01") Then;monitor left mouse button
        
        if $f_number = 0 Then
            my_square(500,700,100)
            $f_number = $f_number + 1
            
        elseif $f_number = 1 Then
            my_square(500,600,100)
            $f_number = $f_number + 1
            
        elseif $f_number = 2 Then
            my_square(500,500,100)
            $f_number = $f_number + 1
            
        elseif $f_number = 3 Then   
            my_square(500,400,100)
            $f_number = 0
            
        EndIf


        
    elseif _IsPressed("1B") Then;ESC to quit
        _WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE+$RDW_ALLCHILDREN)
        _WinAPI_ReleaseDC(0, $hDC)
        _GDIPlus_Shutdown()
        Exit
        
        
    EndIf

    Sleep(10)
WEnd


func my_square($start_x, $start_y, $mylenght)
    _GDIPlus_Startup ()
    $hDC = _WinAPI_GetWindowDC(0)

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


    _GDIPlus_GraphicsDrawLine($hGraphic, $start_x , $start_y, $start_x + $mylenght, $start_y, $hPen);oriz UP
    _GDIPlus_GraphicsDrawLine($hGraphic, $start_x  + $mylenght, $start_y, $start_x + $mylenght, $start_y + $mylenght, $hPen);right
    _GDIPlus_GraphicsDrawLine($hGraphic, $start_x , $start_y + $mylenght, $start_x + $mylenght, $start_y + $mylenght, $hPen);oriz down          
    _GDIPlus_GraphicsDrawLine($hGraphic, $start_x , $start_y, $start_x, $start_y + $mylenght, $hPen);left
    
    $Color += Hex(2)
    _GDIPlus_PenSetColor($hPen, $Color)
    
    
    if $f_number = 3 Then;destroy all for restart
        _WinAPI_RedrawWindow(_WinAPI_GetDesktopWindow(), 0, 0, $RDW_INVALIDATE+$RDW_ALLCHILDREN)
        _WinAPI_ReleaseDC(0, $hDC)
        _GDIPlus_Shutdown()
    EndIf
    
    sleep(500)
    
    _GDIPlus_Shutdown()
EndFunc

Very simple code but works. With too flickers and without color fill...

Anyone can adjust code and teach me some tricks ?

thank you!

m.

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