如何获取对话框类的类模板名(lpszTemplateName),其实想通过模板名Create()窗口,这里我不用IDD还有个问题是:我如何才能两次Create这个对话框对象。因为第二次create会失败在
CreateIndirect(hTemplate, pParentWnd, hInst);//就算我之前已经对这个对话框对象进行了 temppage1.SendMessage(WM_CLOSE);
temppage1.SendMessage(WM_DESTROY);
temppage1.DestroyWindow();

temppage1.CreateObject();
temppage1.Create(...);//失败这是我的一个试验~

解决方案 »

  1.   

    在编辑对话框资源的时候,把资源ID改成字符串形式,例如:"Dialog"(要有引号),"Dialog"就是资源名称。
    对话框销毁后可以再次创建,DestroyWindow、Create就可以,不需要发其它消息,不过其它一些地方可能会有所影响,最好重新构造对象。你可以在OnCreate、OnInitDialog函数设置断点调试一下。
      

  2.   

    我用MFC的 CDialog 的DestroyWindow再Create, 没出现这个问题调试看你的DestroyWindow 是否成功, 或者这些代码是否在他自己的消息响应中调用, 如果是也会出问题
      

  3.   

    所以想问~应该怎样做~
     其实这个对话框对像我是在一个按纽函数中弹出尝试的~ 
    static CDLG temppage1;//的 当涵数结束还在
      

  4.   


    我知道了,,这个资源名就是我所说的(lpszTemplateName),我还以为是类模板名. 
    这样一来,就是IDD和这个lpszTemplateName是不能共存的吗?要么就其中一个?
      

  5.   

    如果只是用了ID数字这种定义~那么些CreateDlg()中的参数(LPSTR lpszTemplateName)就不能用了吗?要改回来?
    那么应该都没什么人用CreateDlg(),而用Create()了那么关于CreateEx()它可不可以修改到(他里面有style参数设置应该可以,,,但我不会用~因为有lpszClassName,听说要注册?) 资源创建前 将WS_CHILD转换为WS_POPUP呢? 因为Create()的的参数没有style设置,要modifystyle只有等create后才能~
      

  6.   

    烦~
    我的CreateEx()总不成功
    我已经用了
    WNDCLASS   wndclass=   
    {   
    CS_DBLCLKS,   
    // CS_HREDRAW|   
    // CS_VREDRAW,   
    //CS_GLOBALCLASS|   
    //CS_OWNDC|   
    //CS_PARENTDC|   
    //CS_NOCLOSE|   
    AfxWndProc,   
    0,0,   
    AfxGetInstanceHandle(),   
    AfxGetApp()->LoadIcon(IDR_MAINFRAME),   
    ::LoadCursor(NULL,IDC_CROSS),   
    (HBRUSH)(COLOR_APPWORKSPACE),   
    NULL/*MAKEINTRESOURCE(IDR_MAINFRAME)*/,   
    "MyClassName"   
    };   
    AfxRegisterClass(&wndclass);  
    //page1.Create(CSheetPage::IDD,NULL);
    page1.CreateEx(0,"MyClassName"   
    ,"aa",WS_POPUPWINDOW,CRect(10,10,500,500),this,CSheetPage::IDD);/////////////////////////////////////////////////////////////////////////
    或者用了////////
    CString strMyClass; // load stock cursor, brush, and icon for
    // my own window class try
    {
    strMyClass = AfxRegisterWndClass(
    CS_VREDRAW | CS_HREDRAW,
    ::LoadCursor(NULL, IDC_ARROW),
    (HBRUSH) ::GetStockObject(WHITE_BRUSH),
    ::LoadIcon(NULL, IDI_APPLICATION));
    }
    catch (CResourceException* pEx)
    {
    AfxMessageBox(
    _T("Couldn't register class! (Already registered?)"));
    pEx->Delete();
    }
    page1.CreateEx(0, strMyClass  
    ,"aa",WS_POPUPWINDOW,CRect(10,10,500,500),this,CSheetPage::IDD);////最后的
    page1.ShowWindow(SW_SHOW);//都失败~出错,无效句柄 m_hWnd=NULL
      

  7.   


    第二次Create换一个类名如 temppage2 重新构造
      

  8.   

    我换了另一个对象,还叫第二次Create吗?我现在加分是想知道如何用CreateEx()创建对话框我发觉我用的classname都不是用SPY++查找出来对应的类名"#27750 (对话框)" 因为我发觉程序里其他的对话框类型都是这个类名~为什么噶?
      

  9.   

    对话框类是系统预先注册的类名,你自己注册一个就是普通窗口了,而不是对话框窗口,所以创建对话框必须提供对话框资源模板而不是自己注册窗口。也就是说,你要么在资源中创建对话框模板,要么在内存中创建对话框模板,然后用CDialog::CreateIndirect来创建对话框。
      

  10.   

    如何在内存中创建对话框模板?难道没有人用CreateEx()成功创建过对话框吗?
      

  11.   

    问题开始烦拉~有人说createex不能创建已经存在的对话框资源ID,因为CreateEx(...)参数中的nID是指the child of the windows不是本对话框ID...??我自己试过通过AfxRegisterWndClass(...)注册一个窗口类,成功用CreateEx()创建过对话框,但创建出来的对话框是一个空白的对话框~ 怎能创建出已有的对话框资源ID的相同对话框呢~不用Create而用CreateEx()
      

  12.   

    怎么还在基本概念上纠缠呢?上面我已经说过了:用AfxRegisterWndClass是另外注册了一个窗口,和对话框没有关系;CDialog类只重载了Create函数,你调用的CreateEx是CWnd的,所以创建的不再是对话框而是普通窗口;CDialog::Create函数中的对话框资源模板参数,不管是nIDTemplate还是lpszTemplateName,都是资源中定义的对话框模板的不同表示方式,一般都是用ID的形式,如果你在资源中用"资源名"的形式定义,就可以通过lpszTemplateName这样的资源名来调用;也可以把ID用MAKEINTRESOURCE宏转换为资源名(不过没必要)。你要在内存中构造对话框模板而不使用资源提供,就必须调用CDialog::CreateIndirect(非模态)或者CDialog::InitModalIndirect(模态)函数。具体用法,MSDN的“Using Dialog Boxes”一节中有例子,是API的,不过原理一样:Creating a Template in Memory
    Applications sometimes adapt or modify the content of dialog boxes depending on the current state of the data being processed. In such cases, it is not practical to provide all possible dialog box templates as resources in the application's executable file. But creating templates in memory gives the application more flexibility to adapt to any circumstances. In the following example, the application creates a template in memory for a modal dialog box that contains a message and OK and Help buttons. In a dialog template, all character strings, such as the dialog box and button titles, must be Unicode strings. This example uses the MultiByteToWideChar function to generate these Unicode strings. The DLGITEMTEMPLATE structures in a dialog template must be aligned on DWORD boundaries. To align these structures, this example uses a helper routine that takes an input pointer and returns the closest pointer that is aligned on a DWORD boundary.Example#define ID_HELP   150
    #define ID_TEXT   200LPWORD lpwAlign ( LPWORD lpIn)
    {
        ULONG ul;    ul = (ULONG) lpIn;
        ul +=3;
        ul >>=2;
        ul <<=2;
        return (LPWORD) ul;
    }LRESULT DisplayMyMessage(HINSTANCE hinst, HWND hwndOwner, 
        LPSTR lpszMessage)
    {
        HGLOBAL hgbl;
        LPDLGTEMPLATE lpdt;
        LPDLGITEMTEMPLATE lpdit;
        LPWORD lpw;
        LPWSTR lpwsz;
        LRESULT ret;
        int nchar;    hgbl = GlobalAlloc(GMEM_ZEROINIT, 1024);
        if (!hgbl)
            return -1;
     
        lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
     
        // Define a dialog box.
     
        lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU
                       | DS_MODALFRAME | WS_CAPTION;
        lpdt->cdit = 3;  // number of controls
        lpdt->x  = 10;  lpdt->y  = 10;
        lpdt->cx = 100; lpdt->cy = 100;    lpw = (LPWORD) (lpdt + 1);
        *lpw++ = 0;   // no menu
        *lpw++ = 0;   // predefined dialog box class (by default)    lpwsz = (LPWSTR) lpw;
        nchar = 1+ MultiByteToWideChar (CP_ACP, 0, "My Dialog", 
                                        -1, lpwsz, 50);
        lpw   += nchar;    //-----------------------
        // Define an OK button.
        //-----------------------
        lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
        lpdit = (LPDLGITEMTEMPLATE) lpw;
        lpdit->x  = 10; lpdit->y  = 70;
        lpdit->cx = 80; lpdit->cy = 20;
        lpdit->id = IDOK;  // OK button identifier
        lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;    lpw = (LPWORD) (lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0080;    // button class    lpwsz = (LPWSTR) lpw;
        nchar = 1+MultiByteToWideChar (CP_ACP, 0, "OK", -1, lpwsz, 50);
        lpw   += nchar;
        lpw = lpwAlign (lpw); // align creation data on DWORD boundary
        *lpw++ = 0;           // no creation data    //-----------------------
        // Define a Help button.
        //-----------------------
        lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
        lpdit = (LPDLGITEMTEMPLATE) lpw;
        lpdit->x  = 55; lpdit->y  = 10;
        lpdit->cx = 40; lpdit->cy = 20;
        lpdit->id = ID_HELP;    // Help button identifier
        lpdit->style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;    lpw = (LPWORD) (lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0080;                 // button class atom    lpwsz = (LPWSTR) lpw;
        nchar = 1+MultiByteToWideChar (CP_ACP, 0, "Help", -1, lpwsz, 50);
        lpw   += nchar;
        lpw = lpwAlign (lpw); // align creation data on DWORD boundary
        *lpw++ = 0;           // no creation data    //-----------------------
        // Define a static text control.
        //-----------------------
        lpw = lpwAlign (lpw); // align DLGITEMTEMPLATE on DWORD boundary
        lpdit = (LPDLGITEMTEMPLATE) lpw;
        lpdit->x  = 10; lpdit->y  = 10;
        lpdit->cx = 40; lpdit->cy = 20;
        lpdit->id = ID_TEXT;  // text identifier
        lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;    lpw = (LPWORD) (lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0082;                         // static class    for (lpwsz = (LPWSTR)lpw;    
            *lpwsz++ = (WCHAR) *lpszMessage++;
        );
        lpw = (LPWORD)lpwsz;
        lpw = lpwAlign (lpw); // align creation data on DWORD boundary
        *lpw++ = 0;           // no creation data    GlobalUnlock(hgbl); 
        ret = DialogBoxIndirect(hinst, (LPDLGTEMPLATE) hgbl, 
            hwndOwner, (DLGPROC) DialogProc); 
        GlobalFree(hgbl); 
        return ret; 
    }
      

  13.   

    好!~ 终于都明白个原由了.      代码还没全吗?可惜API的...    呵呵~谢谢了~