Edit box with automatic scrollbars?

This is a discussion on Edit box with automatic scrollbars? within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm trying to modify an edit box so that when the text in it exceeds its visible size, it ...

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

    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,856
    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,263

    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,856
    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, 01:59 PM
  2. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 03:40 AM
  3. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 07: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, 07:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21