Jump to content

Disallow the enter key in rich text control


qwert
 Share

Recommended Posts

I have an unusual situation where a rich text field always contains 4 lines of text.  I do not want the field to accept/process an Enter key, which would create a fifth line.  $ES_WANTRETURN is the default style when the control is created.  How can I remove it?  And will that give me the behavior I want?  Or is there a better way?

I tried creating the control with only $ES_MULTILINE, but the Enter key was still accepted.

Thanks in advance for any assistance.

 

Link to comment
Share on other sites

  • Moderators

qwert,

I have been playing around and cannot find an easy solution - it seems that using $ES_MULTILINE forces $ES_WANTRETURN regardless of how you subsequently reset the styles (see the Setting Styles tutorial in the Wiki to see how you can do this).

The only thing I can think of is having 4 single line controls - but that raises problem with all the required RTF formatting. Do you have an example file that I can use to experiment?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Thanks for offering to take a look.  Here is a minimum example of the situation:

#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>

$Rich = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\lang9\fs28 These four lines\par of rich text\par were prepared\par using WordPad.\par}"

$hGui = GUICreate("Rich Test", 300, 200, -1, -1, $WS_POPUP, BitOR($WS_EX_TOPMOST, $WS_EX_COMPOSITED))

$hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 20, 20, 250, 160, $ES_MULTILINE, $WS_EX_TRANSPARENT) ; BitOR($ES_MULTILINE, $ES_AUTOVSCROLL) ;
_GUICtrlRichEdit_SetFont($hRichEdit, 14, "Arial")
GUISetState()

_GUICtrlRichEdit_StreamFromVar($hRichEdit, $Rich)

While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
            _GUICtrlRichEdit_Destroy($hRichEdit)
            Exit
    EndSwitch
WEnd

Aside from the $ES_WANT RETURN issue, where is this default font size of 10 getting defined?  How can I force 14pt for anything that might be entered?

And how can I make the cursor go to the start of the text, not the end?

 

56c3b7d70aa3a_RichExample.PNG.3067e517cb

Link to comment
Share on other sites

This kind of does what you want. You could also stream the richedit to a var, trim the new line and then set the richedit with the new info.

#include <GuiRichEdit.au3>
#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <SendMessage.au3>

Global $___pOld_WndProc = 0
Global $___hNew_WndProc = DllCallbackRegister("_New_WndProc", "int", "hwnd;uint;wparam;lparam")
Global $___pNew_WndProc = DllCallbackGetPtr($___hNew_WndProc)

$Rich = "{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}{\*\generator Msftedit 5.41.21.2510;}\viewkind4\uc1\pard\sl240\slmult1\lang9\fs28 These four lines\par of rich text\par were prepared\par using WordPad.\par}"

$hGui = GUICreate("Rich Test", 300, 200, -1, -1, $WS_POPUP, BitOR($WS_EX_TOPMOST, $WS_EX_COMPOSITED))

$hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 20, 20, 250, 160, $ES_MULTILINE, $WS_EX_TRANSPARENT) ; BitOR($ES_MULTILINE, $ES_AUTOVSCROLL) ;
_GUICtrlRichEdit_SetFont($hRichEdit, 14, "Arial")

GUISetState()
GUICtrlSetState($hRichEdit, $GUI_DISABLE)

$___pOld_WndProc = _WinAPI_SetWindowLong($hRichEdit, -4, $___pNew_WndProc)
If @error Then MsgBox("", "", "Failed to set info for richedit control")

_GUICtrlRichEdit_StreamFromVar($hRichEdit, $Rich)
_GUICtrlRichEdit_SetFont($hRichEdit, 14)

While True
    Switch GUIGetMsg()
    Case $GUI_EVENT_CLOSE
            _GUICtrlRichEdit_Destroy($hRichEdit)
            DllCallbackFree($___hNew_WndProc)
            Exit
    EndSwitch

WEnd

Func _New_WndProc($hWndFrom, $iMsg, $wParam, $lParam)
    If ($hWndFrom <> $hRichEdit) Then Return _WinAPI_CallWindowProc($___pOld_WndProc, $hWndFrom, $iMsg, $wParam, $lParam)

    Switch $iMsg
        Case 0x0100
            Switch $wParam
                Case 0x0D
                    Send("{Backspace}")
            EndSwitch
    EndSwitch

    Return _WinAPI_CallWindowProc($___pOld_WndProc, $hWndFrom, $iMsg, $wParam, $lParam)
EndFunc   ;==>_New_WndProc

As for the text formatting, you'll need update the font/size of the richedit yourself with _GUICtrlRichEdit_SetFont

Found this topic, it might help. If you didn't want to completely block input to the window you could go through the .au3 file and figure out how to just block the enter key.

 

 

Edited by InunoTaishou
Link to comment
Share on other sites

Yes, it does neutralize the Enter key 0x0D ... and I can almost follow how it does it.  Can you tell me what this statement is doing:

$___pOld_WndProc = _WinAPI_SetWindowLong($hRichEdit, -4, $___pNew_WndProc)

And can you think of any downside(s) of this "intercept" approach?  On a first-look test, it looks pretty good.

Thanks for your help.

Link to comment
Share on other sites

The statement is setting the WndProc for the richedit handle.

Create a new callback (Global $___hNew_WndProc = DllCallbackRegister("_New_WndProc", "int", "hwnd;uint;wparam;lparam"))

Get a pointer to the callback (Since Windows uses pointers) (Global $___pNew_WndProc = DllCallbackGetPtr($___hNew_WndProc))

Set the window procedure for the richedit, storing the old pointer in $__pOld_WndProc ($___pOld_WndProc = _WinAPI_SetWindowLong($hRichEdit, -4, $___pNew_WndProc))

Check out these topics to learn more

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.wndproc%28v=vs.110%29.aspx

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633573%28v=vs.85%29.aspx

As for downsides, it's not blocking the enter key, it's just recognizing that the enter key was pressed while the richedit is active, then sending backspace. There's probably a better way, I think the old hook.dll that used to be on the autoit forums could have done it without letting the enter command go to the rich edit, but this was a cleaner way than setting Enter to a hotkey whenever the $hGUI is active, and resetting the hotkey when the $hGUI is not active.

Edited by InunoTaishou
Link to comment
Share on other sites

OK, I think I follow that.  It preserves the normal processing of the rich edit control, but inserts a function to detect an Enter key ... and when one occurs, inserts a backspace code to effectively take it away.  Although it's not relevant to my use, it looks like the same method could be used to insert strings of text in response to defined key codes.

Unless there's substantial processing overhead to this method, it looks like it will suit my needs.

Thanks for taking the time to explain.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...