返回列表 回复 发帖

[转帖]如何创建一个不规则形状的窗口

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