|
楼主 |
发表于 2005-11-23 15:52:46
|
显示全部楼层
<><STRONG>如何使用SetWindowRgn函数创建异形窗口</STRONG></P>
<>
<TABLE cellSpacing=0 cellPadding=1 width="100%" align=center border=0>
<TBODY>
<TR>
<TD bgColor=#febf01>
<TABLE cellSpacing=0 cellPadding=3 width="100%" align=center border=0>
<TBODY>
<TR>
<TD width="60%" bgColor=#eeeeee>时间:2000/10/11 21:06</TD>
<TD width="40%" bgColor=#eeeeee><A href="http://www.etechnic.com.cn/" target="_blank" >eNet技术</A></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR></P>
<> 很多的游戏和工具软件都使用异形窗口来出奇至胜。其实创建异形窗口从技术上来说难度不大,但是有没有效果主要还是在于创意是否出奇。下面简单的介绍如何使用SetWindowRgn制作异形窗口。 </P>
<></P>
<> SetWindowRgn是新的SDK函数。该函数将绘画消息和鼠标消息都限定在窗口的一个指定的区域内,实际上使窗口成为指定的不规则形状。 </P>
<></P>
<> 首先使用AppWizard创建一个基于对话框的应用程序,并使用资源编辑器从资源中删除有的缺省控件、标题以及边界。 </P>
<></P>
<> 给对话类增加一个CRgn数据成员,以便使用该数据成员建立窗口区域。 </P>
<></P>
<P>Class CTestDlg:public CDialog </P>
<P>{ … </P>
<P>private: </P>
<P> CRgn m_rgn; // window region </P>
<P>…} </P>
<P></P>
<P> 修改对话框的OnInitDialog函数,建立一个椭圆区域并调用SetWindowRgn将该区域分配给窗口: </P>
<P></P>
<P>BOOL CTestDlg::OnInitDialog () </P>
<P>{ </P>
<P></P>
<P>CDialog::OnInitDialog () </P>
<P></P>
<P>//得到对话框的尺寸 </P>
<P></P>
<P>CRect rcDialog; </P>
<P>GetClientRect (rcDialog) </P>
<P></P>
<P>//创建一个椭圆形的区域,并使用SetWindowRgn重设对话框的形状。 </P>
<P></P>
<P>m_rgn.CreateEllipticRgn (0 , 0 , rcDialog.Width(), </P>
<P>rcDialog.Height ()); SetWindowRgn (GetSafeHwnd () , (HRGN) m_ rgn,TRUE ); </P>
<P></P>
<P>return TRUE; </P>
<P>} </P>
<P></P>
<P> 一个简单的异形窗口已经生成,如果编译运行工程,将出现一个椭圆形的窗口。下面的代码是在OnPaint消息中进行处理,使得对话框具有立体效果。 </P>
<P></P>
<P>void CTestDlg::OnPaint () </P>
<P>{ </P>
<P> CPaintDC paintDC (this); </P>
<P> // 得到设备 // 设置画椭圆时不画边界 </P>
<P> paintDC. SelecStockObject (NULL_PEN); </P>
<P> // 得到球体的颜色 </P>
<P> COLORREF ballColor= RGB( 0 , 0 , 255); </P>
<P> BYTE byRed =GetRValue (ballColor); </P>
<P> BYTE byGreen = GetGValue (ballColor) ; </P>
<P> BYTE byBlue = GetBValue (ballColor) ; </P>
<P> // 得到球体的范围。 </P>
<P> Rect rcDialog; </P>
<P> GetClientRect (rcDialog) ; </P>
<P> // 得出画球体的最小单位 </P>
<P> int nUnits =min (rcDialog.right , rcDialog.bottom ) ; </P>
<P> //计算水平方向和垂直方向的画线单位。 </P>
<P> float fltStepHorz = (float) rcDialog.right /nUnits ; </P>
<P> float fltStepVert = (float) rcDialog.bottom /nUnits ; </P>
<P> int nEllipse = nUnits/3 ; </P>
<P> // 开始画出多个椭圆,以达到视觉上的立体感。 </P>
<P> CBrush brush ; </P>
<P> // 使用ballColor的颜色填充画刷。 </P>
<P> CBrush *pBrushOld; </P>
<P> // 存储旧画刷 </P>
<P></P>
<P> for (nIndex = 0 ;nIndes < nEllipse ;nIndes++) </P>
<P> { </P>
<P> CreatSolidBrush (RGB ( ( (nIndex*byRed ) /nEllipse ); </P>
<P></P>
<P> ( ( nIndex * byGreen ) /nEllipse ), ( (nIndex*byBlue)/nEllipse ) ) ) ; </P>
<P> pBrushOld= paintDC .SelectObject (&brhsh) ; </P>
<P> paintDC .Ellipse ( (int) fltStepHorz * 2, (int)fltStepVert * nIndex , </P>
<P> rect. right -( (int)fltStepHorz * nIndex )+ 1, rect . bottom -( (int) </P>
<P> fltStepVert * (nIndex *2) ) +1) ; </P>
<P> brush.DelecteObject ( ) ; </P>
<P> } </P>
<P>} </P>
<P></P>
<P> 异形窗口不是只适用于对话框,同样的适用于单文档,多文档类型的应用。如果读者有兴趣,可以自己进行尝试。如有疑问可发Mail至学院信箱。 </P>
<P> </P>
<P><BR> </P> |
|