孫鑫VC 講座 筆記補充 六

2021-04-09 10:51:25 字數 2029 閱讀 3738

1、完成對乙個menu item 的標識:checkmenuitem()

// cmainframe::ontoggletestmenuitem() is a menu command handler for

// "test" menu item (whose resource id is id_help_test). it toggles

// the checked or unchecked state of the "test" menu item.

// cmainframe is a cframewnd-derived class.

void cmainframe::ontoggletestmenuitem()

{// get the popup menu which contains the "test" menu item.

cmenu* mmenu = getmenu();

cmenu* submenu = mmenu->getsubmenu(3);

// check the state of the "test" menu item. check the menu item

// if it is currently unchecked. otherwise, uncheck the menu item

// if it is not currently checked.

uint state = submenu->getmenustate(id_help_test, mf_bycommand);

assert(state != 0xffffffff);

if (state & mf_checked)

submenu->checkmenuitem(id_help_test, mf_unchecked | mf_bycommand);

else

submenu->checkmenuitem(id_help_test, mf_checked | mf_bycommand);

在此,我們用的是getmenustate()返回值為掩碼的形式(otherwise the return value is a mask (boolean or) of the values),然後把state它所包含的狀態和我們所關心的狀態作"&"操作,就可以看出我們所關心的狀態在不在。

2、新增自定義的選單

(1)、自定義一選單資源,或者採用資源編輯器,或者手動.

(2)、定義一cmenu物件,後呼叫loadmenu(id);

(3)、在框架類的oncreate()中,setmenu();

cwnd::setmenu():sets the current menu to the specified menu. causes the window to be redrawn to reflect  themenu change。

如果為null,則是移除當前選單。

3、加乙個小插曲啊:在vc中將程式排版 alt+f8

4、設定位圖標題選單:setmenuitembitmaps()

這個是msdn上的例子程式,在使用setmenuitembitmaps()之前你要自己首先建立兩個位圖,定義cbitmap物件與資源關聯,另外需注意的是你建立的點陣圖大小的問題,這你必須參考getsystemmetrics(),這個函式功能很豐富。

對與在析構函式中的操作,我是這樣理解的:當我們呼叫loadbitmap()是會把資源調入記憶體中,m_hobject 指向該資源,如果我們不把它選擇進入裝置描述表,我們就需要釋放在記憶體中的資源。

一下為msdn中的資料:

you can use the cgdiobject::deleteobject function to delete bitmap loaded by the loadbitmap function, or the cbitmap destructor will delete the object for you.

caution

before you delete the object, make sure it is not selected into a device context.

孫鑫VC 講座筆記 2 C

1,c語言中,結構體struct中不能包括函式的,而在c 中struct中可以包括函式。2,c 中結構體和類可以通用,區別主要表現在訪問控制方面 struct中預設是public,而 class中預設的是private。3,建構函式最重要的作用是建立物件的本身,c 中每個類可以擁有多個建構函式,但必...

孫鑫VC 講座筆記 2 C

1,c語言中,結構體struct中不能包括函式的,而在c 中struct中可以包括函式。2,c 中結構體和類可以通用,區別主要表現在訪問控制方面 struct中預設是public,而 class中預設的是private。3,建構函式最重要的作用是建立物件的本身,c 中每個類可以擁有多個建構函式,但必...

孫鑫VC 講座筆記 2 C

1,c語言中,結構體struct中不能包括函式的,而在c 中struct中可以包括函式。2,c 中結構體和類可以通用,區別主要表現在訪問控制方面 struct中預設是public,而 class中預設的是private。3,建構函式最重要的作用是建立物件的本身,c 中每個類可以擁有多個建構函式,但必...