Posts: 1,031
Threads: 246
Joined: Jul 2022
Hi,
The following code,Use Hotkey Alt+A, can append text to the current line in the edit box, but there will be errors when operating the second line. Please see the following picture
Macro Macro6
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "Dialog" "4"
;3 Edit 0x54231044 0x200 8 8 208 102 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""
str controls = "3"
str e3
e3=
;AAAAAAAAAAAAAAAAAAAAAAAAAA
;BBBBBBBBBBBBBBBBBBBBBBBBBB
;CCCCCCCCCCCCCCCCCCCCCCCCCC
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
#sub DlgProc
function# hDlg message wParam lParam
sel message
,case WM_INITDIALOG
,DT_SetAccelerators hDlg "401 Aa"
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
,case 401
,sub.SetCurLine id(3 hDlg) "_____"
ret 1
#sub SetCurLine
function h str's
POINT p; xm(p)
int char = SendMessage(h EM_CHARFROMPOS p.x p.y)
int line = SendMessage(h EM_LINEFROMCHAR char 0)
int lineStart = SendMessage(h EM_LINEINDEX line 0)
int lineLength = SendMessage(h EM_LINELENGTH line 0)
int lineEnd = lineStart + lineLength
SendMessage(h EM_SETSEL lineStart lineEnd)
_s.getsel
_s+F"{s}"
SendMessageW h EM_REPLACESEL 0 @_s
Posts: 1,031
Threads: 246
Joined: Jul 2022
If line 2 is operated first, the result is correct, why?
Posts: 1,336
Threads: 61
Joined: Jul 2006
you had several mistakes.
use this instead.
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "Dialog" "4"
;3 Edit 0x54231044 0x200 8 8 208 102 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""
str controls = "3"
str e3
e3="AAAAAAAAAAAAAAAAAAAAAAAAAA[]BBBBBBBBBBBBBBBBBBBBBBBBBB[]CCCCCCCCCCCCCCCCCCCCCCCCCC"
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
#sub DlgProc
function# hDlg message wParam lParam
sel message
,case WM_INITDIALOG
,DT_SetAccelerators hDlg "401 Aa"
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
,case 401
,int cid=id(3 hDlg)
,sub.SetCurLine(cid "_____")
ret 1
#sub SetCurLine
function h str's
POINT p; xm(p h 1) ;;get mouse position into p.x and p.y
int lh=MakeInt(p.x p.y)
int cursorPos = SendMessage(h EM_CHARFROMPOS 0 lh)
int cpos line
cpos = cursorPos&0xFFFF
line = cursorPos>>16
int lineStart = SendMessage(h EM_LINEINDEX line 0)
SendMessage(h EM_SETSEL lineStart cpos)
_s.getsel
_s+ s
SendMessageW h EM_REPLACESEL 1 @_s
Posts: 1,031
Threads: 246
Joined: Jul 2022
02-27-2023, 02:48 AM
(This post was last modified: 02-27-2023, 02:51 AM by Davider.)
Thanks for your help, there are still problems
I need to insert text at the end of the current line.
1.When I press Alt+A for the second time, the text is added to the end of the last line
When the cursor is inside a line, press the hotkey Alt+A, and the text will be inserted inside the third line
Posts: 1,336
Threads: 61
Joined: Jul 2006
text is added to line relative to mouse position
Posts: 1,031
Threads: 246
Joined: Jul 2022
02-27-2023, 02:56 AM
(This post was last modified: 02-27-2023, 03:06 AM by Davider.)
Strange, text will be randomly inserted anywhere
Sorry, you are right, just as you said,
Quote:text is added to line relative to mouse position
cpos = cursorPos&0xFFFF
line = cursorPos>>16
The code is a little difficult to understand,How do I insert text to the end of the line where the cursor is located?
Posts: 1,031
Threads: 246
Joined: Jul 2022
02-27-2023, 04:52 AM
(This post was last modified: 02-27-2023, 05:06 AM by Davider.)
resolved!
insert text to the end of the line where the cursor is located
int lineStart = SendMessage(h EM_LINEINDEX line 0)
int lineLength = SendMessage(h EM_LINELENGTH line 0)
int lineEnd = lineStart + lineLength
SendMessage(h EM_SETSEL lineStart lineEnd)
How to take the text insertion point as the coordinate
GetCaretXY
Posts: 1,336
Threads: 61
Joined: Jul 2006
sorry got busy with other things
this line is incorrect
int lineLength = SendMessage(h EM_LINELENGTH line 0)
cannot be line number
must be character index
this should do what you want
str dd=
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 224 136 "Dialog" "4"
;3 Edit 0x54231044 0x200 8 8 208 102 ""
;1 Button 0x54030001 0x4 116 116 48 14 "OK"
;2 Button 0x54030000 0x4 168 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2040C02 "*" "" "" ""
str controls = "3"
str e3
e3="AAAAAAAAAAAAAAAAAAAAAAAAAA[]BBBBBBBBBBBBBBBBBBBBBBBBBB[]CCCCCCCCCCCCCCCCCCCCCCCCCC"
if(!ShowDialog(dd &sub.DlgProc &controls)) ret
#sub DlgProc
function# hDlg message wParam lParam
sel message
,case WM_INITDIALOG
,DT_SetAccelerators hDlg "401 Aa"
,case WM_DESTROY
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
,case 401
,int cid=id(3 hDlg)
,sub.SetCurLine(cid "_____")
ret 1
#sub SetCurLine
function h str's
int x y
GetCaretXY(x y 0 0 1)
int lh=MakeInt(x y)
int cursorPos = SendMessage(h EM_CHARFROMPOS 0 lh)
int cpos line
cpos = cursorPos&0xFFFF
line = cursorPos>>16
int lineStart = SendMessage(h EM_LINEINDEX line 0)
int lineLength = SendMessage(h EM_LINELENGTH cpos 0)
int lineEnd = lineStart + lineLength
SendMessage(h EM_SETSEL lineStart lineEnd)
_s.getsel
_s+ s
SendMessageW h EM_REPLACESEL 1 @_s
Posts: 1,031
Threads: 246
Joined: Jul 2022
02-27-2023, 05:43 AM
(This post was last modified: 02-27-2023, 05:47 AM by Davider.)
Thanks again
The following code is short and effective, but I don't know why
#sub SetCurLine2
function h str's
int cursorPos = SendMessage(h EM_CHARFROMPOS 0 -1)
int cpos = cursorPos&0xFFFF
int line = cursorPos>>16
int lineStart = SendMessage(h EM_LINEINDEX line 0)
SendMessage(h EM_SETSEL lineStart cpos)
_s.getsel
_s+ s
SendMessageW h EM_REPLACESEL 1 @_s
I can't understand the following code. Where can I find this explanation?
cursorPos&0xFFFF
cursorPos>>16
Posts: 1,336
Threads: 61
Joined: Jul 2006
02-27-2023, 06:39 AM
(This post was last modified: 02-27-2023, 06:40 AM by Kevin.)
it sort of works but has flaws. If cursor is not at end of line replacement is wherever cursor is. Better to use longer code, it is the correct way.
this
cursorPos&0xFFFF
cursorPos>>16
is the the low-order word and the high-order word of the returned value.
Edit controls: The LOWORD specifies the zero-based index of the character nearest the specified point. This index is relative to the beginning of the control, not the beginning of the line. If the specified point is beyond the last character in the edit control, the return value indicates the last character in the control. The HIWORD specifies the zero-based index of the line that contains the character. For single-line edit controls, this value is zero. The index indicates the line delimiter if the specified point is beyond the last visible character in a line.
EM_CHARFROMPOS message (Winuser.h) - Win32 apps | Microsoft Learn
Posts: 1,031
Threads: 246
Joined: Jul 2022
Very useful! thank you so much
|