如何創建一個不規則形狀的窗口
發表于:2007-07-14來源:作者:點擊數:
標簽:
可以使用新的SDK函數SetWindowRgn。該函數將繪畫和鼠標消息限定在窗口的一個指定的區域,實際上使窗口成為指定的不規則形狀。 使用AppWizard創建一個基于對話框的應用程序并使用資源編輯器從主對話資源中刪除有的缺省控件、標題以及邊界。 給對話類增加一個C
可以使用新的SDK函數SetWindowRgn。該函數將繪畫和鼠標消息限定在窗口的一個指定的區域,實際上使窗口成為指定的不規則形狀。
使用AppWizard創建一個基于對話框的應用程序并使用資源編輯器從主對話資源中刪除有的缺省控件、標題以及邊界。
給對話類增加一個CRgn數據成員,以后要使用該數據成員建立窗口區域。
Class CRoundDlg : public CDialog
{ …
private :
Crgn m_rgn : // window region
…}
修改OnInitDialog函數建立一個橢圓區域并調用SetWindowRgn將該區域分配給窗口:
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
}
通過建立區域和調用SetWindowRgn,已經建立一個不規則形狀的窗口,下面的例子程序是修改OnPaint函數使窗口形狀看起來象一個球形體。
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 , rect.bottom )
//calculate he horiaontal and vertical step size
float fltStepHorz = (float) rect.right /nUnits
float fltStepVert = (float) rect.bottom /nUnits
int nEllipse = nUnits/3 // calculate how many todraw 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-rightcorner
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, rect . bottom -( (int)
fltStepVert * (nIndex *2) ) +1)
//delete the brush
brush.DelecteObject ( )
} }
最后,處理WM_NCHITTEST消息,使當擊打窗口的任何位置時能移動窗口。
UINT CRoundDlg : : OnNchitTest (Cpoint point )
{
//Let user move window by clickign anywhere on thewindow .
UINT nHitTest = CDialog : : OnNcHitTest (point)
rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest
}
原文轉自:http://www.anti-gravitydesign.com