Board logo

标题: [转帖]如何创建一个不规则形状的窗口 [打印本页]

作者: cn2002    时间: 2003-9-15 04:12     标题: [转帖]如何创建一个不规则形状的窗口

可以使用新的SDK函数SetWindowRgn。该函数将绘画和鼠标消息限定在窗口的一
个指定的区域,实际上使窗口成为指定的不规则形状。
使用AppWizard创建一个基于对的应用程序并使用资源编辑器从主对话资源中删
除所在的缺省控件、标题以及边界。
给对话类增加一个CRgn数据成员,以后要使用该数据成员建立窗口区域。
  1. Class CRoundDlg : public CDialog
  2. {
  3. …
  4. private :
  5. Crgn m_rgn : // window region
  6. …
  7. } ;
  8. 修改OnInitDialog函数建立一个椭圆区域并调用SetWindowRgn将该区域分配给
  9. 窗口:
  10. BOOL CRoundDlg : : OnInitDialog ( )
  11. {
  12. CDialog : : OnInitDialog ( ) ;
  13.  
  14. //Get size of dialog .
  15. CRect rcDialog ;
  16. GetClientRect (rcDialog );
  17.  
  18. // Create region and assign to window .
  19. m_rgn . CreateEllipticRgn (0 , 0 , rcDialog.Width ( ) , rcDialog.Height (
  20. ) );
  21. SetWindowRgn (GetSafeHwnd ( ) , (HRGN) m_ rgn , TRUE );
  22.  
  23. return TRUE ;
  24. }
  25. 通过建立区域和调用SetWindowRgn,已经建立一个不规则形状的窗口,下面的例
  26. 子程序是修改OnPaint函数使窗口形状看起来象一个球形体。
  27. void CRoundDlg : : OnPaint ( )
  28. {
  29. CPaintDC de (this) ; // device context for painting .
  30. //draw ellipse with out any border
  31. dc. SelecStockObject (NULL_PEN);
  32.  
  33. //get the RGB colour components of the sphere color
  34. COLORREF color= RGB( 0 , 0 , 255);
  35. BYTE byRed =GetRValue (color);
  36. BYTE byGreen = GetGValue (color);
  37. BYTE byBlue = GetBValue (color);
  38.  
  39. // get the size of the view window
  40. Crect rect ;
  41. GetClientRect (rect);
  42.  
  43. // get minimun number of units
  44. int nUnits =min (rect.right , rect.bottom );
  45.  
  46. //calculate he horiaontal and vertical step size
  47. float fltStepHorz = (float) rect.right /nUnits ;
  48. float fltStepVert = (float) rect.bottom /nUnits ;
  49.  
  50. int nEllipse = nUnits/3; // calculate how many to draw
  51. int nIndex ; // current ellipse that is being draw
  52.  
  53. CBrush brush ; // bursh used for ellipse fill color
  54. CBrush *pBrushOld; // previous brush that was selected into dc
  55.  
  56. //draw ellipse , gradually moving towards upper-right corner
  57. for (nIndex = 0 ; nIndes < + nEllipse ; nIndes ++)
  58. {
  59. //creat solid brush
  60. brush . CreatSolidBrush (RGB ( ( (nIndex *byRed ) /nEllipse ).
  61. ( ( nIndex * byGreen ) /nEllipse ), ( (nIndex * byBlue) /nEllipse ) ) );
  62.  
  63. //select brush into dc
  64. pBrushOld= dc .SelectObject (&brhsh);
  65.  
  66. //draw ellipse
  67. dc .Ellipse ( (int) fltStepHorz * 2, (int) fltStepVert * nIndex ,
  68. rect. right -( (int) fltStepHorz * nIndex )+ 1,
  69. rect . bottom -( (int) fltStepVert * (nIndex *2) ) +1) ;
  70.  
  71. //delete the brush
  72. brush.DelecteObject ( );
  73. }
  74. }
  75.  
  76. 最后,处理WM_NCHITTEST消息,使当击打窗口的任何位置时能移动窗口。
  77. UINT CRoundDlg : : OnNchitTest (Cpoint point )
  78. {
  79. //Let user move window by clickign anywhere on the window .
  80. UINT nHitTest = CDialog : : OnNcHitTest (point) ;
  81. rerurn (nHitTest = = HTCLIENT)? HTCAPTION: nHitTest ;
  82. }
复制代码





欢迎光临 星星博客 (http://bbs.huhoo.net/) Powered by Discuz! 7.0.0