Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Progress Bar Slider


  • Please log in to reply
5 replies to this topic
Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
This is a base script I have made to use Clickable Progress Bars as Slider controls. The bar's value is set to mouse position when clicked.
It currently supports up to 9 bars, and they work independent of size, range or position in gui. I might improve it sometime so it can support even more bars and vertical bars too.

;
; ::Progress Bar Slider::
;
; Author: Pulover [Rodolfo U. Batista]
; [email protected]
; URL: https://ahknet.autohotkey.com/~Pulover/ProgressBarSlider.ahk
;

MaxRange1 := 100
MaxRange2 := 200
MaxRange3 := 300
MaxRange4 := 500
MaxRange5 := 1000

margin := 4  ; Margin compensation of the window. Change to 0 for borderless windows.

;Gui, +Owner +LastFound +AlwaysOnTop +ToolWindow -Caption
Gui, Add, Progress, w300 h20 cGreen BackgroundLime vPrBar1 Range0-%MaxRange1%, 100
Gui, Add, Progress, x20 w500 h15 cRed BackgroundYellow vPrBar2 Range0-%MaxRange2%, 200
Gui, Add, Progress, x80 y60 w600 h40 cBlue vPrBar3 Range0-%MaxRange3%, 300
Gui, Add, Progress, x40 w150 h20 -Smooth vPrBar4 Range0-%MaxRange4%, 500
Gui, Add, Progress, x200 yp w450 h20 cBlack BackgroundSilver vPrBar5 Range0-%MaxRange5%, 1000
Gui, Show, w700 h140, Progress Bar Slider by Pulover
OnMessage(0x201, "WM_LBUTTONDOWN")  ; Monitors Left Clicks on Gui.
Loop, 9
   GuiControlGet, PrBar%A_Index%, Pos
return

WM_LBUTTONDOWN(wParam, lParam)
{
   global id
   MouseGetPos, mX,,, control
   If !InStr(control, "msctls_progress32")  ; If the click wasn't in the progress bar, do nothing.
      return
   id := SubStr(control, 18, 1)
   SetTimer, WatchCursor, 0
}

WatchCursor:
If !GetKeyState("LButton", "P")
{
   Tooltip
   SetTimer, WatchCursor, off
   return
}
MouseGetPos, mX  ; Gets mouse position relative to the Window.
mX -= PrBar%id%X + margin
X := floor(MaxRange%id% / (PrBar%id%W/mX))
Tooltip, % PBar(X)
GuiControl,, PrBar%id%, % PBar(X)  ; Sets progress bar to mouse position.
return

PBar(X)
{
   global
   If X < 0
      return 0
   If (X > MaxRange%id%)
      return MaxRange%id%
   Else
      return X
}

GuiClose:
GuiEscape:
ExitApp

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


JSLover
  • Members
  • 920 posts
  • Last active: Nov 02 2012 09:54 PM
  • Joined: 20 Dec 2004
If you don't mind, I have some constructive comments about your code...

[*:3nq2h02f]This line is useless, the default CoordMode is already Window...

CoordMode, Mouse, Window

[*:3nq2h02f]This line is unnecessary (& it can cause problems in bigger scripts, that might need a different float value), you can use floor() on the X value to make it not be floating point...

SetFormat, Float, 0.0  ; For the Tooltip only.

[*:3nq2h02f]I haven't analyzed your new code that much yet, but is this...

margin := 4  ; Margin of the window. Change to 0 for borderless windows.
...a hard-coded margin value? You don't use Gui, Margin anywhere. You only use this var in the calculations...so how do you know the Gui's margin is 4?


[*:3nq2h02f]You still use the "magic number" 0x201...

OnMessage(0x201, "WM_LBUTTONDOWN")  ; Monitors Left Clicks on Gui.
...yes "WM_LBUTTONDOWN" is right next to it, as the function's name, but you can use any function name...

I'd do this instead...

WM_LBUTTONDOWN:=0x201
OnMessage(WM_LBUTTONDOWN, "WM_LBUTTONDOWN")
...then you can even change the function's name & it still makes sense...

WM_LBUTTONDOWN:=0x201
OnMessage(WM_LBUTTONDOWN, "HandleMouseButton")

[*:3nq2h02f]You changed this in your code...

If !InStr(control, "msctls_progress32")  ; If the click wasn't in the progress bar, do nothing.
		return
...from my code...

if (InStr(control, "msctls_progress32")!=1) {
		return
	}
...which changed the meaning. I compare the InStr() result against 1, on purpose, since I only want a match if the string BEGINS with "msctls_progress32", not, on the off chance, that it contains "msctls_progress32" somewhere else in it (rare & unlikely, but possible).


[*:3nq2h02f]Here...

