Hello:

I am attempting to make a progress bar with text in the middle to show a percentage. Searching the forum I found clues about floating a transparent static control over the progress bar. This seems to work but the static control does not repaint its own background so the text looks messed up after awhile. To get around this I use ShowWindow to hide the static control, then ShowWindow to make it visible again. Is there a better way? Also the progress bar does not honor the settings in the resource file for PBS_SMOOTH. Is it possible to set the style to PBS_SMOOTH after it is drawn in the dialogbox from a resource?  Does anyone have this file: progressproc.asm from a 2001 post? It shows how to subclass the progress bar and paint text on it directly. Or does anyone have a ready made control or source code for a horizontal progress bar with text inside it?

(Already looked at Gradprogress. It does not have text inside the progress bar, and I cant seem to figure out how to draw text inside it.)

Thanks in advance,
SS

Posted on 2007-04-01 02:32:10 by SideSwipe
Why don't you ... ?


  • Get the handle of the Progress Bar

  • Get the Device Context handle of the Progress Bar with GetDC()

  • Write text to the Device Context using TextOut



I was going to give you an example with a pseudocode but I then coded it in Delphi. I hope that it is okay with moderators because if it is not, I can replace it with an Assembly code. I just did it in Delphi because I didn't have time so, sorry.

Function DrawTextOnProgressBar (ProgressBarHandle : HWND; Const Text : PChar;
                                BackgroundColor, ForegroundColor : Cardinal;
                                XPosition, YPosition : Cardinal) : Boolean;
Var
  WindowClass : Array[0..255] of Char;
  WindowDC    : HDC;
  Background  : Cardinal;
Const
  PROGRESSCLASS = 'msctls_progress32';
begin
Result := False;
if (IsWindow(ProgressBarHandle) = False) or
  (IsWindowVisible(ProgressBarHandle) = False) then
  Exit;
GetClassName(ProgressBarHandle, WindowClass, SizeOf(WindowClass));
if (lstrcmpi(PROGRESSCLASS, WindowClass) <> 0) then
  Exit;
WindowDC := GetDC(ProgressBarHandle);
if (WindowDC = 0) then
  Exit;
SetBkColor(WindowDC, BackgroundColor);
SetTextColor(WindowDC, ForegroundColor);
Result := TextOut(WindowDC, XPosition, YPosition , Text, lstrlen(Text));
ReleaseDC(ProgressBarHandle, WindowDC);
end;



It allows you to put the handle of the Progress Bar on which you want to draw a text, your text, the foreground and the background colors of the text and finally, the X and Y location of the text. Hope it helps.
Posted on 2007-04-01 04:51:32 by XCHG
Hello XCHG:

Thanks for the reply. Outputting text directly to the progress bar control saved me from using a static control overlayed on top of the progressbar. However, I am back to the problem with the progress bar looking messed up as the text changes. I tried to use ShowWindow to hide and display the progress bar, but the text still looks messed. How do I get the progress bar to redraw itself?  Or, how do I get the overlayed text to not look all roostered?

This is what I have so far. Any help appreciated!

invoke SendMessage,h_progress1, PBM_SETPOS, 60, 0 ;put some bars in the progress bar
invoke GetDC,h_progress1
mov h_progress1_DC,eax
invoke SetBkMode,h_progress1_DC,TRANSPARENT
invoke TextOut,h_progress1_DC,10,1,ADDR random_number_text,6 ;;;;ie: 18.48, 17.55 etc...
invoke ReleaseDC,h_progress1,h_progress1_DC

Also, is it possible to set the style to PBS_SMOOTH after it is drawn in the dialogbox from a resource? How do I do this?

Regards,
SS
Posted on 2007-04-01 10:53:41 by SideSwipe
Hello Again:

OK, so I have answered one of my own questions. To change the style of a window or control use SetWindowLong. I use SetWindowLong to change the style of my progress bar to PBS_SMOOTH like this:

invoke SetWindowLong,h_progress1,GWL_STYLE, WS_VISIBLE or WS_CHILD or PBS_SMOOTH

Problem is, it did not work, the progress bar still shows segmented. I figured out the problem was I am using a XML stylesheet in the .RC file like this:

1    24      DISCARDABLE    "xptheme.xml"

This gives my app the nice XP style controls. When I remove the above line from the RC file then the Progress Bar Smooth style works. Also when the above line is removed from the RC file then the resource editor properties work again and I don't need SetWindowLong at all.

What am I doing wrong? How can I get the nice XP style effect and still be able to set the progress bar to PBS_SMOOTH ? Below is the XML file I found on this board.



<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly
  xmlns="urn:schemas-microsoft-com:asm.v1"
  manifestVersion="1.0">
<assemblyIdentity
    processorArchitecture="x86"
    version="5.1.0.0"
    type="win32"
    name="MyApp"/>
<description>Program Description</description>
<dependency>
  <dependentAssembly>
    <assemblyIdentity
        type="win32"
        name="Microsoft.Windows.Common-Controls"
        version="6.0.0.0"
        publicKeyToken="6595b64144ccf1df"
        language="*"
        processorArchitecture="x86"/>
  </dependentAssembly>
</dependency>
</assembly>

Posted on 2007-04-01 12:38:29 by SideSwipe