1

Is there any way to programmatically scroll a single-line edit control in Windows?

For example, if the text in an edit control is too large to display at once, then the default behavior when the edit control gets the focus is to select all text and show the end of the text. I'd like to instead show the beginning of the text (while still leaving all text selected).

Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
  • That's not the default bahaviour. The control (or it's sub class) must be explicitly selecting all the text on focus. – Deanna Jul 30 '12 at 09:12
  • @Deanna - Are you sure about that? I remember seeing the select-all-on-focus behavior since the Windows 95 days; I'm certain that it is the default behavior. – Josh Kelley Jul 30 '12 at 13:17
  • Most of the Windows Shell seem to do it, but I've not seen it on "native" win32 apps, and MSDN doesn't say it does it on `WM_GOTFOCUS`, just shows the existing selection. I may be completely wrong though :) (I can't compile any C++ stuff atm :( – Deanna Jul 30 '12 at 13:32

2 Answers2

1

Although there's (apparently) no API for scrolling to the beginning and selecting all text, it seems to work to simulate the keystrokes that would do the same:

#ifndef CTRL               
#define CTRL(x) (x&037)    
#endif

SendMessage(edit_handle, WM_KEYDOWN, VK_HOME, 0);
SendMessage(edit_handle, WM_CHAR, CTRL('A'), 0);
Josh Kelley
  • 56,064
  • 19
  • 146
  • 246
-1

You can either call SetScrollPos or send the WM_VSCROLL/WM_HSCROLL message directly to the window. You can find the full list of scroll functions here.

Mike Kwan
  • 24,123
  • 12
  • 63
  • 96
  • I've tried this, and I can't get it to work for a stock single-line edit control. SetScrollPos fails with ERROR_NO_SCROLLBARS. – Josh Kelley Jul 27 '12 at 16:47
  • Ah, missed the part about single-line edit control. That control would not have a scrollbar at all then. – Mike Kwan Jul 27 '12 at 16:51
  • Have you tried `Edit_SetSel`/`EM_SETSEL` with start value as the end (length) and end value as 0? – Mike Kwan Jul 27 '12 at 16:57
  • As far as I can tell, `Edit_SetSel(h, length, 0)` doesn't work either. (In fact, it seems to act the same as `Edit_SetSel(h, 0, length)`, which is strange, since that's not how MSDN describes it.) – Josh Kelley Jul 27 '12 at 17:12
  • 3
    "Edit controls: The control displays a flashing caret at the end position regardless of the relative values of start and end." - This explains why it doesn't work. – Mike Kwan Jul 27 '12 at 17:42