Jump to content

a moving bar with mouse control


nobbe
 Share

Recommended Posts

;
; a gradient bar with mouse control
;
; draw a bar on the dialog , move mouse in bar to get percentage of bar
;
; or set bar position manually with button control 
;
;
; by nobbe 2008 


#include <GuiConstants.au3>
#include <WinApi.au3>
#include <GdiPlus.au3>

; for mouse coord on control
Opt("MouseCoordMode", 2)

Global $user32_dll = DllOpen("user32.dll"); for mouse
Global $ahCallBack[2], $iPercent;

;; global for bar position
Global $bar_start_x = 10;
Global $bar_start_y = 10;
Global $bar_width = 300;
Global $bar_height = 20;

Global $framewidth = 2 ; 2 pixel are black & white frame from control

;; ------- GUI

$Gui = GUICreate("Gradient ProgressBar", 400, 300)
$bar1 = GUICtrlCreateLabel("", $bar_start_x, $bar_start_y, $bar_width, $bar_height, -1, $WS_EX_STATICEDGE) ;; check only for first bar
$Status_Label = GUICtrlCreateLabel("0%", 330, 30, 30, 20)

$btn_25 = GUICtrlCreateButton("25", 20, 100, 75, 25, 0)
$btn_50 = GUICtrlCreateButton("50", 120, 100, 75, 25, 0)
$btn_75 = GUICtrlCreateButton("75", 220, 100, 75, 25, 0)


;;; ---- GDI

_GDIPlus_Startup ()

;; create blue color Bar

$hGraphic_bar_left_side = _GDIPlus_GraphicsCreateFromHWND ($Gui)
$hBitMap_bar_left_side = _GDIPlus_BitmapCreateFromGraphics ($bar_width, 20, $hGraphic_bar_left_side)
$hGraphic_tmp1 = _GDIPlus_ImageGetGraphicsContext ($hBitMap_bar_left_side)
$hPen = _GDIPlus_PenCreate (0, 1)
$Depth = 1.4 ; 1.70 ; Min = 1.10  Max = 1.70

For $i = 1 To $bar_height
    ;  $PenColor = "0xFF" & Hex(255 - $i, 2) & Hex(Round(240 - $i^$Depth), 2) & "3B" ; Orange
    ;  $PenColor = "0xFF" & Hex(255 - $i, 2) & Hex(Round(200 - $i^$Depth), 2) & "FF" ; Magenta
    ;  $PenColor = "0xFF" & Hex(80  + $i, 2) & Hex(Round(240 - $i^$Depth), 2) & "3B" ; Green
    ;   $PenColor = "0xFF" & Hex(80 + $i, 2) & Hex(Round(240 - $i ^ $Depth), 2) & "FF" ; Blue

    ;; alpha / r,g,b

    $PenColor = "0xFF" & Hex(10 + $i, 2) & Hex(Round(80 - $i ^ $Depth), 2) & "FF" ; Blue
    _GDIPlus_PenSetColor ($hPen, $PenColor)
    _GDIPlus_GraphicsDrawLine ($hGraphic_tmp1, 0, $i - 1, $bar_width, $i - 1, $hPen)
Next

_GDIPlus_PenDispose ($hPen)
_GDIPlus_GraphicsDispose ($hGraphic_tmp1)


;; create grey color Bar  on the right side

$hGraphic_bar_right_side = _GDIPlus_GraphicsCreateFromHWND ($Gui)
$hBitMap_bar_right_side = _GDIPlus_BitmapCreateFromGraphics ($bar_width, 20, $hGraphic_bar_right_side)
$hGraphic_tmp2 = _GDIPlus_ImageGetGraphicsContext ($hBitMap_bar_right_side)
$hPen = _GDIPlus_PenCreate (0, 1)
;; flat now
For $i = 1 To $bar_height
    $PenColor = "0xFFE0DFE3" ; some grey
    _GDIPlus_PenSetColor ($hPen, $PenColor)
    _GDIPlus_GraphicsDrawLine ($hGraphic_tmp2, 0, $i - 1, $bar_width, $i - 1, $hPen)
Next

_GDIPlus_PenDispose ($hPen)
_GDIPlus_GraphicsDispose ($hGraphic_tmp2)

;; -- vs NOD ----------------------

;; start call back now
$ahCallBack = CallBack_Init("Paint_Bar_Procedure", 200) ;; with millisec timer

