how do I find the handle of the Frame window..?

I'm trying to Create a Simple Frame window using CFrameWnd Class.  in the OnCreate() function I'm Writing  code to Create Button using CButton:: Create() function but in that function
it taking parent window handle do I find the handle of the Frame window..please help me.

CButton myBtn;
      afx_msg int OnCreate(LPCREATESTRUCT lp)
            myBtn.Create("Click me",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,CRect(10,10,20,30),CFrameWnd::GetActiveView(),1);
            return 0;
thank you
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

the handle of the mainframe window is assigned in the InitInstance member function of your App class which is derived from CWinApp.

for a SDI or MDI MFC application you will find a statement

m_pMainWnd = pMainFrame;

Open in new window

after the mainframe was created.

you easily can retrieve the main window (as CWnd pointer) by calling AfxGetMainWnd() from anywhere in member functions of your MFC application.

CFrameWnd * pMainFrame = (CFrameWnd *)AfxGetMainWnd();

Open in new window

if you don't have a SDI or MDI application, but a dialog-based mfc app, you better would make a new project where you decide either for single document or multi-document mfc application. only those really can handle windows derived from CFrameWnd without greater issues.

you need a cast as m_pMainWnd is only a CWnd * pointer in order to be usable for dialog-based mfc apps as well.

a window handle of the main window can be retrieved by

CWnd * pMainWnd = AfxGetMainWnd();
HWND mainHwnd = (pMainWnd != NULL)? pMainWnd->m_hWnd : (HWND)(NULL);

Open in new window

the mainframe (and other mfc frame windows) are created by using a CMultiDocTemplate or CSingleDocTemplate helper class which makes a triple out of frame class, document class, and view class.

you may look into the InitInstance function to get familiar with that technique.

anyway, if you want to create a child frame to your main frame by means of a button click, you would use a CMultiDocTemplate helper variable rather than creating a single CFrameWnd. that is because each frame needs a document class (derived from CDocument) and a view class (derived from CView) in order to work properly and to be able to customize it.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The MFC framework offers different ways to retrieve objects like frames, documents and frames. Frame windows can retrieved from a view by simply calling GetParent member function. As told, the main frame window of an MDI or SDI apllication can be retrieved from everywhere by calling AfxGetMainWnd function. Child frames of this main frame could be retrieved by enumerating the child windows of the main frame, although it might be easier to store a newly created child frame (pointer) into a member variable or into an array member of the mainframe class.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.