Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
The simple reason you can't put a Label on top of a ProgressBar is because a Label is windowless, which means that VB draws the text directly on the Form. A windowless control can never be above a windowed control (unless the control is a container like the PictureBox or Frame of course).
Mark showed you how you can replace the ProgressBar with a PictureBox but if you still want to use the ProgressBar the following code will draw text on it.
VB Code:
Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hWnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByVal hDC As Long) As Long
Private Declare Function DrawText _
Lib "user32.dll" Alias "DrawTextA" ( _
ByVal hDC As Long, _
ByVal lpStr As String, _
ByVal nCount As Long, _
ByRef lpRect As RECT, _
ByVal wFormat As Long) As Long
Private Declare Function SetBkMode Lib "gdi32.dll" ( _
ByVal hDC As Long, _
ByVal nBkMode As Long) As Long
Private Declare Function GetClientRect _
Lib "user32.dll" ( _
ByVal hWnd As Long, _
ByRef lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Const TRANSPARENT As Long = 1
Private Const DT_CENTER As Long = &H1
Public Sub TextOnProgress(pb As ProgressBar, ByVal sText As String)
Dim hDC As Long
Dim r As RECT
hDC = GetDC(pb.hWnd)
Call SetBkMode(hDC, TRANSPARENT)
Call GetClientRect(pb.hWnd, r)
Call DrawText(hDC, sText, -1&, r, DT_CENTER)
Call ReleaseDC(pb.hWnd, hDC)
End Sub
Simply call the TextOnProgress this way:
VB Code:
Call TextOnProgress(ProgressBar1, "The text to show")
1) If your post has been adequately answered please click in your post on "Mark Thread Resolved".
2) If someone has been useful to you please show your respect by rating their posts.
3) Please use [highlight="VB"] 'your code goes in here [/highlight] tags when posting code.
4) Before posting your question, make sure you checked this links: MICROSOFT MSDN -- VB FORUMS SEARCH
5)Support Classic VB - A PETITION TO MICROSOFT
This thread is 12 years old. Nobody who participated has been here for a few years. Therefore, it would be better if you started a new thread with the question, as you aren't likely to get any useful responses in this thread.
I don't think you have created the fresh thread yet ?
In the meantime, this may help if you (Andre) are looking for text 'over' (appearing to be on top) of the progress bar - http://www.planet-source-code.com/vb...25863&lngWId=1
The author has been around the block for a long time.
Public Sub TextOnProgress(pb As ProgressBar, ByVal sText As String)
Dim hDC As Long
Dim r As RECT
hDC = GetDC(pb.hwnd)
pb.Refresh
SetBkMode hDC, TRANSPARENT
GetClientRect pb.hwnd, r
SetTextColor hDC, &HFFFFFF
DrawText hDC, sText, -1&, r, DT_CENTER
ReleaseDC pb.hwnd, hDC
End Sub
Thanks for your help
Have a good day
Last edited by AndreBernard; Sep 26th, 2017 at 03:15 AM.
Public Sub TextOnProgress(pb As ProgressBar, ByVal sText As String)
Dim hDC As Long
Dim r As RECT
hDC = GetDC(pb.hwnd)
pb.Refresh
SetBkMode hDC, TRANSPARENT
GetClientRect pb.hwnd, r
SetTextColor hDC, &HFFFFFF
DrawText hDC, sText, -1&, r, DT_CENTER
ReleaseDC pb.hwnd, hDC
End Sub
FYI: Typically, you should not retrieve the hDC before you refresh. Refreshing could potentially result in a change of hDC depending on the actions performed by the control during its Refresh method.
Insomnia is just a byproduct of, "It can't be done"
Just a quick reminder that while you can do this, you probably shouldn't do this. Microsoft explicitly discourages text on top of progress bars because it can be very difficult to read, especially on systems with non-standard themes. From that link:
- Do not embed text, such as a percentage, in the progress bar itself. This is not accessible.
- If the progress bar is contained in the status bar, put any corresponding message text to its left in the status bar.