透明窗口的移动范例

1 篇文章 0 订阅

xaml 文件

<WindowChrome.WindowChrome>
    <WindowChrome CaptionHeight="0" ResizeBorderThickness="20"/>
</WindowChrome.WindowChrome>

<Viewbox  Margin="0,0,0,0">
    <Grid Width="450" Height="70">
        <Border Margin="0"    BorderThickness="1" BorderBrush="Gray" CornerRadius="15">
            <Border.Effect>
                <DropShadowEffect Direction="0" ShadowDepth="0" BlurRadius="15"  Opacity="0.5" Color="#FF5B5B5B"></DropShadowEffect>
            </Border.Effect>
        </Border>

        <TextBlock  x:Name="SpeedModeName" Foreground="White" Text="{Binding SpeedMode}"    TextAlignment="Center"  FontSize ="9"  TextWrapping="Wrap" Height="44" Margin="10,10,425,10"/>
        <Canvas Margin="30,0,365,0">
            <TextBlock  x:Name="ForAndBacControlName"  Foreground="White"  Width="55" Text="进退控制"   TextAlignment="Center"  FontSize ="8"  TextWrapping="Wrap" Height="11" Canvas.Top="3" />
            <Image x:Name="ForAndBacControlImage" Source="/Res/MainTerminalDisplay/Speed.png" Width="55" Height="44" Panel.ZIndex="0" Canvas.Top="15"/>
            <TextBlock x:Name="SpeedControlNameS" Text="" Canvas.Top="16" Canvas.Left="10" Panel.ZIndex="1" TextAlignment="Center" VerticalAlignment="Center" Width="35" RenderTransformOrigin="0.639,0.686" Height="20"></TextBlock>
            <TextBlock x:Name="SpeedControlNameN" Text="" Canvas.Top="36" Canvas.Left="10" Panel.ZIndex="1" FontSize ="9" TextAlignment="Center" VerticalAlignment="Center" Width="37" RenderTransformOrigin="0.502,1.201" Height="10"></TextBlock>
        </Canvas>

        <Canvas Margin="90,0,305,0">
            <TextBlock  x:Name="RotationControlName"  Foreground="White"  Width="55" Text="旋转控制"   TextAlignment="Center"  FontSize ="8"  TextWrapping="Wrap" Height="11" Canvas.Top="3" />
            <Image x:Name="RotationImage" Source="/Res/MainTerminalDisplay/Speed.png" Width="55" Height="44" Panel.ZIndex="0" Canvas.Top="15"/>
            <TextBlock  x:Name="RotationSName" Text="" Canvas.Top="16" Canvas.Left="10" Panel.ZIndex="1" TextAlignment="Center" VerticalAlignment="Center" Width="34"></TextBlock>
            <TextBlock  x:Name="RotationNName"   Text="" Canvas.Top="36" Canvas.Left="10" Panel.ZIndex="1" FontSize ="9" TextAlignment="Center" VerticalAlignment="Center" Width="37"></TextBlock>
        </Canvas>

        <Canvas Margin="150,0,245,0">
            <Image x:Name="CatheterControlImage" Source="/Res/MainTerminalDisplay/ActionBars.png" Width="55" Height="43" Panel.ZIndex="0" Canvas.Top="11"/>
            <Label Height="63"  FontSize="9" x:Name="LeftOperatorName" VerticalAlignment="Center" BorderThickness="1"
	         Content=""   HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="55"/>
        </Canvas>

    

     


     
    </Grid>
</Viewbox>

cs 文件

//最后的宽度(Last Width)
private double LastWidth ;
//最后的高度(Last Height)
private double LastHeight ;
private static float AspectRatio = 450.0f / 70.0f;

    /// </summary>
    protected override void OnSourceInitialized(EventArgs e)
    {
        base.OnSourceInitialized(e);
        HwndSource source = HwndSource.FromVisual(this) as HwndSource;
        if (source != null)
        {
            source.AddHook(new HwndSourceHook(WinProc));
        }
    }

    public const Int32 WM_EXITSIZEMOVE = 0x0232;

    /// <summary>
    /// 重载窗口消息处理函数
    /// (Overload window message processing function)
    /// </summary>
    private IntPtr WinProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, ref Boolean handled)
    {
        IntPtr result = IntPtr.Zero;
        double width = SystemParameters.PrimaryScreenWidth;
        double height = SystemParameters.PrimaryScreenHeight;
        if (this.Width > width)
        {
            this.Width = width;
            this.Height = this.Width / AspectRatio;
        }

        if (this.Height > height)
        {
            this.Height = height;
            this.Width = this.Height * AspectRatio;
        }

        switch (msg)
        {
            //处理窗口消息 (Handle window messages)
            case WM_EXITSIZEMOVE:
                {                      
                    //上下拖拉窗口 (Drag window vertically)
                    if (this.Height != LastHeight)
                    {
                        this.Width = this.Height * AspectRatio;
                    }
                    // 左右拖拉窗口 (Drag window horizontally)
                    else if (this.Width != LastWidth)
                    {
                        this.Height = this.Width / AspectRatio;
                    }
                    LastWidth = (int)this.Width;
                    LastHeight = (int)this.Height;
                    this.Show();
                    break;
                }
        }          
        return result;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值