-
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 :confused:
-
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.
-
Thanx a lot! :D I noticed that behavior about my app.
-
Ummm.......typo
GetLastErr()
is actually
GetLastError()