自学WPF--第三课透明与混色

本文介绍如何使用WPF实现窗体透明效果及鼠标控制窗体移动的方法。通过设置Opacity属性来调整透明度,并结合AllowTransparency与WindowStyle属性完成整体设置。同时提供C#与VB.NET两种语言的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

WPF可是很好的设置空间与窗体透明,通过属性Opacity设置(属性值介于0-1),窗体透明还需设置窗体的AllowTransparency属性为True(允许透明),以及WindowStyle为None(窗体无边框),

示例如下图:

代码如下:

    <Grid Opacity="0.5">

 <Ellipse Height="89" HorizontalAlignment="Left" Margin="57,44,0,0" Name="ellipse1" Stroke="Black" VerticalAlignment="Top" Width="163" Fill="Red" Opacity="0.5" />

        <Ellipse Height="88" HorizontalAlignment="Left" Margin="149,44,0,0" Name="ellipse2" Stroke="Black" VerticalAlignment="Top" Width="185" Fill="Lime" Opacity="0.5" />

        <Ellipse Height="100" HorizontalAlignment="Left" Margin="88,95,0,0" Name="ellipse3" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="Blue" Opacity="0.5" />

    </Grid>

设置鼠标控制窗体移动事件代码如下:

C#:

        private double oldx, oldy;

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)

        {

            oldx = e.GetPosition(this).X;

            oldy = e.GetPosition(this).Y;

        }

       private void Window_MouseMove(object sender, MouseEventArgs e)

        {

            if (e.LeftButton == MouseButtonState.Pressed)

            {

                double x = e.GetPosition(this).X;

                double y = e.GetPosition(this).Y;

 

                double dx = x - oldx;

                double dy = y - oldy;

 

                this.Left += dx;

                this.Top += dy;

 

                oldx = x;

                oldy = y;

            }

        }

 

vb.net代码:

Class MainWindow
    Dim oldx As Double
    Dim oldy As Double


    Private Sub MainWindow_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseDown
        oldx = e.GetPosition(Me).X
        oldy = e.GetPosition(Me).Y
    End Sub

    Private Sub MainWindow_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Input.MouseEventArgs) Handles Me.MouseMove
       
            If e.LeftButton = MouseButtonState.Pressed Then
                Dim x As Double = e.GetPosition(Me).X
                Dim y As Double = e.GetPosition(Me).Y
                Dim dx As Double = x - oldx
                Dim dy As Double = y - oldy
                Me.Left += dx
                Me.Top += dy
                oldx = x
                oldy = y
            End If

    End Sub
End Class

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值