Thread: MessageBox-a-like

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,273

    MessageBox-a-like

    Hello,

    I'm trying to create a function that generates a dialog containing a prompt and an edit box that, much like MessageBox, is sized proportionally to the prompt.

    I've been using GetTextExtentPoint32, but that always gives me a width that's nearly twice the size of the actual prompt. Of course, it's being used at the point of creation of the window, so I'm having to get the desktop's DC and do it there. The desktop DC for some reason doesn't seem to have the standard dialog font as its default font, so I'm assuming it's getting this massive value through its use of the standard System font.

    I suppose it could be done in WM_INITDIALOG, but that would be more complicated as I would have to retrieve the caption text, resize all of the controls, etc.

    Is there a better way?

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    23
    Well why not. At least Wine code is doing it.

    Code:
        /* Get the text size */
        GetClientRect(GetDlgItem(hwnd, MSGBOX_IDTEXT), &rect);
        rect.top = rect.left = rect.bottom = 0;
        DrawTextA( hdc, lpszText, -1, &rect,
    	       DT_LEFT | DT_EXPANDTABS | DT_WORDBREAK | DT_CALCRECT);
        /* Min text width corresponds to space for the buttons */
        tleft = 2 * ileft + iwidth;
        twidth = max((bw + bspace) * buttons + bspace - tleft, rect.right);
        theight = rect.bottom;
    And after that they are calling SetWindowPos() for the buttons.
    Code:
        /* Position the buttons */
        bpos = (wwidth - (bw + bspace) * buttons + bspace) / 2;
        for (buttons = i = 0; i < 7; i++) {
           /* some arithmetic to get the right order for YesNoCancel windows */
           hItem = GetDlgItem(hwnd, (i + 5) % 7 + 1);
           if (GetWindowLongA(hItem, GWL_STYLE) & WS_VISIBLE) {
              if (buttons++ == ((lpmb->dwStyle & MB_DEFMASK) >> 8)) {
                 SetFocus(hItem);
                 SendMessageA( hItem, BM_SETSTYLE, BS_DEFPUSHBUTTON, TRUE );
              }
              SetWindowPos(hItem, 0, bpos, tiheight, bw, bh,
                    SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREDRAW);
              bpos += bw + bspace;
           }
       }
    In this code you can see some Wine internals, but you should be
    able to change it to your own data structs.

    And all of the above is done at WM_INITDIALOG.

    Delf
    Last edited by Delf; 10-01-2003 at 04:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete files that are used
    By Yuri in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2005, 01:48 PM
  2. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2005, 11:18 PM
  3. Forcing MessageBox to be non-concurrent?
    By JasonD in forum Windows Programming
    Replies: 17
    Last Post: 12-10-2003, 03:16 PM
  4. EnterCriticalSection and MessageBox
    By novacain in forum Windows Programming
    Replies: 13
    Last Post: 01-30-2003, 08:48 AM
  5. double in a MessageBox
    By Robert602 in forum Windows Programming
    Replies: 3
    Last Post: 12-17-2001, 03:10 PM