Link to home
Start Free TrialLog in
Avatar of fraggel
fraggel

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of alexo
alexo
Flag of Antarctica image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

Alex, I don't see an answer to the coordinate problem--and I don't have one to offer.  The code looks right.
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.


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.
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.

Avatar of fraggel

ASKER

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
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.
Avatar of fraggel

ASKER

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
Have Fun!