简体   繁体   English

使用WM_SETTEXT设置记事本文本不会影响记事本实例中的Text_Changed事件

[英]Using WM_SETTEXT to set Notepad text is not affecting Text_Changed event in the Notepad instance

I've almost completed a project that will basically, take the contents of a .txt file, open a new notepad instance using 'Process.Start("notepad")' and then set the text using the "WM_SETTEXT" constant. 我几乎已经完成了一个项目,该项目基本上将采用.txt文件的内容,使用'Process.Start(“ notepad”)'打开一个新的记事本实例,然后使用“ WM_SETTEXT”常量设置文本。 I have this working beautifully so I don't need any assistance with setting the text. 我的工作非常漂亮,因此设置文本不需要任何帮助。 However, I have one small issue I noticed during testing. 但是,我在测试过程中发现了一个小问题。 After the text is set in the notepad instance, if you close the notepad window, it does NOT ask if you wish to save the changes. 在记事本实例中设置文本后,如果关闭记事本窗口,则不会询问您是否要保存更改。 This led me to believe that "WM_SETTEXT" does NOT fire the "Text_Changed" event in the notepad instance. 这使我相信“​​ WM_SETTEXT”不会在记事本实例中触发“ Text_Changed”事件。

After some thinking, I realized that it could also mean that notepad only asks to save changes if a user types something manually. 经过一番思考,我意识到这也可能意味着记事本仅在用户手动输入内容时要求保存更改。 So perhaps rather than checking for "Text_Changed" it may be checking for a "Mouse_Down" event?? 因此,也许不是检查“ Text_Changed”,而是检查“ Mouse_Down”事件? Whichever one it may be, I would like some input from someone with a little more knowledge as to the 'internal' workings of notepad and how it checks if there were 'changes' to the text that need to be saved. 无论是哪种方式,我都希望有人对记事本的“内部”工作以及如何检查是否需要保存的文本进行“更改”的知识有所了解。

Here is part of my SetText function: 这是我的SetText函数的一部分:

        if (FileIO.Index != null && FileIO.Index.Count > 0)
        {
            MessageBox.Show("Recovering " + FileIO.Index.Count + " files...");
            foreach (string guid in FileIO.Index)
            {
                if (!string.IsNullOrWhiteSpace(guid))
                {
                    string contents = "";
                    if (!FileIO.Recover(guid, out contents))
                        MessageBox.Show("Couldn't recover '" + guid + "'");
                    else
                    {
                        Process p = Process.Start("notepad");
                        Thread.Sleep(200);
                        SetText(GetNotepadEditBox(p.MainWindowHandle), contents);
                    }
                }
            }
            MessageBox.Show("Recovered all files successfully!", "Done recovering files!", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

Methods: 方法:

private static IntPtr GetNotepadEditBox(IntPtr hParentWindow)
{
    return Win32.FindWindowEx(hParentWindow, IntPtr.Zero, "Edit", null);
}

private void SetText(IntPtr hEditBox, string text)
{
    IntPtr len = new IntPtr(text.Length);
    StringBuilder sb = new StringBuilder(text);

    Win32.SendMessage(hEditBox, Win32.WM_SETTEXT, len, sb);

    sb = null;
    len = IntPtr.Zero;
}

The edit control keeps the properties text and modified as distinct entities, allowing applications to build their own modification management around them. 编辑控件保留属性文本并将其修改为不同的实体,从而允许应用程序围绕它们构建自己的修改管理。 Sending a WM_SETTEXT message does not automatically set the modified flag. 发送WM_SETTEXT消息不会自动设置修改后的标志。 To set this flag you have to explicitly send an EM_SETMODIFY message to the edit control. 要设置此标志,您必须向编辑控件明确发送EM_SETMODIFY消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM