Thread: progress bar newbie

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    Unhappy progress bar newbie

    I'm trying to create a progress bar for the first time in a dialog box. I've been reading msdn examples, but to be honest I am so new to windows programing, I can't follow it. The problem is, I get the idea but I don't know where to add the code.

    I want to add the progress bar to a dialog box other than the main dlg and I am not understanding how to do that. Do I add it to the cpp file for that dlg, the .h file ????

    Basically when the user clicks ok, and I run my code, I want to update the progress bar that I added to the dlg.

    example:
    Code:
    void PALLGRPPERF::OnOK() 
    //call all my files and crunch numbers
    //fill progress bar while doing this }
    I used the dlg editors to add the progress bar, but can anyone tell me how to add functionality to progress bar when I execute my code from clicking OK button?

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    Try the SendMessage API to set the position of the progress bar:
    SendMessage( hWndControl, PBM_SETPOS,newPosition,0);

    Look here for more info:
    http://msdn.microsoft.com/library/de...pbm_setpos.asp

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    164
    thanks for replying

    do I place the SendMessage command in the same block of code as the
    void PALLGRPPERF::OnOK()

    and do I create the progress bar and then use the idc control address in place of PBM_SETPOS from
    SendMessage( hWndControl, PBM_SETPOS,newPosition,0);

    I assume I use the commctrl.h header, but I get error messages when using the send message you posted, I think I am using it in the wrong location.

    sorry for the stupid questions, like I said I am total newbie and trying to grasp the concepts.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    If you want to use the idc address thingy try this, replacing newPosition with the position you want the progress bar at:
    Code:
    SendMessage(GetDlgItem(hwnd,IDC_WHATEVER), PBM_SETPOS,newPosition,0);
    And did you remember to call InitCommonControlsEx like this:
    Code:
    INITCOMMONCONTROLSEX icex;
    icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    icex.dwICC  = ICC_PROGRESS_CLASS;
    InitCommonControlsEx(&icex);
    Also, don't forget to add the comctl32.lib file to your project.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    164
    Sorry for the late response, but thanks for yours, I will give it a try.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    164
    Ok, Inserted the code you posted and changed it to meet my variables but I am getting some errors :
    error C2065: 'hwnd' : undeclared identifier
    error C2065: 'newPosition' : undeclared identifier

    using the following code:
    Code:
    INITCOMMONCONTROLSEX icex;
    	icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    	icex.dwICC  = ICC_PROGRESS_CLASS;
    	InitCommonControlsEx(&icex);
    	SendMessage(GetDlgItem(hwnd,IDC_PROGRESS1), PBM_SETPOS,newPosition,0);
    I'm sure I butchered this pretty bad, I included windows.h hoping that would take care of the firt error message about hwnd not being declared, but it did not make a difference.

    Any thoughts? Can someone tell me what tell me the purpose of this statement:
    Code:
    SendMessage(GetDlgItem(hwnd,IDC_PROGRESS1), PBM_SETPOS,newPosition,0);

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    That statement changes the progress bar. I thought that was what you wanted to do, but maybe I misunderstood. NewPosition was just a dummy variable, you have to change it to whatever you want the progress bar to be set at. You also have to change hwnd to the handle of the parent of the progress bar.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    164
    you were right, thats what I wanted to accomplish, I just didn't understand your code and how to use it... but the last statement makes alot of sense based off what your coded, I will work that one out... Thanks

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    164
    Ok, I worked out the detail of the position variable, which makes sense, as I incrament the variable I will call the SendMessage statement each time to display the location to display!
    very cool...

    However, I have not been able to figure out how to find the "hwnd" variable from the parent window the progress bar resides in. Can you point me in the direction of where I need to go to find that?

    I keep getting the following errors:
    error C2275: 'CWnd' : illegal use of this type as an expression
    error C2660: 'SendMessageA' : function does not take 4 parameters

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    If you look at your window procedure:
    Code:
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	/* Blah Blah Blah */
    	
    	case WM_THE-MESSAGE-WHEN-YOU-WANT-TO-CHANGE-THE-PROGRESS-BAR:
    		SendMessage(GetDlgItem(hWnd,IDC_PROGRESS1),PBM_SETPOS,newPosition,0);
    	
    	/* Blah Blah Blah */
    }
    The "hwnd" that you want is the one that is the first parameter of your procedure function.

  11. #11
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    hunterdude, it's obvious that WaterNut is using MFC. Anyway I'm familiar with MFC just enough to stay away from that crap, but I do believe that he must use member CWnd::m_hWnd

  12. #12
    Registered User
    Join Date
    Aug 2004
    Posts
    17
    hunterdude, it's obvious that WaterNut is using MFC. Anyway I'm familiar with MFC just enough to stay away from that crap, but I do believe that he must use member CWnd::m_hWnd
    Oh, that would explain it. I know absolutely nothing about MFC so maybe that's why I misunderstood.

    Anyway, here's a simple example without MFC:
    Code:
    #include <windows.h>
    #include <commctrl.h> //the common controls header file
    #pragma comment(lib,"comctl32.lib") //here's that comctl32.lib file
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrev,LPSTR lpCmdLine,int nCmdShow)
    {
    	WNDCLASSEX wc;
    	wc.lpfnWndProc = WndProc;
    	wc.hInstance = hInstance;
    	wc.lpszClassName = "da class";
    	wc.cbSize = sizeof (WNDCLASSEX);
    	wc.hCursor = ::LoadCursor (0, IDC_ARROW);
    	wc.hbrBackground = reinterpret_cast<HBRUSH> (COLOR_BTNFACE + 1);
    	wc.style = 0;
    	wc.cbClsExtra = 0;
    	wc.cbWndExtra = 0;
    	wc.hIcon = 0;
    	wc.hIconSm = 0;
    	wc.lpszMenuName = 0;
    	::RegisterClassEx(&wc);
    	
    	HWND hWnd = ::CreateWindowEx(0,"da class","A window",WS_OVERLAPPED,
    		200,200,200,200,NULL,NULL,hInstance,NULL);
    
    	::ShowWindow(hWnd,nCmdShow);
    	
    	MSG msg;
    	while (GetMessage(&msg, NULL, 0, 0)) 
    	{
    		TranslateMessage(&msg);
    		DispatchMessage(&msg);
    	}
    	return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	switch (message) 
    	{
    	case WM_CREATE:
    		{
    		INITCOMMONCONTROLSEX icex;
    		icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
    		icex.dwICC  = ICC_PROGRESS_CLASS;
    		InitCommonControlsEx(&icex);
    		//123 is the id (just like IDC_PROGRESS1)
    		::CreateWindowEx(0,PROGRESS_CLASS,"Progress bar",WS_CHILD|WS_VISIBLE,25,50,150,50,
    			hWnd,reinterpret_cast<HMENU>(123),(HINSTANCE)::GetWindowLong(hWnd,GWL_HINSTANCE),NULL);
    		}
    		break;
    	case WM_LBUTTONDOWN:
    		{
    		//you clicked the left button...change the progress to 25
    		SendMessage(GetDlgItem(hWnd,123),PBM_SETPOS,25,0);
    		}
    		break;	
    	case WM_RBUTTONDOWN:
    		{
    		//you clicked the right button...change the progress to 75
    		SendMessage(GetDlgItem(hWnd,123),PBM_SETPOS,75,0);
    		}
    		break;	
    	case WM_DESTROY:
    		//bye bye
    		::PostQuitMessage(0);
    		return 0;
    	}
    	return DefWindowProc(hWnd, message, wParam, lParam);
    }

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Basically when the user clicks ok, and I run my code, I want to update the progress bar that I added to the dlg.<<

    Use the CProgressCtrl::StepIt member function.

    If you're using mfc then use the member functions available for the control class of interest, in your case CProgressCtrl.

    If you have to use api functions then make sure you use the scope resolution ( :: ) operator to let the compiler know you're after a global (api) rather than a class local (mfc) function. For example, ::SendMessage is different from CWnd::SendMessage

    Here's (MFC Controls: The Progress Bar) a non-msdn how-to for progressbars that may be of some interest; a search with google should turn up others.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    Registered User
    Join Date
    May 2004
    Posts
    164
    Sorry late on my reply....
    Your correct updating is what I wanted to do. I see what your saying about the newPosition variable, and the hwnd...

    How can I go about determining the handle of the parent window I am using for the progress bar?

    Also I get compiler errors :
    Code:
    error C2660: 'SendMessageA' : function does not take 4 parameters

  15. #15
    Registered User
    Join Date
    May 2004
    Posts
    164
    Hey everyone, thanks for all the replies, I apologize, I was not getting the updates, until I went to hybrid mode for this thread, I see all the other replies now, please ignore my last post, I am working with what was posted earlier now.

    THanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Progress Bar
    By Stabbsy in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2007, 10:30 PM
  2. Updating a progress bar
    By NewGuy100 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2006, 01:00 PM
  3. beach bar (sims type game)
    By DrKillPatient in forum Game Programming
    Replies: 1
    Last Post: 03-06-2006, 01:32 PM
  4. Is comctl32.lib required for progress bar controls?
    By JasonD in forum Windows Programming
    Replies: 4
    Last Post: 08-28-2004, 02:30 PM
  5. File Through Winsock & Progress on bar
    By (TNT) in forum Windows Programming
    Replies: 4
    Last Post: 12-05-2001, 10:50 PM