如何在本地代码中设置TextBox控件中的文本? [英] How to set the text in a TextBox control from native code?
问题描述
是否可以从本机代码操作.net控件?特别是,我想从一些本地代码中设置 TextBox
的文本。我想要这样做,以保持业务逻辑与用户界面分离,但正是本地代码只知道如何适当地格式化数据(在协议内接收的一些字节)。
Is it possible to manipulate .net controls from native code? Particularly, I'd like to set the text of a TextBox
from some native code. I want it done this way to keep the business logic separated from the user interface, but it is precisely the native code which only knows how to appropriately format the data (some bytes received within a protocol).
我知道我可以通过 Handle
属性获得窗口句柄,但是一旦有,我该如何调用所需的方法?
I know I could get the window handle through the Handle
property, but once there, how would I call the desired method?
字符串处理是我唯一的选择吗?
Is string handling my only option? Should the native code build up the message to be displayed on the control?
推荐答案
设置文本的本地方式文本框是发送文本框的 WM_SETTEXT
消息:
The "native" way of setting the text on a textbox is to send the textbox the WM_SETTEXT
message:
// handle is initialized from TextBox::Handle
PostMessage(handle, WM_SETTEXT, 0, _T("Some text"));
阅读 PostMessage
和 SendMessage
。根据您分配和构建文本的方式,您可能需要使用 SendMessage
以避免过早地取消分配。
Read up on PostMessage
and SendMessage
, too. Depending on how you allocate and build up the text, you may need to use SendMessage
to avoid deallocating prematurely.
这篇关于如何在本地代码中设置TextBox控件中的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!