您的位置:首页 > 其它

窗口的创建过程:

2017-05-25 16:17 155 查看
窗口的创建过程:

1 定义窗口类,填充如下的结构体,其中hIcon,hCursor,都是必须的,否则窗口注册就会失败

typedef struct _WNDCLASS {

UINT style;

WNDPROC lpfnWndProc;

int cbClsExtra;

int cbWndExtra;

HINSTANCE hInstance;

HICON hIcon;

HCURSOR hCursor;

HBRUSH hbrBackground;

LPCTSTR lpszMenuName;

LPCTSTR lpszClassName;

} WNDCLASS, *PWNDCLASS;

2 调用RegisterWindow注册窗口类

3 调用CreateWindow创建窗口

4 显示窗口

5 更新窗口

你打开资源文件看看IDR_MAINFRAME和菜单,图标,位图都有关联,如果你把它删了,程序就没有资源了

不出错才怪呢
https://support.microsoft.com/zh-cn/help/131368/how-to-create-mfc-applications-that-do-not-have-a-menu-bar-in-visual-c


How to create MFC applications that do not have a menu bar in Visual C++

电子邮件
打印

Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided
by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.

Note Microsoft Visual C++ 2005 supports both the managed code model that is provided by the .NET Framework and the
unmanaged native Windows code model.

https://support.microsoft.com/zh-cn/help/131368/how-to-create-mfc-applications-that-do-not-have-a-menu-bar-in-visual-cSummary

For most Windows-based applications, a menu bar is a part of the user interface. The menu bar provides a functionality summary for the person using the program. However, it is not required that every Windows-based application must contain a menu bar. This article
describes how to create an MFC application that does not have a menu bar.

For Windows-based applications generated by AppWizard, the IDR_MAINFRAME menu resource is the standard menu resource for both SDI and MDI applications. It is the only menu resource for an SDI application. MDI applications contain additional menus for each type
of MDI child window they support. Those menu resources are usually named IDR_xxxTYPE, where xxx is related to the name of the corresponding document type. Thus, creating an application with no menus is not as easy for an MDI application as for an SDI application.
You basically have to modify all functions related to loading and switching menus.


More Information


Steps to Create SDI Application that Has No Menu Bar

Generate an SDI application with AppWizard. Do not delete the IDR_MAINFRAME menu resource. If you have an application that was not generated with AppWizard, do not delete the corresponding main menu resource. Leaving the menu
resource is required to avoid assertion failures in the MFC code.
To prevent the main application window from having a menu bar, delete the already loaded menu, and set the hMenu field of the CREATESTRUCT structure to NULL in the CFrameWnd::PreCreateWindow() function:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if(cs.hMenu!=NULL)
{
::DestroyMenu(cs.hMenu);      // delete menu if loaded
cs.hMenu = NULL;              // no menu for this window
}

return CFrameWnd::PreCreateWindow(cs);
}



Steps to Create MDI Application that Has No Menu Bar

Generate an MDI application with AppWizard. Do not delete the IDR_MAINFRAME menu resource. If you have an application that was not generated with AppWizard, do not delete the corresponding main menu resource. Leaving the menu
resource is required to avoid assertion failures in the MFC code.
Delete menu resources associated with MDI child windows (IDR_xxxTYPE). They are not used. By deleting them, you avoid a resource (memory) leak.
Override the PreCreateWindow() function for the CMainFrame class:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
if(cs.hMenu!=NULL)
{
::DestroyMenu(cs.hMenu);      // delete menu if loaded
cs.hMenu = NULL;              // no menu for this window
}

return CMDIFrameWnd::PreCreateWindow(cs);
}


Modify the code responsible for switching menus by overriding the LoadFrame() and OnCreateClient() methods of CMainFrame. This is necessary because MFC has already loaded and switched menus automatically. The following shows what
must be done:

// Overridden method declarations for CMainFrame

BOOL LoadFrame( UINT nIDResource,
DWORD dwDefaultStyle = WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE,
CWnd* pParentWnd = NULL, CCreateContext* pContext = NULL );
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs,
CCreateContext* /*pContext*/);