X := MaxRange%id% / (PrBar%id%W/mX)
...is where you should floor() (or round() or whatever)...

X:=floor(MaxRange%id% / (PrBar%id%W/mX))

[*:3nq2h02f]I don't think you should lie in the tooltip & set the progress to something else...why not just limit both?

Tooltip, % PBar(X)
GuiControl,, PrBar%id%, %X%  ; Sets progress bar to mouse position.

Useful forum links: New content since: Last visitPast weekPast 2 weeks (links will show YOUR posts, not mine)

OMFG, the AutoHotkey forum is IP.board now (yuck!)...I may not be able to continue coming here (& I love AutoHotkey)...I liked phpBB, but not this...ugh...

Note...
I may not reply to any topics (specifically ones I was previously involved in), mostly cuz I can't find the ones I replied to, to continue helping, but also just cuz I can't stand the new forum...phpBB was soo perfect. This is 100% the opposite of "perfect".

I also semi-plan to start my own, phpBB-based AutoHotkey forum (or take over the old one, if he'll let me)
PM me if you're interested in a new phpBB-based forum (I need to know if anyone would use it)
How (or why) did they create the Neil Armstrong memorial site (neilarmstronginfo.com) BEFORE he died?

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Thanks for the tips, JSLover. Please, don't get me wrong, I'm not trying to compete here...
First I must say I'm not really a programmer and I know my code is simple, but it was my idea and I liked to try this.

About your comments...
You are right about coordmode, I used another script as base and it used screen, I just forgot I could simply remove it.
The setformat is for the tooltip as I said and anyone who would use it could remove this part, I just couldn't think of something else at that moment.
The margin is more like a typo, I meant to write "margin compensation", but I changed and missed that.
I know I can give any name to the function, I just took that example from the help file.
If !InStr is something I usually use in these situations, I didn't even know the difference from your code.
Following your suggestions I used floor in the calc and the same function of tooltip to set the value to the progress bar. Thanks.

I may not do much more with this script, but I'm happy I could get it to work, at least.

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


nothing
  • Members
  • 129 posts
  • Last active: Oct 03 2014 04:51 AM
  • Joined: 09 Jan 2010
It suprised me that there is no g-Label in Progress Ctrl :oops:
Never mind, i don't often use this control :lol: . However,if someone need,here is a simple one that may help:
Function:
;~ ################XClickableProgress################
;Usage: you have to put OnMessage(0x201,"XClickableProgress") on the very top of the Script to use XClickableProgress() Function. (XClickableProgress() Function can be put anywhere).That all.
;~ Note:It may work with Horizontal & Vertical Progress Ctrls,negative range numbers in progress ctrls, multi-GUI and unlimited amount of progress ctrls.
;~ ################BEGIN##################
OnMessage(0x201,"XClickableProgress") ; this line shoud be move to the top of your script
XClickableProgress() {
 CoordMode,Mouse,Relative
 MouseGetPos,,,,ClassNN
 IfNotInString,ClassNN,msctls_progress32,return
 MouseGetPos,,,,CtrlHwnd,2
 ControlGet,Style,Style,,,ahk_id%CtrlHwnd%
 ControlGetPos,X,Y,W,H,,ahk_id%CtrlHwnd%
 VarSetCapacity(R,8)
 SendMessage,0x0407,,&R,,ahk_id%CtrlHwnd%
 R1:=NumGet(R,0,"Int"),R2:=NumGet(R,4,"Int")
 while (GetKeyState("LButton"))
 {
  MouseGetPos,XM,YM
  ToolTip % V := (V:=(Style&0x4 ? 1-(YM-Y)/H : (XM-X)/W))>=1 ? R2 : V<=0 ? R1 : Round(V*(R2-R1)+R1)
  GuiControl,% A_Gui ":",% CtrlHwnd,% V
 }
 ToolTip
}
;~ #############END#############
Some Simple Examples:
Ex1: Pulover's Example (I hope you don't mind :wink: )
;~ ################XClickableProgress################
;Usage: you have to put OnMessage(0x201,"XClickableProgress") on the very top of the Script to use XClickableProgress() Function. (XClickableProgress() Function can be put anywhere).That all.
;~ Note:It may work with Horizontal & Vertical Progress Ctrls,negative range numbers in progress ctrls, multi-GUI and unlimited amount of progress ctrls.
;~ ################BEGIN##################
OnMessage(0x201,"XClickableProgress")
XClickableProgress() {
 CoordMode,Mouse,Relative
 MouseGetPos,,,,ClassNN
 IfNotInString,ClassNN,msctls_progress32,return
 MouseGetPos,,,,CtrlHwnd,2
 ControlGet,Style,Style,,,ahk_id%CtrlHwnd%
 ControlGetPos,X,Y,W,H,,ahk_id%CtrlHwnd%
 VarSetCapacity(R,8)
 SendMessage,0x0407,,&R,,ahk_id%CtrlHwnd%
 R1:=NumGet(R,0,"Int"),R2:=NumGet(R,4,"Int")
 while (GetKeyState("LButton"))
 {
  MouseGetPos,XM,YM
  ToolTip % V := (V:=(Style&0x4 ? 1-(YM-Y)/H : (XM-X)/W))>=1 ? R2 : V<=0 ? R1 : Round(V*(R2-R1)+R1)
  GuiControl,% A_Gui ":",% CtrlHwnd,% V
 }
 ToolTip
}
;~ #############END#############
MaxRange1 := 100
MaxRange2 := 200
MaxRange3 := 300
MaxRange4 := 500
MaxRange5 := 1000

