請(qǐng)問如何做一個(gè)三角形的CStatic?

熱心網(wǎng)友

做各種花樣的控件,其實(shí)都是一個(gè)原理,用SelectClipRgn裁減控件區(qū)域即可。如果你要做成三角形,那么先將三角形區(qū)域生成CRgn,然后在OnPaint中調(diào)用SelectClipRgn(pRgn)就可以了。 --------------------------------------------------------------- 如何創(chuàng)建一個(gè)不規(guī)則形狀的窗口 可以使用新的SDK 函數(shù)SetWindowRgn。該函數(shù)將繪畫和鼠標(biāo)消息限定在窗口的一個(gè)指定 的區(qū)域,實(shí)際上使窗口成為指定的不規(guī)則形狀。 使用AppWizard創(chuàng)建一個(gè)基于對(duì)的應(yīng)用程序并使用資源編輯器從主對(duì)話資源中刪 除所在的缺省控件、標(biāo)題以及邊界。給對(duì)話類增加一個(gè)CRgn 數(shù)據(jù)成員,以后要使用該 數(shù)據(jù)成員建立窗口區(qū)域。 Class CRoundDlg : public CDialog { … private : Crgn m_rgn : // window region … } ; 修改OnInitDialog函數(shù)建立一個(gè)橢圓區(qū)域并調(diào)用SetWindowRgn 將該區(qū)域分配給窗口: BOOL CRoundDlg : : OnInitDialog ( ) { CDialog : : OnInitDialog ( ) ; //Get size of dialog 。 CRect rcDialog ; GetClientRect (rcDialog ); // Create region and assign to window 。 m_rgn 。 CreateEllipticRgn (0 , 0 , rcDialog。Width ( ) , rcDialog 。Height ( ) ); SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn , TRUE ); return TRUE ; } 通過建立區(qū)域和調(diào)用SetWindowRgn,已經(jīng)建立一個(gè)不規(guī)則形狀的窗口,下面的例子程序 是修改OnPaint函數(shù)使窗口形狀看起來象一個(gè)球形體。 voik CRoundDlg : : OnPaint ( ) { CPaintDC de (this) ; // device context for painting 。 //draw ellipse with out any border dc。 SelecStockObject (NULL_PEN); //get the RGB colour components of the sphere color COLORREF color= RGB( 0 , 0 , 255); BYTE byRed =GetRValue (color); BYTE byGreen = GetGValue (color); BYTE byBlue = GetBValue (color); // get the size of the view window Crect rect ; GetClientRect (rect); // get minimun number of units int nUnits =min (rect。right , ttom ); //calculate he horiaontal and vertical step size float fltStepHorz = (float) rect。right /nUnits ; float fltStepVert = (float) ttom /nUnits ; int nEllipse = nUnits/3; // calculate how many to draw int nIndex ; // current ellipse that is being draw CBrush brush ; // bursh used for ellipse fill color CBrush *pBrushOld; // previous brush that was selected into dc //draw ellipse , gradually moving towards upper-right corner for (nIndex = 0 ; nIndes < + nEllipse ; nIndes ++) { //creat solid brush brush 。 CreatSolidBrush (RGB ( ( (nIndex *byRed ) /nEllipse ) , ( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue) /nEllipse ) ) ); //select brush into dc pBrushOld= dc 。SelectObject (&brhsh); //draw ellipse dc 。Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex , rect。 right -( (int) fltStepHorz * nIndex )+ 1, ttom-((int)fltStepVert*(nIndex*2))+1);//delete the brush brush。DelecteObject ( ); } } 最后,處理WM_NCHITTEST消息,使當(dāng)擊打窗口的任何位置時(shí)能移動(dòng)窗口。 UINT CRoundDlg : : OnNchitTest (Cpoint point ) { //Let user move window by clickign anywhere on the window 。 UINT nHitTest = CDialog : : OnNcHitTest (point) ; rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest ; 。