两个实例了解VB.NET特殊形状窗体

开发 后端
这里介绍们的目的是实现VB.NET特殊形状窗体,VB6中实现做一个古怪的窗口必须要用的也是此程序中最重要的一个函数就是SetWindowRgn它的功能就是对指定的窗口进行重画,把这个窗口你选择的部分留下其余的部分抹掉。

#t#本人很喜欢VB.NET,在工作中也很喜欢总结关于VB.NET特殊形状窗体的经验教训,下面就这个问题来详细说说吧。我们的目的是实现VB.NET特殊形状窗体,VB6中实现(借助API函数)做一个古怪的窗口必须要用的也是此程序中最重要的一个函数就是SetWindowRgn它的功能就是对指定的窗口进行重画,把这个窗口你选择的部分留下其余的部分抹掉。

VB.NET特殊形状窗体参数:
◆hWnd:你所要重画的窗口的句柄,比如你想重画form1则应该让此参数为form1.hWnd
◆hRgn:你要保留的区域的句柄,这个句柄是关键,你需要通过别的渠道来获得在这里的区域是由Combinergn合成的新区域
◆bRedram:是否要马上重画,一般设为true
◆函数CombineRgn将两个区域组合为一个新区域
◆函数Createrectrgn为创建一个由点X1,Y1和X2,Y2描述的矩形区域
◆函数CreateEllipticRgn为创建一个X1,Y1和X2,Y2的椭圆区域用DeleteObject这个函数可删除GDI对象,比如画笔、刷子、字体、位图、区域以及调色板等等。对象使用的所有系统资源都会被释放。

以下是VB6的代码:

  1. PrivateDeclareFunction CreateEllipticRgn Lib "gdi32" (ByVal X1 AsLong, 
    ByVal Y1 AsLong, ByVal X2 AsLong, ByVal Y2 AsLong) AsLong  
  2. PrivateDeclareFunction CreateRectRgn Lib "gdi32" (ByVal X1 AsLong, 
    ByVal Y1 AsLong, ByVal X2 AsLong, ByVal Y2 AsLong) AsLong  
  3. PrivateDeclareFunction CombineRgn Lib "gdi32" (ByVal hDestRgn AsLong, 
    ByVal hSrcRgn1 AsLong, ByVal hSrcRgn2 AsLong, ByVal nCombineMode AsLong) AsLong  
  4. PrivateDeclareFunction SetWindowRgn Lib "user32" (ByVal hWnd AsLong, 
    ByVal hRgn AsLong, ByVal bRedraw AsBoolean) AsLong  
  5. PrivateDeclareFunction DeleteObject Lib "gdi32" (ByVal hObject AsLong) AsLong  
  6. PrivateConst RGN_DIFF = 4 
  7.  
  8. PrivateSub Form_Load()  
  9. Dim rgn AsLong  
  10. Dim rgnRect AsLong  
  11. Dim rgnDest AsLong  
  12.  
  13. rgn = CreateEllipticRgn(0, 0, Me.Width / Screen.TwipsPerPixelX, Me.Height / Screen.TwipsPerPixelY)  
  14. rgnRect = CreateRectRgn((Me.Width / Screen.TwipsPerPixelX - 20) / 2, 
    (Me.Height / Screen.TwipsPerPixelY - 20) / 2, (Me.Width / Screen.TwipsPerPixelX + 20) / 2, 
    (Me.Height / Screen.TwipsPerPixelY + 20) / 2)  
  15. rgnDest = CreateRectRgn(0, 0, 1, 1)  
  16. CombineRgn rgnDest, rgn, rgnRect, RGN_DIFF  
  17. SetWindowRgn Me.hWnd, rgnDest, True  
  18. Call DeleteObject(rgnRect)  
  19. Call DeleteObject(rgnDest)  
  20. EndSub  
  21.  
  22. PrivateSub Command1_Click()  
  23. End  
  24. EndSub 

在VB.NET中,我们可以使用.NET 框架类库System.Drawing.Drawing2D的GraphicsPath 类(应用程序使用路径来绘制形状的轮廓、填充形状内部和创建剪辑区域),来绘制图形,然后通过VB.NET特殊形状窗体的Me.Region来设置窗口的可见区域。

以下是VB.NET的代码:

  1. '声明一个布尔型变量,判断窗体是否正常区域  
  2. Dim IsNormalRegion AsBoolean = True   
  3. PrivateSub Button2_Click(ByVal sender As System.Object, 
    _ByVal e As System.EventArgs) Handles Button2.Click  
  4. If (IsNormalRegion) Then  
  5. '构造一个GraphicsPath对象实例  
  6. Dim Graphics AsNew System.Drawing.Drawing2D.GraphicsPath()  
  7. Dim intHeight AsInteger = Me.Size.Height  
  8. Dim intWidth AsInteger = Me.Size.Width  
  9. '定义内矩形的左上角坐标  
  10. Dim RectTop AsInteger = 100 
  11. '在窗体上绘制一个大椭圆,左上角的坐标取为(0,0)  
  12. Graphics.AddEllipse(0, 0, intWidth, intHeight)  
  13. '再绘制一个小矩形  
  14. Dim AddRect AsNew Rectangle(RectTop, RectTop, 
    intHeight - (RectTop * 2), intHeight - (RectTop * 2))  
  15. Graphics.AddRectangle(AddRect)  
  16. '设置窗口的可见区域  
  17. Me.Region = New Region(Graphics)  
  18. Else  
  19. Me.Region = Nothing 
  20. EndIf  
  21. IsNormalRegion = Not IsNormalRegion  
  22. EndSub 
责任编辑:佚名 来源: 博客
相关推荐

2010-01-11 15:12:30

VB.NET特殊窗体

2009-11-03 17:31:01

VB.NET窗体

2010-01-11 15:31:04

VB.NET拖动窗体

2009-04-30 13:24:45

VB.NET 2008窗体应用实例

2009-10-16 09:35:24

VB.NET制作透明窗

2009-10-14 13:56:05

VB.NET数据窗体

2010-01-13 09:31:39

VB.NET窗体打印

2009-10-14 15:20:21

VB.NET窗体指针

2009-10-14 15:34:29

VB.NET窗体编程模

2009-10-09 16:54:03

VB.NET窗体

2010-01-20 17:54:13

VB.NET特殊字符

2009-10-26 17:53:50

VB.NET新窗体

2010-01-13 15:52:59

VB.NET浮动窗体

2009-11-10 15:07:11

VB.NET窗体

2009-10-14 14:50:16

VB6.0VB.NET

2009-10-28 13:23:52

VB.NET可选参数

2009-10-29 14:04:48

VB.NET Deri

2009-10-30 13:31:06

VB.NET名空间

2009-11-02 10:42:04

VB.NET EXCE

2010-01-07 16:51:56

VB.NET窗体钩子
点赞
收藏

51CTO技术栈公众号