如何将添加在图片框中的用户控件翻转 180 度? [英] How to flip a User Control added in a picturebox by 180 degrees?

查看:52
本文介绍了如何将添加在图片框中的用户控件翻转 180 度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在向图片框添加控件.这个控件是一个组件,准确地说它是一个切换开关

至于您问题的第二部分,请阅读CheckBox.Checked 属性和 CheckBox.CheckedChanged 事件.

实现示例:

Private Sub ToggleSwitch1_CheckedChanged(sender As Object, e As EventArgs) 处理 ToggleSwitch1.CheckedChanged如果 ToggleSwitch1.Checked 那么'待办事项处于 ON 状态...别的'待办事项处于关闭状态..万一结束子

I'm adding a control to a picturebox. This control is a component, precisely it's a toggle switch posted here. I'd like this control to be vertical and not horizontal .

So, since objects can't be rotated or flipped, I found a way to flip the picturebox image with:

PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
    PictureBox1.Refresh()

The error I'm getting at RunTime is:

System.Windows.Forms.PictureBox.Image.get returned Nothing

Endeed the control is not an image so is there a way to flip the control inside of the picturebox by 180 degrees? Also, You think there is a way to know when the value of the toggle switch is on or off? Thanks

解决方案

As already mentioned above, extend the control to have it drawn either horizontally or vertically.

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class ToggleSwitch
    Inherits CheckBox

    Public Sub New()
        MyBase.New

        SetStyle(ControlStyles.AllPaintingInWmPaint Or
                 ControlStyles.UserPaint, True)
        UpdateStyles()
        Padding = New Padding(6)
    End Sub

    Private _orientation As Orientation = Orientation.Horizontal
    Public Property Orientation As Orientation
        Get
            Return _orientation
        End Get
        Set(value As Orientation)
            _orientation = value

            Dim max = Math.Max(Width, Height)
            Dim min = Math.Min(Width, Height)

            If value = Orientation.Vertical Then
                Size = New Size(min, max)
            Else
                Size = New Size(max, min)
            End If
        End Set
    End Property

    'Fix by: @41686d6564
    <Browsable(False),
    Bindable(False),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
    EditorBrowsable(EditorBrowsableState.Never)>
    Public Overrides Property AutoSize As Boolean
        Get
            Return False
        End Get
        Set(value As Boolean)
        End Set
    End Property

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Dim g = e.Graphics

        g.Clear(BackColor)
        g.SmoothingMode = SmoothingMode.AntiAlias
        g.PixelOffsetMode = PixelOffsetMode.Half

        Dim p = Padding.All
        Dim r = 0
        Dim rec As Rectangle

        Using gp = New GraphicsPath
            If _orientation = Orientation.Vertical Then
                r = Width - 2 * p
                gp.AddArc(p, p, r, r, -180, 180)
                gp.AddArc(p, Height - r - p, r, r, 0, 180)
                r = Width - 1
                rec = New Rectangle(0, If(Checked, Height - r - 1, 0), r, r)
                'Or
                'rec = New Rectangle(0, If(Checked, 0, Height - r - 1), r, r)
                'To get the ON on top.
            Else
                r = Height - 2 * p
                gp.AddArc(p, p, r, r, 90, 180)
                gp.AddArc(Width - r - p, p, r, r, -90, 180)
                r = Height - 1
                rec = New Rectangle(If(Checked, Width - r - 1, 0), 0, r, r)
            End If

            gp.CloseFigure()

            g.FillPath(If(Checked, Brushes.DarkGray, Brushes.LightGray), gp)
            g.FillEllipse(If(Checked, Brushes.Green, Brushes.WhiteSmoke), rec)
        End Using
    End Sub

End Class

As for the second part of your question, please read CheckBox.Checked property and CheckBox.CheckedChanged event.

Impelemntation example:

Private Sub ToggleSwitch1_CheckedChanged(sender As Object, e As EventArgs) Handles ToggleSwitch1.CheckedChanged
    If ToggleSwitch1.Checked Then
        'ToDo with ON state...
    Else
        'ToDo with OFF state..
    End If
End Sub

这篇关于如何将添加在图片框中的用户控件翻转 180 度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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