GUISetState()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg


        Case $btn_25
            _move_bar(25);
        Case $btn_50
            _move_bar(50);
        Case $btn_75
            _move_bar(75);


        Case $GUI_EVENT_CLOSE
            CallBack_Free($ahCallBack)

            _GDIPlus_ImageDispose ($hBitMap_bar_left_side)
            _GDIPlus_GraphicsDispose ($hGraphic_bar_left_side)
            
            _GDIPlus_ImageDispose ($hBitMap_bar_right_side)
            _GDIPlus_GraphicsDispose ($hGraphic_bar_right_side)

            _GDIPlus_Shutdown ()

            Exit
    EndSwitch
    



    ;; my mouse BTN LEFT is down --
    
    If _IsPressed("01", $user32_dll) Then
        While _IsPressed("01", $user32_dll)
            
            $pos = MouseGetPos()
            
            $pos[0] += $framewidth; add frame with of control
            
            
            If ($pos[0] >= $bar_start_x) And ($pos[0] <= ($bar_width + $bar_start_x)) And _
                    ($pos[1] >= $bar_start_y) And ($pos[1] <= ($bar_height + $bar_start_y)) Then  ;; are we within bar ??
                
                $iPercent = Int(($pos[0] - $bar_start_x) / $bar_width * 100);
                
                
                ;$position_in_bar = $bar_width / 100 * $iPercent
                _DebugPrint("Mouse is x,y:" & $pos[0] & "," & $pos[1] & " percent : " & $iPercent);
            EndIf
        WEnd
    EndIf ; mouse BTN 1 pressed??



WEnd



;; other function now
Func Paint_Bar_Procedure($hWnd, $nMsg, $wParam, $lParam)

; move the bar 
    _move_bar($iPercent);
EndFunc   ;==>Paint_Bar_Procedure

; 

Func CallBack_Init($sFuncName, $iTime, $sParam = "hwnd;int;int;dword")
    Local $hCallBack = DLLCallbackRegister ($sFuncName, "int", $sParam)

    Local $aTimer = DllCall("user32.dll", "uint", "SetTimer", _
            "hwnd", 0, "uint", 0, "int", $iTime, "ptr", DllCallbackGetPtr ($hCallBack))

    Local $ahCallBack[2] = [$hCallBack, $aTimer[0]]

    Return $ahCallBack
EndFunc   ;==>CallBack_Init


;
;
Func CallBack_Free($ahCallBack)
    If $ahCallBack[0] <> -1 Then DllCallbackFree ($ahCallBack[0])
    If $ahCallBack[1] <> -1 Then DllCall("user32.dll", "int", "KillTimer", "hwnd", 0, "uint", $ahCallBack[1])
EndFunc   ;==>CallBack_Free

;
;
;
Func _DebugPrint($s_text)
    $s_text = StringReplace($s_text, @LF, @LF & "-")
    ConsoleWrite($s_text & @LF); & _
EndFunc   ;==>_DebugPrint


;
; draws 2 bars , left is blue , right is grey 
; 
FUNC _move_bar($prc)

    $iPercent = $prc 

    GUICtrlSetData($Status_Label, $prc & "%")
    $position_in_bar = $bar_width / 100 * $prc - ($framewidth * 2) ;; or we move out the bar

    ;; draw blue bar from left
    _GDIPlus_GraphicsDrawImageRectRect ($hGraphic_bar_left_side, $hBitMap_bar_left_side, _
            0, 0, $prc, $bar_height, _
            $bar_start_x + $framewidth, _
            $bar_start_y + $framewidth, _
            $position_in_bar, _
            $bar_height - ($framewidth * 2))

    ; draw grey bar to right side
    _GDIPlus_GraphicsDrawImageRectRect ($hGraphic_bar_right_side, $hBitMap_bar_right_side, 0, 0, $prc, $bar_height, _
            ($position_in_bar + $bar_start_x) + $framewidth, _
            $bar_start_y + $framewidth, _
            $bar_width - $position_in_bar - ($framewidth * 2), _
            $bar_height - $framewidth * 2);
endfunc

Link to comment
Share on other sites

I see no gradient.

the gradient is only slightly there

if you change the values you can adjust it

$hGraphic_bar_left_side = _GDIPlus_GraphicsCreateFromHWND ($Gui)

$hBitMap_bar_left_side = _GDIPlus_BitmapCreateFromGraphics ($bar_width, $bar_height, $hGraphic_bar_left_side)

Local $hGraphic_tmp1 = _GDIPlus_ImageGetGraphicsContext ($hBitMap_bar_left_side)

Local $hPen = _GDIPlus_PenCreate (0, 1)

--->>> for more gradient 1.7

Local $Depth = 1.4 ; 1.70 ; Min = 1.10 Max = 1.70

Local $i

For $i = 1 To $bar_height

; $PenColor = "0xFF" & Hex(255 - $i, 2) & Hex(Round(240 - $i^$Depth), 2) & "3B" ; Orange

; $PenColor = "0xFF" & Hex(255 - $i, 2) & Hex(Round(200 - $i^$Depth), 2) & "FF" ; Magenta

; $PenColor = "0xFF" & Hex(80 + $i, 2) & Hex(Round(240 - $i^$Depth), 2) & "3B" ; Green

--->>> for more gradient

; $PenColor = "0xFF" & Hex(80 + $i, 2) & Hex(Round(240 - $i ^ $Depth), 2) & "FF" ; Blue

;; alpha / r,g,b

$PenColor = "0xFF" & Hex(10 + $i, 2) & Hex(Round(80 - $i ^ $Depth), 2) & "FF" ; Blue

_GDIPlus_PenSetColor ($hPen, $PenColor)

_GDIPlus_GraphicsDrawLine ($hGraphic_tmp1, 0, $i - 1, $bar_width, $i - 1, $hPen)

Next

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