?
Solved

Basic statusbar management in VC++

Posted on 1998-05-23
9
Medium Priority
?
1,051 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:fraggel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 
LVL 11

Accepted Solution

by:
alexo earned 150 total points
ID: 1403606
>> 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
 
LVL 22

Expert Comment

by:nietod
ID: 1403607
Alex, I don't see an answer to the coordinate problem--and I don't have one to offer.  The code looks right.
0
 
LVL 11

Expert Comment

by:alexo
ID: 1403608
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 22

Expert Comment

by:nietod
ID: 1403609
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
 
LVL 11

Expert Comment

by:alexo
ID: 1403610
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
 

Author Comment

by:fraggel
ID: 1403611
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
 
LVL 11

Expert Comment

by:alexo
ID: 1403612
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
 

Author Comment

by:fraggel
ID: 1403613
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
 
LVL 11

Expert Comment

by:alexo
ID: 1403614
Have Fun!
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question