?
Solved

Basic statusbar management in VC++

Posted on 1998-05-23
9
Medium Priority
?
1,060 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
  • 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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
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

Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 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