The Caret

This is a discussion on The Caret within the Windows Programming forums, part of the Platform Specific Boards category; Hello guys is there another way to move a window caret to the end of an edit window? I tried ...

  1. #1
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82

    The Caret

    Hello guys is there another way to move a window caret to the end of an edit window?

    I tried this...

    Code:
    void movecaret(HWND hwnd)
    {
      int textlen = GetWindowTextLength(hwnd);
    
      SetFocus(hwnd);
      SetCaretPos(textlen, textlen);
    }
    ...but it won't work

  2. #2
    Visionary Philosopher Sayeh's Avatar
    Join Date
    Aug 2002
    Posts
    212
    I want to mention 2 things. I'll talk about SetCaretPos() in a moment, but first I want to impress upon you the importance of keeping track of state. Most all of you fail to do this.

    Whenever you change the focus from one window to the next, or from anything to the next, you should save the existing state of focus before you change something, and then restore it when you are done.

    This is called being "well behaved". If you don't, your app either works funny sometimes, or someone elses app behaves poorly. Apps must get along as good neighbors.

    With that in mind, Instead of setting focus to hwnd, you should do a GetFocus() and keep that in another variable, SetFocus() to your existing window, do your work, and then SetFocus() back to the original window.

    Same thing with your Caret Focus.

    It's overhead, but it's important in a GUI.

    ----

    Now about your caret-- how do you know it's not positioning it correctly? Maybe it's just not visible? Or perhaps you aren't even using it correctly. Have you ever looked at the definition for SetCaretPos() ? It's prototype is thus:

    BOOL SetCaretPos(int x,int y);

    You're not passing the right values. You also didn't look at the return result from the call. SetCaretPos() returns a zero if it failed. In which case you can call GetLastErr() for extended error information. Any other return value means success.

    What did SetCarePos() return?

    Did you use HideCaret() and ShowCaret() correctly/at-all?

    Good Luck.
    It is not the spoon that bends, it is you who bends around the spoon.

  3. #3
    Rez
    Rez is offline
    Registered User
    Join Date
    Nov 2002
    Posts
    82
    Thanx a lot! I noticed that behavior about my app.

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    Ummm.......typo

    GetLastErr()

    is actually

    GetLastError()
    "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. Rich Edit Control - Get And Set Caret Position?
    By Charles01 in forum Windows Programming
    Replies: 1
    Last Post: 12-05-2005, 10:26 AM
  2. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 01:59 PM
  3. How to show Blinking caret?
    By loko in forum C Programming
    Replies: 2
    Last Post: 07-13-2005, 03:53 AM
  4. weird errors
    By R3N3G4D3 in forum Windows Programming
    Replies: 0
    Last Post: 06-14-2005, 07:54 AM
  5. gotoxy(x, y); function
    By bluehead in forum Windows Programming
    Replies: 8
    Last Post: 01-13-2002, 01:08 PM

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