c 自定義CMenuEx類

2021-10-20 12:18:11 字數 4904 閱讀 1824

#if !defined(afx_menuex_h__fe677f6b_2315_11d7_8869_bb2b2a4f4d45__included_)

#define afx_menuex_h__fe677f6b_2315_11d7_8869_bb2b2a4f4d45__included_

#include "afxtempl.h"

#if _msc_ver > 1000

#pragma once

#endif // _msc_ver > 1000

typedef struct tagmenuitem

; cimagelist *pimagelist;

cbitmap *pleftbitmap;

}menuitem,*lpmenuitem;

///class cmenuex : public cmenu

;#endif

// menuex.cpp: implementation of the cmenuex class.

////

#include "stdafx.h"

#include "menuex.h"

#ifdef _debug

#undef this_file

static char this_file=__file__;

#define new debug_new

#endif

//// construction/destruction

//implement_dynamic( cmenuex, cmenu )

cmenuex::cmenuex():m_szimage(16,15)

cmenuex::~cmenuex()

//釋放所有的選單項

for(i = 0; i < m_menuitemarr.getsize(); i++)

//銷毀選單

destroymenu();}/

//當選單項為不可用時繪製灰色的文字

void cmenuex::graystring(cdc *pdc, const cstring &str, const crect rect)

///繪製選單項位圖

void cmenuex::drawmenuitemimage(cdc *pdc, crect &rect, bool bselected, bool bchecked,

bool bgrayed, bool bhasimage,lpmenuitem lpitem)

else

rt.inflaterect(-2,-2);

//畫出"√"

pdc->setbkmode(transparent);

pdc->settextcolor(m_coltext);

pdc->drawtext("√",&rt,dt_expandtabs|dt_vcenter|dt_singleline);

} rect.left +=m_szimage.cx + 4 +2 ;

return ; }

if(bhasimage)

uint ustyle =ild_transparent; //cimagelist::draw()繪製點陣圖的風格

if(bgrayed)

else

}// pdc->fillsolidrect(rect.left + 2,rect.top + 2,m_szimage.cx,m_szimage.cy,0);

lpitem->pimagelist->draw(pdc,lpitem->uindex,pt,ustyle); //在選單項中繪製位圖

//調整可繪製矩形的大小

//4:位圖外接矩形比位圖大4

//2:選單文字與點陣圖外接矩形的間隔為2

+=m_szimage.cx + 4 + 2; }}

///繪製選單項文字

//引數:rect:立體矩形的rect

// rttext:選單文字的rect

void cmenuex::textmenu(cdc *pdc, crect rect,crect rctext,bool bselected, bool bgrayed, lpmenuitem lpitem)

if(bgrayed)

else }

void cmenuex::drawimageleft(cdc *pdc, crect &rect,lpmenuitem lpitem)

void cmenuex::drawitem(lpdrawitemstruct lpdis)

else

dc.setbkmode(opaque);

dc.detach();}//

//由工具欄的位圖來產生選單所用的點陣圖列表m_imagelist

int cmenuex::getimagefrom*******(uint u*******, c******* *p*******,colorref crmask/*工具欄點陣圖的掩碼*/,uint ubitmap)

} if(uid != id_separator)

ncount ++;

}/*for(int j = 0; j < m_menuitemarr.getsize(); j++) */

//計算出位圖的寬度

nwidth =bmpinfo.bmwidth/ncount;

bmp.deleteobject();

trace("menu bitmap--width:%d\theight:%d\n",nwidth,nheight);

//建立位圖列表

if(ubitmap)

else

m_szimage.cx =nwidth;

m_szimage.cy =nheight;

return ncount;

}void cmenuex::measureitem(lpmeasureitemstruct lpmis)

else

lpmis->itemheight =m_szimage.cy+4;

afxgetmainwnd()->releasedc(pdc); }}

void cmenuex::setimageleft(uint idbmpleft,bool bispopupmenu)

//重新計算選單項左邊點陣圖的位置

/* int menuitemcount = this->getmenuitemcount();

if(bispopupmenu)

else

}}*/

}void cmenuex::settextcolor(colorref crcolor)

}void cmenuex::setbackcolor(colorref crcolor)

}void cmenuex::sethighlightcolor(colorref crcolor)

}void cmenuex::load*******(uint u*******, uint uface)

getimagefrom*******(u*******,&m_wnd*******,rgb(192,192,192),0);

//initmenu(this,u*******,&m_wnd*******,uface);

}bool cmenuex::removemenuex(uint nposition, uint nflags)

else

if((int) uitemid >= 0) //普通選單項或分割條

}} else //子選單

return cmenu::removemenu(nposition,nflags);

}bool cmenuex::loadmenu(uint umenu)

else if((int)uid == -1) //彈出選單(即子選單)

else //正常的選單項

}menu.destroymenu(); //銷毀臨時選單

return true;

}void cmenuex::changemenustyle(cmenu *pmenu,hmenu hnewmenu)

else if(uid == -1) //彈出選單(即子選單)

else //正常的選單項

}}bool cmenuex::modifymenuex(uint nposition, uint nflags, uint nidnewitem, lpctstr lpsznewitem)

else

//普通選單項和彈出式選單

menuitem *pmenuitem = null;

for( i = 0; i < m_menuitemarr.getsize(); i++) }

//沒找到對應的選單項

if(i >= m_menuitemarr.getsize())

/* uint unewflags = 0;

if(nflags & mf_checked) unewflags |= mf_checked;

if(nflags & mf_unchecked) unewflags |= mf_unchecked;

if(nflags & mf_byposition) unewflags |= mf_byposition;

if(nflags & mf_bycommand) unewflags |= mf_bycommand;

unewflags |= mf_ownerdraw;*/

nflags &= ~mf_string;

nflags |= mf_ownerdraw;

return cmenu::modifymenu(nposition,nflags,nidnewitem,(lpctstr)pmenuitem);

}void cmenuex::calculateleftimagepos(cmenu *ppopupmenu)

} }}

C 自定義List類

如下 using system using system.collections.generic using system.linq using system.text namespace myarraylist public myarraylist int capacity 索引器 public ...

C 自定義類排序

1 宣告乙個類 人物類 public class person 2 宣告乙個繼承了介面icomparer的類 比較人物類例項大小,實現介面icomparer public class personcomparer icomparer return 0 3 main函式,建立乙個list,並使用剛建立...

c 自定義string類

1.標頭檔案部分 define crt secure no warnings pragma once include includeusing namespace std class mystring 2.函式定義部分 include mystring.h mystring mystring mys...