首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >WPF中透明窗口实现拖拽功能

WPF中透明窗口实现拖拽功能

作者头像
码客说
发布于 2023-07-11 14:07:48
发布于 2023-07-11 14:07:48
8150
举报
文章被收录于专栏:码客码客

前言

默认创建的窗口是可以拖拽放大缩小的,但是如果窗口设置为透明,就不能拖拽了。

页面

代码语言:javascript
复制
<Grid Name="ImgGrid" Margin="0,0,0,0">
    <Border
        Margin="5"
        Background="#333333"
        BorderBrush="#2D8CF0"
        BorderThickness="1">
        <Image
            Name="MImg"
            MouseLeftButtonDown="Window_MouseLeftButtonDown_1"
            Source="/Images/TouPing/phone_bg.jpg" />
    </Border>

    <Grid Name="MThumpGrid">
        <Thumb
            Width="16"
            Height="16"
            HorizontalAlignment="Left"
            VerticalAlignment="Top"
            Cursor="ScrollNW"
            DragDelta="LeftTop_DragDelta" />

        <Thumb
            Width="16"
            Height="16"
            HorizontalAlignment="Right"
            VerticalAlignment="Top"
            Cursor="ScrollNE"
            DragDelta="RightTop_DragDelta" />

        <Thumb
            Width="16"
            Height="16"
            HorizontalAlignment="Left"
            VerticalAlignment="Bottom"
            Cursor="ScrollSW"
            DragDelta="LeftBottom_DragDelta" />

        <Thumb
            Width="16"
            Height="16"
            HorizontalAlignment="Right"
            VerticalAlignment="Bottom"
            Cursor="ScrollSE"
            DragDelta="RightBottom_DragDelta" />
    </Grid>
</Grid>

样式调整

代码语言:javascript
复制
<Window.Resources>
    <Style TargetType="Thumb">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <Ellipse
                        Fill="White"
                        Stroke="#2D8CF0"
                        StrokeThickness="2" />
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

事件

代码语言:javascript
复制
private void LeftTop_DragDelta(object sender, DragDeltaEventArgs e)
{
    if (Width <= 400 && e.HorizontalChange > 0)
    {
        return;
    }
    if (Height <= 400 && e.VerticalChange > 0)
    {
        return;
    }
    Left += e.HorizontalChange;
    Top += e.VerticalChange;
    Width -= e.HorizontalChange;
    Height -= e.VerticalChange;
}

private void LeftBottom_DragDelta(object sender, DragDeltaEventArgs e)
{
    if (Width <= 400 && e.HorizontalChange > 0)
    {
        return;
    }
    if (Height <= 400 && e.VerticalChange < 0)
    {
        return;
    }
    Left += e.HorizontalChange;
    Width -= e.HorizontalChange;
    Height += e.VerticalChange;
}

private void RightTop_DragDelta(object sender, DragDeltaEventArgs e)
{
    if (Width <= 400 && e.HorizontalChange < 0)
    {
        return;
    }
    if (Height <= 400 && e.VerticalChange > 0)
    {
        return;
    }
    Top += e.VerticalChange;
    Width += e.HorizontalChange;
    Height -= e.VerticalChange;
}

private void RightBottom_DragDelta(object sender, DragDeltaEventArgs e)
{
    if (Width <= 400 && e.HorizontalChange < 0)
    {
        return;
    }
    if (Height <= 400 && e.VerticalChange < 0)
    {
        return;
    }
    Width += e.HorizontalChange;
    Height += e.VerticalChange;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-06-21,如有侵权请联系 cloudcommunity@tencent.com 删除
目录
  • 前言
  • 页面
  • 事件
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档