// Overridden method declarations for CMainFrame

BOOL CMainFrame::LoadFrame(UINT nIDResource, DWORD dwDefaultStyle,
CWnd* pParentWnd, CCreateContext* pContext)
{
return CFrameWnd::LoadFrame(nIDResource,dwDefaultStyle,
pParentWnd,pContext);
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs,
CCreateContext* /*pContext*/)
{
return CreateClient(lpcs,NULL);
}


NOTE: Instead of calling the base class (CMDIFrameWnd) in the override of LoadFrame, you call its base class, CFrameWnd,
instead. That way you can avoid the code that deals with MDI menus.

作为一个MFC新手可能不知道怎样切换menu。在网上也很难找到相关资料。

当应用程序向导生成的 SDI 或 MDI 应用程序时,它创建了菜单资源 (使用 IDR_MAINFRAME 作为其资源 id。此菜单显示在所有时间 SDI 应用程序,并且仅在 MDI 应用程序中没有活动文档时。对于 MDI 应用程序中,应用程序向导还会生成菜单资源为一个 multidoc 模板创建使用 IDR_xxxxTYPE 作为其资源 id。附加的菜单资源可供其使用资源编辑器创建和使用另一台 multidoc 模板关联在一起 (见 CMultiDocTemplate)。该框架将显示与每个 multidoc 模板自动关联的菜单资源。此方法将限制每个文档模板的一个菜单。若要避开此限制,可以重写 CDocument::GetDefaultMenu(),并执行一些调用以显示菜单。

MFC 使用 CMDIChildWnd::m_hMenuShared 和 CFrameWnd::m_hMenuDefault 数据成员来确定要显示的菜单。在 MDI 应用程序,其中每个文档模板关联的菜单资源文档模板构造期间加载并复制到 CMDIChildWnd::m_hMenuShared。然后在 CMDIChildWnd::OnUpdateFrameMenu() 中使用此菜单时没有活动的 MDI 子窗口设置 MDI 框架窗口的菜单。CMDIChildWnd::OnUpdateFrameMenu() 使用 CFrameWnd::m_hMenuDefault,在没有活动子窗口时,使用 IDR_MAINFRAME 的 CFrameWnd 构造过程中加载。SDI 程序调用 CFrameWnd::OnUpdateFrameMenu(),CFrameWnd::m_hMenuDefault 用于其菜单。

在 SDI 和 MDI 应用程序中,如果需要显示的菜单从两个其他来源获取之前使用的 m_hMenuShared 或 m_hMenuDefault 菜单。该框架首先检查 CFrameWnd::m_hMenuAlt。这用于在受支持的 OLE 程序中使用就地激活的对象,提供其自己的菜单。它将检查第二个来源是 CDocument::GetDefaultMenu(),这也是虚拟。它的默认实现将返回 NULL。可以重写此函数允许程序以选择要显示的菜单。

手动更改菜单是通过调用 OnUpdateFrameMenu() 和 DrawMenuBar()。要设置到窗口中的新建菜单上,则必须调用 CFrameWnd::OnUpdateFrameMenu(NULL) 或 CMDIFrameWnd::OnUpdateFrameMenu(NULL)。调用 DrawMenuBar() 来重绘菜单。使用下面的代码为 MDI 和 SDI 实现:

在资源编辑器中创建新的菜单资源 (IDR_MYMENU1)。

添加到 CMyDocument 的 HMENU 的数据成员,重写 GetDefaultMenu() 返回该数据成员:

// .h file // HMENU m_hMyMenu; // virtual HMENU GetDefaultMenu(); // get menu depending on state HMENU CMyDocument::GetDefaultMenu() { return m_hMyMenu; // just use original default }

请记住,以初始化该成员变量,无论是在构造函数中的空值或 CDocument::OnNewDocument()。

更改和重绘菜单上,在合适的时间。例如,当拆分窗格之间进行切换,通常可以在 CView::OnActivateView() 中。下面的代码演示如何在该函数中实现它。

标签: <无>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