1. André Rust
  2. PowerBuilder
  3. Wednesday, 19 July 2023 06:28 AM UTC

Hi

How can I use the Windows progress bar in PowerBuilder (Windows API)? Please with an example.

I use PB19R3.

 

Thanks.

André

Accepted Answer
Benjamin Gaesslein Accepted Answer Pending Moderation
  1. Wednesday, 19 July 2023 11:32 AM UTC
  2. PowerBuilder
  3. # Permalink

Hi André,

to create a default Windows progressbar in PB, you can use the CreateWindowEx api function with the PROGRESS_CLASS window class. Here's the source code for a small window that implements this, with a command button to step through the progress bar.

 

forward
global type w_progressbar from window
end type
type cb_increment from commandbutton within w_progressbar
end type
end forward

global type w_progressbar from window
integer width = 1317
integer height = 540
boolean titlebar = true
string title = "Progressbar"
boolean controlmenu = true
boolean minbox = true
boolean maxbox = true
boolean resizable = true
string icon = "AppIcon!"
boolean center = true
cb_increment cb_increment
end type
global w_progressbar w_progressbar

type prototypes
private:
	protected Function long CreateWindowExW( &
		ulong dwExStyle, &
		string ClassName, &
		long WindowName, &
		ulong dwStyle, &
		ulong UX, &
		ulong UY, &
		ulong nWidth, &
		ulong nHeight, &
		ulong hWndParent, &
		ulong hMenu, &
		ulong hInstance, &
		ulong lpParam ) library "user32.dll"
end prototypes

type variables
private:
	long hProgress
	// Window constants
	constant long WS_CHILD = 1073741824
	constant long WS_VISIBLE = 268435456
	
	// Progressbar window class
	constant string PROGRESS_CLASS = 'msctls_progress32'
	
	// Message constants, we only use a few of these but I'll leave them for posterity
	constant long CCM_FIRST = 8192 // &H2000
	constant long CCM_SETBKCOLOR = CCM_FIRST + 1
	constant long PBM_SETBKCOLOR = CCM_SETBKCOLOR // Sets the Progress Bar background color
	
	constant long WM_USER 		= 1024
	constant long PBM_SETRANGE = (WM_USER + 1) // Sets the total low & high of the Progress Bar
	constant long PBM_SETPOS = (WM_USER + 2) // Sets the current % position of the Progress Bar
	constant long PBM_DELTAPOS = (WM_USER + 3)
	constant long PBM_SETSTEP = (WM_USER + 4)
	constant long PBM_STEPIT = (WM_USER + 5)
	constant long PBM_SETRANGE32 = (WM_USER + 6)
	constant long PBM_GETRANGE = (WM_USER + 7)
	constant long PBM_GETPOS = (WM_USER + 8)
	constant long PBM_SETBARCOLOR = (WM_USER + 9) // Sets the color of the Progress Bar
	constant long PBM_SETMARQUEE = (WM_USER + 10)
	constant long PBM_GETSTEP = (WM_USER + 13)
	constant long PBM_GETBKCOLOR = (WM_USER + 14) // Get Progress Bar Background Color
	constant long PBM_GETBARCOLOR = (WM_USER + 15) // Get Progress Bar bar Color
	constant long PBM_SETSTATE = (WM_USER + 16) // NORMAL, ERROR, PAUSED
	constant long PBM_GETSTATE = (WM_USER + 17)
end variables

on w_progressbar.create
this.cb_increment=create cb_increment
this.Control[]={this.cb_increment}
end on

on w_progressbar.destroy
destroy(this.cb_increment)
end on

event open;hProgress = CreateWindowExW( &
	0, &
	PROGRESS_CLASS, &
	0, &
	WS_CHILD + WS_VISIBLE, &
	10, &
	10, &
	UnitsToPixels( this.width, XUnitsToPixels!) - 30, &
	15, &
	Handle( this ), &
	0, &
	Handle ( GetApplication() ), &
	0 )

// Setting min and max values
// Max value needs to be bitshifted by 16 here because the higher 16 bits of the 32 bit variable is used here
Send(hProgress, PBM_SETRANGE, 0, 100 * 2^16 )

// Setting the step size, 10 is the default
Send(hProgress, PBM_SETSTEP, 5, 0)
end event

type cb_increment from commandbutton within w_progressbar
integer x = 489
integer y = 308
integer width = 343
integer height = 92
integer taborder = 10
integer textsize = -10
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Progress!"
boolean flatstyle = true
end type

event clicked;
// Increment progress bar
Send( hProgress , PBM_STEPIT, 0, 0 )
end event

Comment
  1. André Rust
  2. Wednesday, 19 July 2023 11:51 AM UTC
Hi Benjamin,



thank you very much for your help. I will do it this way and hope it works.



André
  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 19 July 2023 13:52 PM UTC
FWIW: I would use PB's built-in progress bar vs Windows API's then you are A) future proofed for new O/S versions and B) ready for running your PB Apps in x64 bit mode. Food for thought.
  1. Helpful
  1. Benjamin Gaesslein
  2. Thursday, 20 July 2023 05:39 AM UTC
PB's built-in control is of course easier to handle and works just as well.
  1. Helpful
