Jump to content

_GUICtrlRichEdit_AppendText steals focus


Habland0
 Share

Recommended Posts

I am writing text to a RichEdit control using _GUICtrlRichEdit_AppendText(). If my script currently doesn't have focus, the next call to_GUICtrlRichEdit_AppendText() causes the script to steal focus. However, if I then switch to another app again (thus causing my script to lose focus), the problem no longer occurs and any subsequent calls to _GUICtrlRichEdit_AppendText() are silently written to the RichEdit control.

How can I make it so _GUICtrlRichEdit_AppendText() never steals focus in the first place?

For fun I even set the window to @SW_HIDE and it still stole focus when I invoked _GUICtrlRichEdit_AppendText() ;)

Here is a script you can compile to quickly test it yourself:

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

    Global $hGui, $hRichEdit, $iMsg
    $hGui = GUICreate("Example (" & StringTrimRight(@ScriptName,4) &")", 320, 350, -1, -1)
    $hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 300, 220, _
            BitOR($ES_MULTILINE, $WS_VSCROLL, $ES_AUTOVSCROLL))
    GUISetState()

    For $i = 0 To 5
    _GUICtrlRichEdit_AppendText($hRichEdit, @CR & "Switch to another application and wait 5 seconds...")
    Sleep(5000)
    Next
Edited by Habland0
Link to comment
Share on other sites

Hi,

After looking through the GuiRichEdit.au3 I can see where the default behavior is coming from.

_GUICtrlRichEdit_Create() has a few Styles added regardless of what style you set, these forced styles make the richedit behave as it should.

Seems like there are a couple of ways to overide the styles, but each way has it's quirks.

The 2 forced styles that seem to cause the default steel focus behavior seem to be WS_CHILD and WS_VISIBLE.

Cheap workaround, but also has it's own quirks..

#include <GuiRichEdit.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <WinAPI.au3>
#Include <Constants.au3>

Global $hGui, $hRichEdit, $iMsg

$hGui = GUICreate("Example (" & StringTrimRight(@ScriptName,4) &")", 320, 350, -1, -1)
$hRichEdit = _GUICtrlRichEdit_Create($hGui, "", 10, 10, 300, 220, -1, -1)
_WinAPI_SetWindowLong($hRichEdit, $GWL_STYLE, BitOR($ES_MULTILINE, $ES_WANTRETURN))
WinSetState($hRichEdit, "", @SW_SHOW)
GUISetState(@SW_SHOW)

For $i = 0 To 5
    _GUICtrlRichEdit_AppendText($hRichEdit, @CR & "Switch to another application and wait 5 seconds...")
    Sleep(5000)
Next

_GUICtrlRichEdit_Destroy($hRichEdit)

Cheers

Link to comment
Share on other sites

The 2 forced styles that seem to cause the default steel focus behavior seem to be WS_CHILD and WS_VISIBLE.

Pretty clever workaround by removing the forced attributes and then @SW_SHOW it anyway. I suppose this way Windows still assumes the control is not visible and therefor no focus stealing occurs. Smashing ;)

Edit: does have the drawback that the RichEdit control loses the ability to autoscroll or show any scrollbars (even when the required styles are added). Nevertheless, thanks for enlightening me on this issue. It's great to see different approaches.

Edited by Habland0
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...