Solved

Basic statusbar management in VC++

Posted on 1998-05-23
9
1,017 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 50 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now