VB.net平滑非矩形形状的边缘 [英] VB.net Smooth out Non-rectangular Form edges

查看:94
本文介绍了VB.net平滑非矩形形状的边缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在VB.net中平滑非矩形形状的边缘.例如在我的通告中
边缘呈锯齿状.如何使它们平滑?


我尝试了2种方法. 1使用透明键,即,将表单背景色设置为与透明键相同的颜色.第二种方法使用区域,下面是代码:

How do I smooth out non-rectangular form edges in VB.net. For example in my circular
form the edges are jagged. How do I smooth them out?


I have tried 2 methods. 1 is using the transparency key, that is, setting the form background colour with the same colour as that of the transparency key. The second method uses the regions, here is the code:

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim gpath As New Drawing2D.GraphicsPath 'gpath
gpath.AddEllipse(New Rectangle(1, 1, 137, 136))
Me.Region = New Region(gpath)
gpath.Dispose()
End Sub

推荐答案

这里是只要表单没有边框就可以使表单边缘变圆的代码,我还使其可拖动,因此它是可缩放的圆形边缘形式:

Here''s a code that can round form edges as long as the form doesn''t have a border, I also made it draggable so that it is a round edged, draggable form:

Public Class Form1

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Height = 300
        Me.Width = 400
        Dim p As New Drawing2D.GraphicsPath()
        p.StartFigure()
        p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
        p.AddLine(40, 0, Me.Width - 40, 0)
        p.AddArc(New Rectangle(Me.Width - 40, 0, 40, 40), -90, 90)
        p.AddLine(Me.Width, 40, Me.Width, Me.Height - 40)
        p.AddArc(New Rectangle(Me.Width - 40, Me.Height - 40, 40, 40), 0, 90)
        p.AddLine(Me.Width - 40, Me.Height, 40, Me.Height)
        p.AddArc(New Rectangle(0, Me.Height - 40, 40, 40), 90, 90)
        p.CloseFigure()
        Me.Region = New Region(p)
        Me.BackColor = Color.Black

    End Sub
    Dim drag As Boolean
    Dim mousex As Integer
    Dim mousey As Integer

    Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
        drag = True 'Sets the variable drag to true.
        mousex = Windows.Forms.Cursor.Position.X - Me.Left 'Sets variable mousex
        mousey = Windows.Forms.Cursor.Position.Y - Me.Top 'Sets variable mousey
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Height = 300
        Me.Width = 400
        Dim p As New Drawing2D.GraphicsPath()
        p.StartFigure()
        p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
        p.AddLine(40, 0, Me.Width - 40, 0)
        p.AddArc(New Rectangle(Me.Width - 40, 0, 40, 40), -90, 90)
        p.AddLine(Me.Width, 40, Me.Width, Me.Height - 40)
        p.AddArc(New Rectangle(Me.Width - 40, Me.Height - 40, 40, 40), 0, 90)
        p.AddLine(Me.Width - 40, Me.Height, 40, Me.Height)
        p.AddArc(New Rectangle(0, Me.Height - 40, 40, 40), 90, 90)
        p.CloseFigure()
        Me.Region = New Region(p)
        Me.BackColor = Color.Black
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'If drag is set to true then move the form accordingly.
        If drag Then
            Me.Top = Windows.Forms.Cursor.Position.Y - mousey
            Me.Left = Windows.Forms.Cursor.Position.X - mousex
            Me.FormBorderStyle = FormBorderStyle.None
            Me.Height = 300
            Me.Width = 400
            Dim p As New Drawing2D.GraphicsPath()
            p.StartFigure()
            p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
            p.AddLine(40, 0, Me.Width - 40, 0)
            p.AddArc(New Rectangle(Me.Width - 40, 0, 40, 40), -90, 90)
            p.AddLine(Me.Width, 40, Me.Width, Me.Height - 40)
            p.AddArc(New Rectangle(Me.Width - 40, Me.Height - 40, 40, 40), 0, 90)
            p.AddLine(Me.Width - 40, Me.Height, 40, Me.Height)
            p.AddArc(New Rectangle(0, Me.Height - 40, 40, 40), 90, 90)
            p.CloseFigure()
            Me.Region = New Region(p)
            Me.BackColor = Color.Black
        End If
    End Sub

    Private Sub Form1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
        drag = False 'Sets drag to false, so the form does not move according to the code in MouseMove
        Me.FormBorderStyle = FormBorderStyle.None
        Me.Height = 300
        Me.Width = 400
        Dim p As New Drawing2D.GraphicsPath()
        p.StartFigure()
        p.AddArc(New Rectangle(0, 0, 40, 40), 180, 90)
        p.AddLine(40, 0, Me.Width - 40, 0)
        p.AddArc(New Rectangle(Me.Width - 40, 0, 40, 40), -90, 90)
        p.AddLine(Me.Width, 40, Me.Width, Me.Height - 40)
        p.AddArc(New Rectangle(Me.Width - 40, Me.Height - 40, 40, 40), 0, 90)
        p.AddLine(Me.Width - 40, Me.Height, 40, Me.Height)
        p.AddArc(New Rectangle(0, Me.Height - 40, 40, 40), 90, 90)
        p.CloseFigure()
        Me.Region = New Region(p)
        Me.BackColor = Color.Black
    End Sub
End Class



希望对您有所帮助



Hope this helps


APIHelp.Size newSize =新的APIHelp.Size(bitmap.Width,bitmap.Height); //调整窗口大小以匹配位图
APIHelp.Point sourceLocation =新的APIHelp.Point(0,0);
APIHelp.Point newLocation =新的APIHelp.Point(this.Left,this.Top);
APIHelp.BLENDFUNCTION blend =新的APIHelp.BLENDFUNCTION();
blend.BlendOp = APIHelp.AC_SRC_OVER;
blend.BlendFlags = 0//始终为0
blend.SourceConstantAlpha = 255;//设置为255
blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;
APIHelp.Size newSize = new APIHelp.Size(bitmap.Width, bitmap.Height); // Size window to match bitmap
APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
APIHelp.Point newLocation = new APIHelp.Point(this.Left, this.Top);
APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION();
blend.BlendOp = APIHelp.AC_SRC_OVER;
blend.BlendFlags = 0 // Always 0
blend.SourceConstantAlpha=255;// Set to 255
blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;


这篇关于VB.net平滑非矩形形状的边缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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