title: Win32 Edit Control Caret Placement Offset
body: <p>I gave an English explanation of my problem below but it is a visual issue so if you don't want to read it all just look at the picture at the bottom).</p>
  
  <p>I'm working on building a reverse polish notation calculator for my class and I just completed having the button controls on my GUI be able to append their values to the edit control which works fine, but the caret is doing something weird and I can't find any information on it. </p>
  
  <p>I send a custom message to the edit control in which it finds the length of the current text in the control and then places the caret at the end of the text so I can then add what text needs to be added (it is right aligned with <code>ES_RIGHT</code>), which again works just fine, but when the caret is in the right most place it can be, it is placed practically right through the middle of most any number. </p>
  
  <p>This only seems to happen in the right most place the caret can be (i.e. anywhere else the caret sits directly to the right of the preceding char, as it should) and I have tried replacing the caret all the way to the right using code, placing it using my keyboard/mouse, and tried adjusting the dimensions of the window in hopes that it was just an offset of the width I defined for it that caused the last place to be off slightly, but the problem persists and it makes it hard to read the last char in the text field.</p>
  
  <p>Relevant Code:</p>
  
  <pre><code>LRESULT CALLBACK EditBoxClass::WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  {
      switch( msg )
      {
      case WM_COMMAND:
          break;
      case WM_APPEND_EDIT:
          /* Get current length of text in the box */
          index = new int( GetWindowTextLength (hWnd) );
          SetFocus( hWnd );
          /* Set the caret to the end of the text in the box */
          SendMessage( hWnd, EM_SETSEL, (WPARAM)index, (LPARAM)index );
          /* "Replace" the selection (the selection is actually targeting 
              nothing and just sits at the end of the text in the box) 
              with the passed in TCHAR* from the button control that 
              sent the WM_APPEND_EDIT message */
          SendMessage( hWnd, EM_REPLACESEL, 0, lParam );
          break;
      }
      return CallWindowProc( EditClassStruct.GetOldProc(), hWnd, msg, wParam, lParam );
  }
  </code></pre>
  
  <p>Picture of problem:</p>
  
  <p><img src="https://i.stack.imgur.com/qljd1.png" alt="image"></p>