消息映射如何与 SendMessage() 方法交互?
How do message maps interface with the SendMessage() method?
尽管阅读了很多MSDN文章,但我似乎无法理解MFC的消息映射和SendMessage()
函数。现在我有一个名为IDC_IPADDRESS_MYADDRESS
的 IP 控件,我想设置它的值。我知道IPM_SETADDRESS
是正确的消息类型,但我不知道如何成功发送消息并更新 ip 控件的值。
我需要向我的消息映射添加什么,
BEGIN_MESSAGE_MAP(myDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, &myDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
如何正确使用该映射条目来更新 IP 地址控件的值?下面是我在对话框 init 方法中使用 SendMessage() 调用更新它的尝试。
// myDlgmessage handlers
BOOL myDlg::OnInitDialog()
{
myDlg::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
//set default IP address
DWORD IP = MAKEIPADDRESS(192, 168, 0, 254);
SendMessage(IPM_SETADDRESS, 0, IP);
return TRUE; // return TRUE unless you set the focus to a control
}
SendMessage(IPM_SETADDRESS, 0, IP);
IPM_SETADDRESS
是正确的消息,但它被发送到主对话框。对话框未查找此消息并忽略它。
您希望改为将消息发送到 IP 控件。这意味着您需要 IP 地址控制的窗口句柄:
CWnd *ptr_ip_address = GetDlgItem(IDC_IPADDRESS_MYADDRESS);
if (ptr_ip_address)
ptr_ip_address->SendMessage(IPM_SETADDRESS, 0, IP);
在 MFC 中,可以改用CIPAddressCtrl
类。您必须声明m_ip_address
并用DoDataExchange
对其进行子类化。此类还具有SetAddress
方法。
class CMyDialog : public CDialogEx
{
...
CIPAddressCtrl m_ip_address;
void DoDataExchange(CDataExchange* pDX);
};
void CMyDialog::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_IPADDRESS_MYADDRESS , m_ip_address);
}
BOOL myDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
m_ip_address.SetAddress(192, 168, 0, 254);
...
}
MFC 消息映射与您的问题没有直接关系。消息映射用于响应窗口消息。例如,您要响应ON_BN_CLICKED
。但在这里,您向控件发送消息,而不是接收消息。
您可以在 WinAPI 编程书籍中阅读有关此内容的更多信息。在普通窗口编程中,有一个"消息循环"和一个"窗口过程",您可以在其中响应消息。MFC 使用消息映射来简化此过程。
BOOL myDlg::OnInitDialog() { myDlg::OnInitDialog(); <- recursive ... }
顺便说一下,将myDlg::OnInitDialog
放入myDlg::OnInitDialog
会导致堆栈溢出。改为调用基类,CDialogEx::OnInitDialog();
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- 通过方法访问结构
- 最小硬币更换问题(自上而下方法)
- C++为构建时间获取QDateTime的可靠方法
- 在C#中处理C++指针而不使用unsafe的最佳方法
- Qt VTK交互风格的信号到小部件
- 处理多个异常集合的C++方法
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 有什么方法可以遍历结构吗
- 当类在C++中定义时,有什么方法可以"register"类吗?
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- 使用std::函数映射对象方法
- 在QGraphicsView上实现交互式样条曲线的最佳方法是什么?
- 消息映射如何与 SendMessage() 方法交互?
- 生成代码以与不需要重新编译的正在运行的 cpp 应用程序交互的好方法是什么?
- 类方法与static和const的交互
- 有没有任何方法可以使用Qt和C++与网页进行交互
- 以交互方式将图像从Haskell后端发送到Qt QUI的最佳方法
- 如何在静态方法中与QPushButton交互