简体   繁体   English

On Update命令不适用于MFC中动态创建的按钮

[英]On Update command is not working for dynamically created button in MFC

In my code, i have added button in view class in OnCreate(). 在我的代码中,我在OnCreate()中的视图类中添加了按钮。 I included On Command and On Update COmmand funtionality. 我包括On Command和On Update COmmand funtionality。 Here On command fucntion is working when i click the button. 这里当我点击按钮时命令功能正常工作。 But On Update COmmand is not working. 但是On Update COmmand不起作用。 Im updating the pressing status of the button using this OnUpdateCommand(). 我使用这个OnUpdateCommand()更新按钮的按下状态。

In OnCreate() 在OnCreate()

rBar.left = 580;
    rBar.right = 620;
    cBZoomOut.Create("",WS_CHILD|WS_VISIBLE|BS_BITMAP ,rBar,this,IDC_TZOOMOUT);
    cBZoomOut.SetIcon(IDI_TZOOMOUT);

    rBar.left = 625;
    rBar.right = 665;
    cBZoomin.Create("",WS_CHILD|WS_VISIBLE|BS_BITMAP ,rBar,this,IDC_TZOOMIN);
    cBZoomin.SetIcon(IDI_TZOOMIN);

Message maps for those buttons. 这些按钮的消息映射。

    afx_msg void OnUpdateTzoomout(CCmdUI *pCmdUI);
    afx_msg void OnTzoomin();
    afx_msg void OnUpdateTzoomin(CCmdUI *pCmdUI);
    afx_msg void OnTzoomout();

    ON_UPDATE_COMMAND_UI(IDC_TZOOMOUT, &CTrendView::OnUpdateTzoomout)
    ON_COMMAND(IDC_TZOOMIN, &CTrendView::OnTzoomin)
    ON_UPDATE_COMMAND_UI(IDC_TZOOMIN, &CTrendView::OnUpdateTzoomin)
    ON_COMMAND(IDC_TZOOMOUT, &CTrendView::OnTzoomout)

On command and OnUpdatecommand function: 在命令和OnUpdate命令函数:

void CTrendView::OnTzoomout()
{   
    sTimeStatus.Format("<=>%d",Minute/2);

}
void CTrendView::OnUpdateTzoomout(CCmdUI *pCmdUI)
{   
    if (Minute == 16)
        pCmdUI->Enable(FALSE);
    else
        pCmdUI->Enable(TRUE);
}

In both Zoomin and Zoomout function, OnUpdateCommnad is not working. 在Zoomin和Zoomout功能中,OnUpdateCommnad无法正常工作。

This routing isn't done automatically. 此路由不会自动完成。

You have to handle WM_IDLEUPDATECMDUI . 你必须处理WM_IDLEUPDATECMDUI Usually you call the internal OnUpdateCmdUI virtual function. 通常您调用内部OnUpdateCmdUI虚函数。 This finally calls UpdateDialogControls . 这最终调用UpdateDialogControls

You find the details in TN021 您可以在TN021中找到详细信息

Just set a breakpoint on a working OnUpdate Handler. 只需在正在运行的OnUpdate Handler上设置断点即可。 And look into the call stack. 并查看调用堆栈。 Than you can see and imagine how the whole stuff works. 你可以看到并想象整个东西是如何运作的。

There is also a possible way to use WM_KICKIDLE and UpdateDialogControls . 还有一种可能的方法来使用WM_KICKIDLEUpdateDialogControls See this article . 看到这篇文章

Try the following. 请尝试以下方法。

In TrendView.h add this: TrendView.h添加:

afx_msg LRESULT OnKickIdle(WPARAM wParam, LPARAM lParam);

In TrendView.cpp add this: TrendView.cpp添加:

#include <afxpriv.h>

...
ON_MESSAGE(WM_KICKIDLE, OnKickIdle)
...

LRESULT CTrendView::OnKickIdle(WPARAM wParam, LPARAM lParam)
{
    UpdateDialogControls(this, FALSE);
    return 0;
}

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

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