There are no comments made yet.
Roland Smith Accepted Answer Pending Moderation
  1. Wednesday, 19 July 2023 12:59 PM UTC
  2. PowerBuilder
  3. # 1

I'm not sure how this is different from the built-in HProgressBar control.

Some of the data types are incorrect, handles should be defined as LongPtr so that it will work in 64bit.

forward
global type w_progressbar from window
end type
type cb_increment from commandbutton within w_progressbar
end type
end forward

global type w_progressbar from window
integer width = 1248
integer height = 652
boolean titlebar = true
string title = "Progressbar"
boolean controlmenu = true
boolean minbox = true
boolean maxbox = true
boolean resizable = true
string icon = "AppIcon!"
boolean center = true
cb_increment cb_increment
end type
global w_progressbar w_progressbar

type prototypes
Private:

Function longptr CreateWindowExW( &
	ulong dwExStyle, &
	string ClassName, &
	longptr WindowName, &
	ulong dwStyle, &
	long UX, &
	long UY, &
	long nWidth, &
	long nHeight, &
	longptr hWndParent, &
	longptr hMenu, &
	longptr hInstance, &
	longptr lpParam &
	) Library "user32.dll"

end prototypes

type variables
Private:

LongPtr hProgress

// Window constants
Constant ULong WS_CHILD			= 1073741824
Constant ULong WS_VISIBLE		= 268435456

// Progressbar window class
Constant String PROGRESS_CLASS = 'msctls_progress32'

// Message constants, we only use a few of these but I'll leave them for posterity
Constant UInt CCM_FIRST			= 8192 // &H2000
Constant UInt CCM_SETBKCOLOR	= CCM_FIRST + 1
Constant UInt PBM_SETBKCOLOR	= CCM_SETBKCOLOR // Sets the Progress Bar background color

Constant UInt WM_USER			= 1024
Constant UInt PBM_SETRANGE		= (WM_USER + 1) // Sets the total low & high of the Progress Bar
Constant UInt PBM_SETPOS		= (WM_USER + 2) // Sets the current % position of the Progress Bar
Constant UInt PBM_DELTAPOS		= (WM_USER + 3)
Constant UInt PBM_SETSTEP		= (WM_USER + 4)
Constant UInt PBM_STEPIT		= (WM_USER + 5)
Constant UInt PBM_SETRANGE32	= (WM_USER + 6)
Constant UInt PBM_GETRANGE		= (WM_USER + 7)
Constant UInt PBM_GETPOS		= (WM_USER + 8)
Constant UInt PBM_SETBARCOLOR	= (WM_USER + 9) // Sets the color of the Progress Bar
Constant UInt PBM_SETMARQUEE	= (WM_USER + 10)
Constant UInt PBM_GETSTEP		= (WM_USER + 13)
Constant UInt PBM_GETBKCOLOR	= (WM_USER + 14) // Get Progress Bar Background Color
Constant UInt PBM_GETBARCOLOR	= (WM_USER + 15) // Get Progress Bar bar Color
Constant UInt PBM_SETSTATE		= (WM_USER + 16) // NORMAL, ERROR, PAUSED
Constant UInt PBM_GETSTATE		= (WM_USER + 17)

end variables

on w_progressbar.create
this.cb_increment=create cb_increment
this.Control[]={this.cb_increment}
end on

on w_progressbar.destroy
destroy(this.cb_increment)
end on

event open;
hProgress = CreateWindowExW( &
	0, &
	PROGRESS_CLASS, &
	0, &
	WS_CHILD + WS_VISIBLE, &
	10, &
	10, &
	UnitsToPixels( this.width, XUnitsToPixels! ) - 30, &
	15, &
	Handle( this ), &
	0, &
	Handle( GetApplication() ), &
	0 )

// Setting min and max values
// Max value needs to be bitshifted by 16 here because the higher 16 bits of the 32 bit variable is used here
Send( hProgress, PBM_SETRANGE, 0, 100 * 2^16 )

// Setting the step size, 10 is the default
Send( hProgress, PBM_SETSTEP, 5, 0 )

end event

type cb_increment from commandbutton within w_progressbar
integer x = 402
integer y = 288
integer width = 407
integer height = 100
integer taborder = 10
integer textsize = -10
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "Progress!"
boolean flatstyle = true
end type

event clicked;
// Increment progress bar
Send( hProgress, PBM_STEPIT, 0, 0 )

end event

Comment
  1. Roland Smith
  2. Wednesday, 19 July 2023 13:53 PM UTC
Why do you want to do it via WinAPI and not use the built-in HProgressBar control?
  1. Helpful
  1. Benjamin Gaesslein
  2. Thursday, 20 July 2023 05:30 AM UTC
FWIW, My version runs just fine deployed as 64 bit. All Windows handles only use 32 significant bits to maintain interoperability between 32- and 64-bit applications and this is highly unlikely to change. ( See https://learn.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication )
  1. Helpful
  1. Benjamin Gaesslein
  2. Thursday, 20 July 2023 05:34 AM UTC
Otherwise, there's little to no difference between this and the HProgressBar control. I was messing around with API functions anyway so it was just a quick experiment for me. :)
  1. Helpful
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.