-2

I still learn mfc, i want to create application like webbrowser. it have seperate page that can call anytime. I try to implement that. But i can't implement the 2nd page and so on. in the code only have 2 pages. login and logout page. i put login in render method. but i dont know how to implement 2nd page (logout). for now i just directly put it inside login method. can u show me in mfc ways to achieve this?

#include <afxwin.h>

#define LOGIN_BTN 5
#define LOGOUT_BTN 6

class CMainFrame: public CFrameWnd
{
  CPoint mCoordinate;
  CSize mDimension;
  public:
    CMainFrame()
    {
      // Get primary screen resolution
         int widthScreen = GetSystemMetrics(SM_CXSCREEN);
         int heightScreen = GetSystemMetrics(SM_CYSCREEN);
         
      // Set size and position to middle screen
      mDimension.cx = 280;
      mDimension.cy = 133;
      mCoordinate.x = (widthScreen / 2) - (mDimension.cx / 2);
      mCoordinate.y = (heightScreen / 2) - (mDimension.cy / 2);

      Create(
        NULL, 
        "Pandora", 
        WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU, 
        CRect(mCoordinate, mDimension));
    };

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

    void removeAllChild()
    {
      CWnd* pChild = nullptr;
      while(true)
      {
        pChild = GetWindow(GW_CHILD);

        if(pChild == NULL) { break; }

        delete pChild;
      };
    };

    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)
{
  render();

  return 0;
};

void CMainFrame::login()
{
  // delete all child
  removeAllChild();

  // How to make this logout button such it in 2nd page? 
  CButton* logoutBtn = new CButton;
  logoutBtn->Create(
    "Logout", 
    WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 
    CRect(CPoint(40, 32), CSize(200, 70)), 
    this, LOGOUT_BTN);
};

void CMainFrame::logout()
{
  removeAllChild();
  render();
};

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

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

CApplication app;
juragan
  • 11
  • Do you need a tab control? The easiest way to do it is create a dialog based app, and insert a tab control in to it. If you have a frame window like this, you can create multiple child windows, put buttons within those child windows (or child dialogs). By the way, in your `OnCreate` method make sure to call the base class `CFrameWnd::OnCreate(lpCreateStruct);` – Barmak Shemirani Oct 26 '21 at 22:40
  • @BarmakShemirani No, i dont need tab. just a single client area that can swap entire content. like login page, logout page. after searching. i think it related to CView. but i dont know how to add CView to CFrameWnd. from I search, View is like a page. it appears only in client area section. but i dont know how to add CView to CFrameWnd. no tutorial that just show this simple thing. all tutorial is base on automation generate by App Wizard. my environment use Comamnd line to compile. – juragan Oct 27 '21 at 09:43
  • You want to create two child windows with `CWnd` or `CDialog` inside mainframe, switch between them. – Barmak Shemirani Oct 27 '21 at 11:18
  • I already try with temper first frame with so on frame. it make border inside client area. But after searching the approriate ways is use CDocument and CView. I already search until google page 10. not even tutorial or snipet code that just explain simple thing. – juragan Oct 27 '21 at 12:11
  • @BarmakShemirani How a CDocument bind to CFrameWnd. All tutorial i find is app wizard by picture. It's not i want. I need to know by Code. How to start single document. then dynamic change view. I know to handling message. But i need first step bind CDocument to CFrame. I dont see method that explain. bind CDocument to Frame. to Make clear what i want. u can look this code https://pastecode.io/s/8snnqe8z. All message event is implement in View class. like really how a webbrowser render page and listen event. index.html, login.html, logout.html – juragan Oct 27 '21 at 12:15
  • The doc/view model is too complicated, you have to read a book. You don't always have to use it. If you want to put 2 child windows inside a main frame window, and switch between them, this can be easily done and is a useful learning exercise. – Barmak Shemirani Oct 27 '21 at 13:23

1 Answers1

0

You could refer to the document : Adding Multiple Views to a Single Document. Also here is a similar thread may help: How to change MFC View by clicking a Button inside the MainFrame. You could use the code below to switch the view:

pActiveView->ShowWindow(SW_HIDE);
pNewView->ShowWindow(SW_SHOW);
((CFrameWnd *)m_pMainWnd)->SetActiveView(pNewView);
((CFrameWnd *)m_pMainWnd)->RecalcLayout();
pNewView->Invalidate();
Minxin Yu - MSFT
  • 2,234
  • 1
  • 3
  • 14