首页 > 解决方案 > 如何制作toggle child?

问题描述

我创建了一个登录框架,它可以根据登录成功、孩子更改等情况像往常一样切换。并注销。但我的切换卡在这个过程中。登录->注销->登录。第二次登录后。孩子不想改回注销。它卡住了。我检查了几次并尝试了所有的东西,比如添加 UpdateWindow、SetWindowPos

#define LOGIN_BTN 1
#define LOGOUT_BTN 2

class CMainFrame: public CFrameWnd
{
  public:
    CMainFrame()
    {
      Create(
        NULL,
        "Learn MFC",
        WS_OVERLAPPEDWINDOW,
        CRect(CPoint(100, 100), CSize(640, 360)));
    };
  protected:
    afx_msg int OnCreate(LPCREATESTRUCT);
    afx_msg void login();
    afx_msg void logout();
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_COMMAND(LOGIN_BTN, &CMainFrame::login)
ON_COMMAND(LOGOUT_BTN, &CMainFrame::logout)
END_MESSAGE_MAP()

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
  CButton* loginBtn = new CButton();
  loginBtn->Create(
    "Login", 
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    CRect(CPoint(100, 60), CSize(150, 25)), 
    this, LOGIN_BTN);

  return 0;
};

void CMainFrame::login()
{
  std::string resServer = "token";

  if(resServer == "token")
  {
    // Destroy all childs window
    while(GetWindow(GW_CHILD))
    {
      CWnd* pChild = GetWindow(GW_CHILD);
      pChild->DestroyWindow();
    }

    CButton* logoutBtn = new CButton;
    logoutBtn->Create(
      "Logout", 
      WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
      CRect(CPoint(100, 60), CSize(150, 25)), 
      this, LOGOUT_BTN);
  };
}

void CMainFrame::logout()
{
  CButton* loginBtn = new CButton();
  loginBtn->Create(
    "Login", 
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    CRect(CPoint(100, 60), CSize(150, 25)), 
    this, LOGIN_BTN);
};

class CApplication : public CWinApp {
   BOOL InitInstance() {
      CMainFrame* mainWnd = new CMainFrame();
      m_pMainWnd = mainWnd;

      mainWnd->ShowWindow(SW_NORMAL);
      mainWnd->UpdateWindow();
      
      return TRUE;
   }
};

CApplication app; 

标签: c++winapimfc

解决方案


推荐阅读