Basic statusbar management in VC++

I found the following code somwhere at Microsofts site:
switch (message)
{
   case WM_CREATE:
      hWndStatus = CreateWindowEx(
            0L,                              // no extended styles
            STATUSCLASSNAME,                 // status bar
            "",                              // no text
            WS_CHILD | WS_BORDER | WS_VISIBLE,  // styles
            -100, -100, 10, 10,              // x, y, cx, cy
            hWnd,                            // parent window
            (HMENU)100,                      // window ID
            hInst,                           // instance
            NULL);                           // window data
      if (hWndStatus == NULL)
         MessageBox (NULL, "Status Bar not created!", NULL, MB_OK );
      break;

   case WM_MOUSEMOVE:
      wsprintf(szBuf, "Mouse position: %d, %d", LOWORD(lParam), HIWORD(lParam));
      SendMessage(hWndStatus, SB_SETTEXT, 0, (LPARAM)(LPSTR)szBuf);
      break;
   .
   .
   .
}

But I can't get it to work. I declared szBuf as
char szBuf[30];
The only thing that appears in the statusbar is
"Mouse position: 64436, 0" and it never changes.
What's wrong?
How do I resize the statusbar when the window is resized?
I've tried this:
case WM_SIZE:
   SendMessage( hWndStatus, WM_SIZE, wParam, lParam);
break;
but with no success.
TIA
/Niklas
fraggelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

alexoCommented:
>> hWndStatus = CreateWindowEx(...)
Try using CreateStatusWindow() instead.
Also make it a "simple" status bar by:
SendMessage(hWndStatus, SB_SIMPLE, TRUE, 0L);

>> SendMessage(hWndStatus, SB_SETTEXT, 0, (LPARAM)(LPSTR)szBuf);
Since you didn't define any parts, you should use:
SendMessage(hWndStatus, SB_SETTEXT, (WPARAM)255, (LPARAM)(LPSTR)szBuf);

The status bar (in "simple mode") will display the new text when the window next receives the WM_PAINT message, so you can SendMessage() WM_PAINT right after changing the text.

Otherwise, you will have to define parts:

The following example creates a status window that has a sizing grip and divides the window into four equal parts based on the width of the parent window's client area.

// DoCreateStatusWindow - creates a status window and divides it into  the specified number of parts.
// Returns the handle to the status window.
// hwndParent - parent window for the status window
// nStatusID - child window identifier
// hinst - handle to the application instance
// nParts - number of parts into which to divide the status window
HWND DoCreateStatusWindow(HWND hwndParent, int nStatusID, HINSTANCE hinst, int nParts)
{
    HWND hwndStatus;
    RECT rcClient;
    HLOCAL hloc;
    LPINT lpParts;
    int i, nWidth;
 
    // Ensure that the common control DLL is loaded.
    InitCommonControls();
 
    // Create the status window.
    hwndStatus = CreateWindowEx(
        0,                       // no extended styles
        STATUSCLASSNAME,         // name of status window class
        (LPCTSTR) NULL,          // no text when first created
        SBARS_SIZEGRIP |         // includes a sizing grip
        WS_CHILD,                // creates a child window
        0, 0, 0, 0,              // ignores size and position
        hwndParent,              // handle to parent window
        (HMENU) nStatusID,       // child window identifier
        hinst,                   // handle to application instance
        NULL);                   // no window creation data
 
    // Get the coordinates of the parent window's client area.
    GetClientRect(hwndParent, &rcClient);
 
    // Allocate an array for holding the right edge coordinates.
    hloc = LocalAlloc(LHND, sizeof(int) * nParts);
    lpParts = LocalLock(hloc);
 
    // Calculate the right edge coordinate for each part, and
    // copy the coordinates to the array.
    nWidth = rcClient.right / nParts;
    for (i = 0; i < nParts; i++) {
        lpParts[i] = nWidth;
        nWidth += nWidth;
    }
 
    // Tell the status window to create the window parts.
    SendMessage(hwndStatus, SB_SETPARTS, (WPARAM) nParts, (LPARAM) lpParts);
 
    // Free the array, and return.
    LocalUnlock(hloc);
    LocalFree(hloc);
    return hwndStatus;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Alex, I don't see an answer to the coordinate problem--and I don't have one to offer.  The code looks right.
0
alexoCommented:
I'll elaborate:

SendMessage(hWndStatus, SB_SETTEXT, 0, (LPARAM)(LPSTR)szBuf) sets the text of the first part of the statusbar.  However, no parts were defined.  The status bar needs to be either declared as "simple" (i.e., no parts) and use part 255, or at least one part needs to be set up.


0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

nietodCommented:
Wrong coordinate problem.  Or I'm totallly lost.  His mouse message seems to be getting the wrong coordinates,  He said

The only thing that appears in the statusbar is
"Mouse position: 64436, 0" and it never changes.
What's wrong?

But the code looks right.
0
alexoCommented:
Hmmm...

The problem could be in several places:
* The statusbar code (thet's what I thought).
* In the mouse coordinate calculation.
* In the display.

Best thing to do is to use Spy on the statusbar and see if the message gets there.

A breakpoint on the line that sets the statusbar text can also help.  It's possible to see the actual mouse coordinates and text buffer.

Also:

Send WM_PAINT to repaint the statusbar.

Check that the mouse is not captured by another application.

0
fraggelAuthor Commented:
I found what's missing in my code.
I didn't declare the window handle static, I've added
static HWND hWndStatus;
and it works!
Wasn't easy for you to know about that. The code was right all the time, but not my declaration...
Thanks anyway. Now I can create an even more advanced statusbar for my applications.
/Niklas
0
alexoCommented:
So I got the points for nothing?  Duh!

Niklas, it is possible to ask the customer support (i.e., Linda) to remove a question that wasn't satisfactory answered and refund the points.
0
fraggelAuthor Commented:
I wouldn't say nothing. You told me how to create parts in the status bar. It probably have been my next question, and now I  got answer in advance :-)
Keep the points, I.ll get new ones.
/Niklas
0
alexoCommented:
Have Fun!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.