margin := 4  

Gui, Add, Progress, w300 h20 cGreen BackgroundLime vPrBar1 Range0-%MaxRange1%, 100
Gui, Add, Progress, x20 w500 h15 cRed BackgroundYellow vPrBar2 Range0-%MaxRange2%, 200
Gui, Add, Progress, x80 y60 w600 h40 cBlue vPrBar3 Range0-%MaxRange3%, 300
Gui, Add, Progress, x40 w150 h20 -Smooth vPrBar4 Range0-%MaxRange4%, 500
Gui, Add, Progress, x200 yp w450 h20 cBlack BackgroundSilver vPrBar5 Range0-%MaxRange5%, 1000
Gui, Show, w700 h140, Progress Bar Slider by Pulover
Loop, 9
   GuiControlGet, PrBar%A_Index%, Pos
return

GuiClose:
GuiEscape:
ExitApp
Ex2:
;~ ################XClickableProgress################
;Usage: you have to put OnMessage(0x201,"XClickableProgress") on the very top of the Script to use XClickableProgress() Function. (XClickableProgress() Function can be put anywhere).That all.
;~ Note:It may work with Horizontal & Vertical Progress Ctrls,negative range numbers in progress ctrls, multi-GUI and unlimited amount of progress ctrls.
;~ ################BEGIN##################
OnMessage(0x201,"XClickableProgress")
XClickableProgress() {
 CoordMode,Mouse,Relative
 MouseGetPos,,,,ClassNN
 IfNotInString,ClassNN,msctls_progress32,return
 MouseGetPos,,,,CtrlHwnd,2
 ControlGet,Style,Style,,,ahk_id%CtrlHwnd%
 ControlGetPos,X,Y,W,H,,ahk_id%CtrlHwnd%
 VarSetCapacity(R,8)
 SendMessage,0x0407,,&R,,ahk_id%CtrlHwnd%
 R1:=NumGet(R,0,"Int"),R2:=NumGet(R,4,"Int")
 while (GetKeyState("LButton"))
 {
  MouseGetPos,XM,YM
  ToolTip % V := (V:=(Style&0x4 ? 1-(YM-Y)/H : (XM-X)/W))>=1 ? R2 : V<=0 ? R1 : Round(V*(R2-R1)+R1)
  GuiControl,% A_Gui ":",% CtrlHwnd,% V
 }
 ToolTip
}
;~ #############END#############
Gui, Font, s10, MS Shell Dlg
Gui, Color, Green, FFFFFF
Gui, Add, Progress, x10 y10 w230 h30 range100000-100000000,25
Gui, Add, Progress, x10 y260 w230 h30 range-1000000--1000,25
Gui, Add, Progress, x10 y40 w40 h220 +0x4 range0-100000,25
Gui, Add, Progress, x200 y40 w40 h220 +0x4 range-10000000--1000,25
Gui, Add, Button, x90 y130 w65 h30,nothing :D
Gui, Show, w253 h304, 
Return

GuiClose:
ExitApp


nothing is impossible with ahk (_L).
Excuse my bad English.
Busy

Pulover
  • Members
  • 1596 posts
  • Last active: Apr 06 2016 04:00 AM
  • Joined: 20 Apr 2012
Very nice, nothing. I don't have a use for that yet, so I was thinking about trying other methods later, but yours works fine already.

Rodolfo U. Batista
Pulover's Macro Creator - Automation Tool (Recorder & Script Writer) | Class_LV_Rows - Copy, Cut, Paste and Drag ListViews | Class_Toolbar - Create and modify | Class_Rebar - Adjustable GUI controls

Join the New AutoHotkey Forum!


nothing
  • Members
  • 129 posts
  • Last active: Oct 03 2014 04:51 AM
  • Joined: 09 Jan 2010
:) glad to hear that.
nothing is impossible with ahk (_L).
Excuse my bad English.
Busy