自己学习MFC总结的一些问题(二)

来源:互联网 发布:java set和get的用法 编辑:程序博客网 时间:2024/06/05 08:05

关于Detach()函数

//动态添加菜单栏

CMenu menu; //注意,此处CMenu的对象是局部的,在函数结束的时候会销毁,所以会导致程序出现一些问题,建议将此声明为类的成员变量。

     menu.LoadMenu(IDR_MAINFRAME);

SetMenu(&menu);

menu.Detach(); //如果还是想将CMenu的对象设置成函数的局部变量,可以调用此方法,从而消除程序原先会出现的bug

 

关于Detch函数,在MSDN中关于CMenu类中有这么一段话:

Create a CMenu object on the stack frame as a local(局部变量), then call CMenu’s member functions to manipulate the new menu as needed. Next, call CWnd::SetMenu to set the menu to a window, followed immediately by a call to the CMenu object’s Detach member function. The CWnd::SetMenu member function sets the window’s menu to the new menu, causes the window to be redrawn to reflect the menu change, and also passes ownership of the menu to the window. The call to Detach detaches(使分开, 使分离) the HMENU from the CMenu object, so that when the local CMenu variable passes out of scope, the CMenu object destructor does not attempt to destroy a menu it no longer owns

 

关于GetTextExtent()函数和GetTextMetrics()函数

GetTextMetrics:获取当前字体的信息
GetTextExtent:获取特定的字符串在屏幕上所占的宽度和高度

CDC::GetTextMetrics

作用:返回当前设备描述表中的当前所用的字体的信息(注意:是字体而不是某个字符如I或w的宽度是不一样的,但是它们的字体宽度有可能是一样的)。

CDC::GetTextExtent
调用这个函数,返回用当前字体写下的一行字符串的高度和宽度(注意:这里指是某个字符如I和w)。


0 0
原创粉丝点击