开一个新窗口要注册窗口类。我的问题是想利用MFC已经注册的窗口类来开新窗口,候杰的《深入前出MFC》中也提到了5个已经被MFC注册的窗口类,如:AfxWnd42d。我在dotnet下做VC的程序,直接使用AfxWnd42d,错误是未声明。现在就不知道在dotnet已经被注册的窗口类名称是什么,是不是必须得注册一个新的窗框类才能开窗口。
期待高手的指点。

解决方案 »

  1.   

    CString m_clsName;  //窗口类中
    m_clsName =  AfxRegisterWndClass( UINT nClassStyle, HCURSOR hCursor = 0, HBRUSH hbrBackground = 0, HICON hIcon = 0 ); 注册完了之后,在窗口类的PreCreateWindow函数中,cs.lpszClass = m_clsName; 那么窗口类就是新的类了。如果感觉AfxRegisterWndClass不怎么强大,用
    BOOL AFXAPI AfxRegisterClass( WNDCLASS* lpWndClass );
      

  2.   

    谢谢,一般来说,我们都是点击菜单弹出对话框的。我的程序要弹出一个视频窗口,用对话框不合适,所以要自己创建窗口。可以为所要创建的窗口做一个派生自CWnd的类,也可以使用AfxRegisterWndClass注册一个类再CreateEx一个窗口,我不想创建太多的类,所以在函数里开新的窗口。
    我的问题其实就是是不是非得注册一个新的窗口类。理论上来说,MFC肯定已经注册了窗口类,不然就没法创建窗口了。用一个窗口类可以生成多个窗口,我就是想看看这些类的名称。候杰的书中介绍过,但是不详细。
    不知有没有人对这一问题研究过,还往多指点。
      

  3.   

    BOOL AFXAPI AfxEndDeferRegisterClass(LONG fToRegister)
    {
    // mask off all classes that are already registered
    AFX_MODULE_STATE* pModuleState = AfxGetModuleState();
    fToRegister &= ~pModuleState->m_fRegisteredClasses;
    if (fToRegister == 0)
    return TRUE; LONG fRegisteredClasses = 0; // common initialization
    WNDCLASS wndcls;
    memset(&wndcls, 0, sizeof(WNDCLASS));    // start with NULL defaults
    wndcls.lpfnWndProc = DefWindowProc; //窗口过程
    wndcls.hInstance = AfxGetInstanceHandle(); //实例句柄
    wndcls.hCursor = afxData.hcurArrow; //光标 INITCOMMONCONTROLSEX init;
    init.dwSize = sizeof(init); // work to register classes as specified by fToRegister, populate fRegisteredClasses as we go
    if (fToRegister & AFX_WND_REG)
    {
    // Child windows - no brush, no icon, safest default class styles
    wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndcls.lpszClassName = _afxWnd;
    if (AfxRegisterClass(&wndcls))
    fRegisteredClasses |= AFX_WND_REG;
    }
    if (fToRegister & AFX_WNDOLECONTROL_REG)
    {
    // OLE Control windows - use parent DC for speed
    wndcls.style |= CS_PARENTDC | CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndcls.lpszClassName = _afxWndOleControl;
    if (AfxRegisterClass(&wndcls))
    fRegisteredClasses |= AFX_WNDOLECONTROL_REG;
    }
    if (fToRegister & AFX_WNDCONTROLBAR_REG)
    {
    // Control bar windows
    wndcls.style = 0;   // control bars don't handle double click
    wndcls.lpszClassName = _afxWndControlBar;
    wndcls.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
    if (AfxRegisterClass(&wndcls))
    fRegisteredClasses |= AFX_WNDCONTROLBAR_REG;
    }
    if (fToRegister & AFX_WNDMDIFRAME_REG)
    {
    // MDI Frame window (also used for splitter window)
    wndcls.style = CS_DBLCLKS;
    wndcls.hbrBackground = NULL;
    if (_AfxRegisterWithIcon(&wndcls, _afxWndMDIFrame, AFX_IDI_STD_MDIFRAME))
    fRegisteredClasses |= AFX_WNDMDIFRAME_REG;
    }
    if (fToRegister & AFX_WNDFRAMEORVIEW_REG)
    {
    // SDI Frame or MDI Child windows or views - normal colors
    wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
    wndcls.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    if (_AfxRegisterWithIcon(&wndcls, _afxWndFrameOrView, AFX_IDI_STD_FRAME))
    fRegisteredClasses |= AFX_WNDFRAMEORVIEW_REG;
    }
    if (fToRegister & AFX_WNDCOMMCTLS_REG)
    {
    // this flag is compatible with the old InitCommonControls() API
    init.dwICC = ICC_WIN95_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WIN95CTLS_MASK);
    fToRegister &= ~AFX_WIN95CTLS_MASK;
    }
    if (fToRegister & AFX_WNDCOMMCTL_UPDOWN_REG)
    {
    init.dwICC = ICC_UPDOWN_CLASS;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_UPDOWN_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_TREEVIEW_REG)
    {
    init.dwICC = ICC_TREEVIEW_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_TREEVIEW_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_TAB_REG)
    {
    init.dwICC = ICC_TAB_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_TAB_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_PROGRESS_REG)
    {
    init.dwICC = ICC_PROGRESS_CLASS;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_PROGRESS_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_LISTVIEW_REG)
    {
    init.dwICC = ICC_LISTVIEW_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_LISTVIEW_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_HOTKEY_REG)
    {
    init.dwICC = ICC_HOTKEY_CLASS;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_HOTKEY_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_BAR_REG)
    {
    init.dwICC = ICC_BAR_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_BAR_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_ANIMATE_REG)
    {
    init.dwICC = ICC_ANIMATE_CLASS;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_ANIMATE_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_INTERNET_REG)
    {
    init.dwICC = ICC_INTERNET_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_INTERNET_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_COOL_REG)
    {
    init.dwICC = ICC_COOL_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_COOL_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_USEREX_REG)
    {
    init.dwICC = ICC_USEREX_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_USEREX_REG);
    }
    if (fToRegister & AFX_WNDCOMMCTL_DATE_REG)
    {
    init.dwICC = ICC_DATE_CLASSES;
    fRegisteredClasses |= _AfxInitCommonControls(&init, AFX_WNDCOMMCTL_DATE_REG);
    } // save new state of registered controls
    pModuleState->m_fRegisteredClasses |= fRegisteredClasses; // special case for all common controls registered, turn on AFX_WNDCOMMCTLS_REG
    if ((pModuleState->m_fRegisteredClasses & AFX_WIN95CTLS_MASK) == AFX_WIN95CTLS_MASK)
    {
    pModuleState->m_fRegisteredClasses |= AFX_WNDCOMMCTLS_REG;
    fRegisteredClasses |= AFX_WNDCOMMCTLS_REG;
    } // must have registered at least as mamy classes as requested
    return (fToRegister & fRegisteredClasses) == fToRegister;
    }
    MFC内部也是用AfxRegisterWndClass注册的。其实真是的什么类名是一个什么鬼东西的数字,用spy++可以看到的。
      

  4.   

    如果是对话框,他在
    CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd)
    函数中这么做:
    VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG));
    AfxDeferRegisterClass(AFX_WNDCOMMCTLSNEW_REG);
    注册的初始类是AFX_WNDCOMMCTLSNEW_REG。
    //AFXIMPL.H
    #define AFX_WND_REG                     0x00001
    #define AFX_WNDCONTROLBAR_REG           0x00002
    #define AFX_WNDMDIFRAME_REG             0x00004
    #define AFX_WNDFRAMEORVIEW_REG          0x00008
    #define AFX_WNDCOMMCTLS_REG             0x00010 // means all original Win95
    #define AFX_WNDOLECONTROL_REG           0x00020
    #define AFX_WNDCOMMCTL_UPDOWN_REG       0x00040 // these are original Win95
    #define AFX_WNDCOMMCTL_TREEVIEW_REG     0x00080
    #define AFX_WNDCOMMCTL_TAB_REG          0x00100
    #define AFX_WNDCOMMCTL_PROGRESS_REG     0x00200
    #define AFX_WNDCOMMCTL_LISTVIEW_REG     0x00400
    #define AFX_WNDCOMMCTL_HOTKEY_REG       0x00800
    #define AFX_WNDCOMMCTL_BAR_REG          0x01000
    #define AFX_WNDCOMMCTL_ANIMATE_REG      0x02000
    #define AFX_WNDCOMMCTL_INTERNET_REG     0x04000 // these are new in IE4
    #define AFX_WNDCOMMCTL_COOL_REG         0x08000
    #define AFX_WNDCOMMCTL_USEREX_REG       0x10000
    #define AFX_WNDCOMMCTL_DATE_REG         0x20000#define AFX_WIN95CTLS_MASK              0x03FC0 // UPDOWN -> ANIMATE
    #define AFX_WNDCOMMCTLSALL_REG          0x3C010 // COMMCTLS|INTERNET|COOL|USEREX|DATE
    #define AFX_WNDCOMMCTLSNEW_REG          0x3C000 // INTERNET|COOL|USEREX|DATE
      

  5.   

    Provided WNDCLASSesIn previous versions of MFC (before MFC 4.0), there were a number of predefined Window classes provided. These Window classes are no longer provided by default because of technical problems related to versioning (multiple versions of MFC loaded in one address space) as well as concerns relating to the fact that both MFC applications and OLE Controls may use the MFC DLLs.The following reference is provided to help migrate code that uses these previously provided WNDCLASSes. Applications should use AfxRegisterWndClass (with the appropriate parameters) in place of these classes.The following shows the classes and their attributes: "AfxWnd" is used for all child windows created with CWnd::Create. 
    class style : CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW 
    no icon 
    arrow cursor 
    no background color 
    "AfxFrameOrView" is used for frame windows and views (including stand-alone CFrameWnds and CMDIChildWnds). 
    class style : CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; 
    icon AFX_IDI_STD_FRAME 
    arrow cursor 
    COLOR_WINDOW background color 
    "AfxMDIFrame" is used for the MDI frame window (that is, the parent) created with CMDIFrameWnd::Create. 
    class style : CS_DBLCLKS [ reduces flash during resizing ] 
    icon AFX_IDI_STD_MDIFRAME 
    arrow cursor 
    no background color 
    "AfxControlBar" is used for the standard control bar implementation. 
    class style : 0 [ reduces flash during resizing, no double clicks ] 
    no icon 
    arrow cursor 
    gray background color (COLOR_BTNFACE)