Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Edit control modifies the current line of text
#1
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
[Image: 1677664520]




Macro Macro6
Code:
Copy      Help
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
#2
If line 2 is operated first, the result is correct, why?

[Image: 1677713616]
#3
you had several mistakes.
use this instead.

Code:
Copy      Help
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
#4
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

[Image: 1677725272]

When the cursor is inside a line, press the hotkey Alt+A, and the text will be inserted inside the third line
[Image: 1677725488]
#5
text is added to line relative to mouse position
#6
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

Code:
Copy      Help
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?
#7
resolved!
insert text to the end of the line where the cursor is located

Code:
Copy      Help
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
#8
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

Code:
Copy      Help
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
#9
Thanks again 
 The following code is short and effective, but I don't know why  Big Grin

Code:
Copy      Help
#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?
Code:
Copy      Help
cursorPos&0xFFFF
cursorPos>>16
#10
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
#11
Very useful! thank you so much


Forum Jump:


Users browsing this thread: 4 Guest(s)