我如何在vb.net中更改表单形状 [英] how can i change form shape in vb.net

查看:110
本文介绍了我如何在vb.net中更改表单形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾经使用以下代码在VB 6.0中更改表单形状:

I used to change the Form shape in VB 6.0 using the following code:

Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long

Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Private Sub MakeRoundObject(objObject As Object, Value As Long)
  Static lngHeight, lngLong, lngReturn, lngWidth As Long
  lngWidth = objObject.Width / Screen.TwipsPerPixelX
  lngHeight = objObject.Height / Screen.TwipsPerPixelY
  SetWindowRgn objObject.hWnd, CreateRoundRectRgn(10, 50, lngWidth, lngHeight, Value + 10, Value), True
End Sub

Private Sub Form_Load()
   Call MakeRoundObject(Form1, 50)
End Sub

以相同的方式使用VB.NET代码,如下所示:

In the same way I used VB.NET code as follows:

Imports Microsoft.VisualBasic.Compatibility

Public Class Form1
  Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Integer, ByVal Y1 As Integer, ByVal X2 As Integer, ByVal Y2 As Integer, ByVal X3 As Integer, ByVal Y3 As Integer) As Integer
  Private Declare Function ReleaseCapture Lib "user32" () As Integer
  Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Integer, ByVal hRgn As Integer, ByVal bRedraw As Boolean) As Integer

  Private Sub MakeRoundObject(ByRef objObject As Object, ByRef Value As Integer)
    Static lngLong, lngHeight, lngReturn As Object
    Static lngWidth As Integer
    lngWidth = objObject.Width / VB6.TwipsPerPixelX
    lngHeight = objObject.Height / VB6.TwipsPerPixelY
    SetWindowRgn(objObject.hWnd, CreateRoundRectRgn(0, 0, lngWidth, lngHeight, Value, Value), True)

  End Sub

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    MakeRoundObject(Me, 20)
  End Sub

End Class

但是在后一种情况下,我收到一条错误消息-找不到类型'Form1'上的公共成员'hWnd'."

But in the latter case, I receive an error message - "Public member 'hWnd' on type 'Form1' not found."

我做什么?

推荐答案

您不必使用P/Invoke来执行此操作. Form.Region属性是为此目的而设计的.一个简单的例子:

You don't have to use P/Invoke to do this. The Form.Region property was designed for this. A simple example:

Public Class Form1
  Public Sub New()
    InitializeComponent()
    Dim path As New Drawing2D.GraphicsPath()
    path.AddEllipse(0, 0, Me.Width, Me.Height)
    Me.Region = New Region(path)
  End Sub
End Class

这篇关于我如何在vb.net中更改表单形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