Thread: Edit box with automatic scrollbars?

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

    Lightbulb Edit box with automatic scrollbars?

    Hi,
    I'm trying to modify an edit box so that when the text in it exceeds its visible size, it automatically gains scrollbars. I've tried doing it like so:-
    Code:
    (in my main window's WindowProc...)
    (g_hwndText = edit box)
    case EN_CHANGE:
    {
       int iNumLines;
       HDC hDC;
       RECT rc;
       TEXTMETRIC tm;
       DWORD dwStyle;
    
       GetClientRect(g_hwndText, &rc);
       iNumLines = SendMessage(g_hwndText, EM_GETLINECOUNT, 0, 0);
       hDC = GetDC(g_hwndText);
       GetTextMetrics(hDC, &tm);
       ReleaseDC(g_hwndText, hDC);
       if (rc.bottom - (iNumLines * tm.tmHeight) < 0)
       {
          dwStyle = GetWindowLong(g_hwndText, GWL_STYLE);
          SetWindowLong(g_hwndText, GWL_STYLE, dwStyle | WS_VSCROLL);
       }
    		
       break;
    }
    But it doesn't seem to work. Any ideas?

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Check that the HWND is valid and that the size returned in the tm struc is valid. (and that it enters the scroll loop)

    another way is
    Code:
    GetDlgItemText(blah,blah...)
    GetTextExtentPoint32(hdc, sText, lstrlen(sText), &Size);
    and use Size.cy for the text height.

    Could also try a listbox instead of the edit and use the msg / command 'ensure visible'.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

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

    Cool

    Woohoo! I got it working!!!

    I had two problems:-

    1. EN_CHANGE isn't an actual window message (as I had thought), it comes through WM_COMMAND. D'oh.

    2. I have to use SetWindowPos(g_hwndText, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED) after SetWindowLong for the window to be updated and the scrollbar added.

    It does seem to add the scrollbars a bit early for some reason, but it's looking good

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    I thought it was strange you had a switch just for the edit control.

    Try UpdateWindow() instead of the SetWindowPos() (may be easier)
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 02-13-2008, 02:59 PM
  2. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 04:40 AM
  3. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 08:46 AM
  4. setting fixed floats in edit box
    By WaterNut in forum Windows Programming
    Replies: 4
    Last Post: 08-13-2004, 09:13 AM
  5. Edit box question
    By learning110 in forum Windows Programming
    Replies: 6
    Last Post: 03-28-2003, 08:16 PM