简体   繁体   中英

How to make a CDialog?

I have tried multiple things but the base comes to this:

#include <stdio.h>
#include <afxwin.h>


main( int argc, const char* argv[] )
{

   printf( "\nHello World\n\n" );

   CDialog *dlg = new CDialog();
   dlg->DoModal();

   while (true) {
      Sleep(1); // Sleep is a windows function
   }
}

When I run this, I get the following error:

调试断言失败消息

What am I missing for this dialog?

I looked up several resources, but everything results in the same error message.

Can someone tell me what am I not seeing?

Using the MFC in a console application requires some initializations. Without this you will get asserts.

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: MFC initialization failed\n"));
        return 8;
    }

You must also use a resource that is bound to the CDialog. You may use the appropriate constructors. Or you derive your own dialog from CDialog using the class wizard.

But it doesn't make sense to me to create an MFC console application and use dialogs... Your question may need more details, what you want to do, and why you want to do it in this way.

You may need to read some books or article before you continue this way of programming.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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