MFC設定字型及顏色

2021-05-23 11:13:00 字數 2448 閱讀 9859

設定字型

函式原型:

bool createfont( int nheight, int nwidth, int nescapement, int norientation, int nweight, byte bitalic, byte bunderline, byte cstrikeout, byte ncharset, byte noutprecision, byte nclipprecision, byte nquality, byte npitchandfamily, lpctstr lpszfacename );

引數說明:

nheight            :字型高度. 三中情況 1、>0:字型的高度值(裝置座標);2、=0:字型採用預設值. 3、<0:此值的絕對值為高度.

nwidth             :字型寬度.

nescapement        :文字行的傾斜度.

norientation       :字元基線的傾斜度.

nweight            :字型的粗細.如下:

.fw_dontcare

.fw_thin

.fw_extralight

.....

bitalic            :字型是否為斜體

bunderline         :字型是否帶下劃線

cstrikeout         :字型是否帶刪除線

ncharset           :字型的字符集

.ansi_charset

.default_charset

.symbol_charset.....

noutprecision      :字元的輸出精度

nclipprecision     :字元裁剪的精度

nquality           :字元的輸出質量

npitchandfamily :字元間距和字型族(低位說明間距,高位說明字元族)

lpszfacename       :字型名稱

[程式實現]

假設你已有了名為my的對話方塊工程.並有乙個id=idc_edit1的edit控制項.

class cmydlg : public cdialog

;bool ctmydlg::oninitdialog()

小**明:在oninitdialog()中的//cfont m_font;前的"//"號去掉,將類宣告中的cfont m_font;去掉會是什麼結果?請自己試試.

改變edit字型顏色!

hbrush cbuttondlg::onctlcolor(cdc* pdc, cwnd* pwnd, uint nctlcolor)

}// todo: return a different brush if the default is not desired

return hbr;

}cbrush      m_brushedit;  

m_brushedit.createsolidbrush      (rgb      (      255,      255,      0      )      );  

在dlg::onctlcolor函式中加入:  

if(nctlcolor      ==      ctlcolor_edit)  

改變對話方塊背景

cbitmap m_bkgndbmp;

m_bkgndbmp.loadbitmap(idb_bitmap1);

bool cbuttondlg::onerasebkgnd(cdc* pdc)

在乙個mfc應用程式中,要改變控制項的背景色可通過過載onctlcolor()函式來實現。方法是在該函式中設定所需顏色後再返回乙個畫刷控制代碼便可重繪控制項背景色。onctlcolor()函式對於控制項背景色的處理是通過捕捉相應的控制項訊息來實現的。常用的此類訊息有:

ctlcolor_dlg 對話方塊

ctlcolor_edit 編輯框

ctlcolor_listbox 列表框

ctlcolor_msgbox 訊息框

ctlcolor_scrollbar 滑動條

ctlcolor_static 靜態文字框、矩形等。

以下示例**說明如何更改以上控制項的背景色:

afx_msg hbrush onctlcolor(cdc* pdc, cwnd* pwnd, uint nctlcolor); //過載方法原形宣告 *.h

afx_msg bool onerasebkgnd(cdc* pdc);//對話方塊背景

......

begin_message_map(cbuttondlg, cdialog)

on_wm_ctlcolor() //建立訊息對映 *.cpp

on_wm_erasebkgnd() //設定對話方塊背景

end_message_map()

在程式中可以直接設定如下字型:黑體、宋體、仿宋—gb2312、楷體—gb2312、隸書、幼圓

MFC設定字型及顏色

設定字型 函式原型 bool createfont int nheight,int nwidth,int nescapement,int norientation,int nweight,byte bitalic,byte bunderline,byte cstrikeout,byte nchars...

MFC設定字型及顏色

mfc設定字型及顏色設定字型 函式原型 bool createfont int nheight,int nwidth,int nescapement,int norientation,int nweight,byte bitalic,byte bunderline,byte cstrikeout,b...

MFC設定字型及顏色

設定字型 函式原型 bool createfont int nheight,int nwidth,int nescapement,int norientation,int nweight,byte bitalic,byte bunderline,byte cstrikeout,byte nchars...