June 24, 2024, 06:19:38 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Problem with EM_GETSEL in subclassed edit control

Started by LarryMc, October 21, 2011, 09:08:52 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

LarryMc

Ok, I'm stumped!

Here's what I know.
If you have an edit control and you want to find where the cursor is located in the string you use the EM_GETSEL message.
From the info returned in wparam and lparam you can determine if any text is selected or, if the numbers are the same, it indicates where the cursor is located.

I modified a program of Sapero's to demonstrate my problem.
Edit3 control is subclassed.
When I click on it the WM_SETFOCUS message is sent.
In that portion of the handler I can send the WM_GETTEXTLENGTH message and get the correct result.
I can also send WM_GETTEXT and get the correct results.
But if I send EM_GETSEL I get 0,0 no mater where I click in the edit control

However, if I send the EM_GETSEL message when focus is loss or when a character(in this case a number) is typed I get the values I expect.

Although I don't need it, the EM_SETSEL  message doesn't work either in the WM_SETFOCUS portion of the handler but does in the other places.

Can anyone tell me why it isn't working and/or how to make it work?

LarryMc

$include "windowssdk.inc"
openconsole
Const EDIT_1 = 1
Const EDIT_2 = 2
Const EDIT_3 = 3
Const EDIT_4 = 4
Const EDIT_5 = 5
Const EDIT_6 = 6
Const EDIT_7 = 7
Const EDIT_8 = 8
Const EDIT_9 = 9
Const EDIT_10 = 10
Const BUTTON_11 = 11
Const BUTTON_12 = 12
Window d1
OpenWindow d1,0,0,310,320,0,0,"enter tabbing",&d1_handler
Control d1,@Edit,"Edit1",19,14,265,20,0x50810080,EDIT_1
Control d1,@Edit,"Edit2",19,37,265,20,0x50810080,EDIT_2
Control d1,@Edit,"Edit3",19,60,265,20,0x50810080,EDIT_3
SubclassNumEdit(d1,EDIT_3)
Control d1,@Edit,"Edit4",19,83,265,20,0x50810080,EDIT_4
Control d1,@Edit,"Edit5",19,106,265,20,0x50810080,EDIT_5
Control d1,@Edit,"Edit6",19,129,265,20,0x50810080,EDIT_6
Control d1,@Edit,"Edit7",19,152,265,20,0x50810080,EDIT_7
Control d1,@Edit,"Edit8",19,175,265,20,0x50810080,EDIT_8
Control d1,@Edit,"Edit9",19,198,265,20,0x50810080,EDIT_9
Control d1,@Edit,"Edit10",19,221,265,20,0x50810080,EDIT_10
Control d1,@SysButton,"OK",216,252,70,23,0x50010000,BUTTON_11
Control d1,@SysButton,"Cancel",142,252,70,23,0x50010000,BUTTON_12
For x = EDIT_1 To EDIT_10
   SetControlNotify(d1,x,1,1)
Next x
SetFocus d1,EDIT_1
WaitUntil d1 = 0
closeconsole
End
Sub d1_handler
   Select @Message
      Case @IDControl
         If @ControlID >= EDIT_1 AND @ControlID <= EDIT_10
            ' set the focus of the next control
            If @NotifyCode = @ENENTERKEY OR @NotifyCode = @ENTABKEY
               SetFocus d1,@ControlID + 1
            EndIf
            ' set the background color of the edit control
            If @NotifyCode = @ENKillFocus
               SetControlColor d1,@ControlID,0,0xFFFFFF
            EndIf
            If @NotifyCode = @ENSetFocus
               SetControlColor d1,@ControlID,0,0xF0FFE0
            EndIf
         EndIf
      Case @IDCreate
         CenterWindow d1
      Case @IDCloseWindow
         CloseWindow d1
   EndSelect
Return
EndSub
SUB SubclassNumEdit(parent as WINDOW,id as INT)
   
   hEdit = GETCONTROLHANDLE(parent,id)
   lpfn = SetWindowLongA(hEdit,-4,&numEditHandler)
   'save the old handler as a property in the edit control
   'this way we don't need any global variables
   SetPropA(hEdit,"edit_handler",lpfn)   
RETURN
ENDSUB
SUB UnSubclassNumEdit(parent as WINDOW,id as INT)
   'restore the old handler and remove the property
   hEdit = GETCONTROLHANDLE(parent,id)
   SetWindowLongA(hEdit,-4,GetPropA(hEdit,"edit_handler"))
   RemovePropA(hEdit,"edit_handler")
RETURN
ENDSUB
SUB numEditHandler(hwnd:int,uMsg:int,wParam:int,lParam:pointer),int
int st,sp,L
string s
   SELECT uMsg
      CASE @IDCHAR
SendMessage(d1, EM_GETSEL, &st,&sp,EDIT_3)
?st,sp
         IF ((wParam < 0x30) OR (wParam > 0x39)) AND (wParam <> ASC(".")) AND (wParam <> 0x08) THEN RETURN 1
case wm_setfocus
L = SendMessage(d1, WM_GETTEXTLENGTH, 0, 0,EDIT_3)
?L
SendMessage(d1, WM_GETTEXT, L, s,EDIT_3)
?s
SendMessage(d1, EM_GETSEL, &st,&sp,EDIT_3)
?st,sp
case wm_killfocus
SendMessage(d1, EM_GETSEL, &st,&sp,EDIT_3)
?st,sp
   ENDSELECT
RETURN CallWindowProcA(GetPropA(hwnd,"edit_handler"),hwnd,uMsg,wParam,lParam)
ENDSUB
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library