그립만을 통해 크기를 조정할 수있는 테두리없이 WPF 창을 만드는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/611298

문제

설정하면 ResizeMode="CanResizeWithGrip" WPF에서 Window 그런 다음 크기 조정 그립이 오른쪽 하단에 표시됩니다.

설정하면 WindowStyle="None" 또한 제목 막대가 사라지지만 회색 가장자리는 설정 될 때까지 남아 있습니다. ResizeMode="NoResize". 불행히도, 이러한 속성 세트의 조합으로 크기 조정 그립도 사라집니다.

나는 그것을 우선했다 Window'에스 ControlTemplate 관습을 통해 Style. 창의 테두리를 직접 지정하고 싶고, 4면에서 창을 크기를 조정할 수있는 사용자가 필요하지 않지만 크기 조정 그립이 필요합니다.

누군가이 모든 기준을 충족시키는 간단한 방법을 자세히 설명 할 수 있습니까?

  1. 하지 마라 국경이 있습니다 Window 그 외에는 내가 a에 나 자신을 지정합니다 ControlTemplate.
  2. 하다 오른쪽 하단에 작업 크기 조정 그립이 있어야합니다.
  3. 하지 마라 타이틀 바가 있습니다.
도움이 되었습니까?

해결책

당신이 설정하면 AllowsTransparency 에 속성 Window (투명성 값을 설정하지 않아도) 테두리가 사라지고 그립을 통해서만 크기를 조정할 수 있습니다.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="640" Height="480" 
    WindowStyle="None"
    AllowsTransparency="True"
    ResizeMode="CanResizeWithGrip">

    <!-- Content -->

</Window>

결과는 다음과 같습니다.

다른 팁

나는 국경없는 창을 만들려고 노력했다 WindowStyle="None" 그러나 내가 그것을 테스트했을 때, 맨 위에 흰색 막대가 보이는 것처럼 보이고, 일부 연구 후에는 "국경 크기"인 것으로 보이며, 여기에 이미지가 있습니다 (나는 노란색으로 언급).

The Challenge

인터넷에 대한 일부 연구와 많은 어려운 비 XAML 솔루션 이후, 내가 찾은 모든 솔루션은 C#과 많은 코드 라인의 코드 였으므로 여기에서 솔루션을 간접적으로 발견했습니다. 최대 사용자 정의 창은 낙하 그림자 효과를 잃습니다

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

메모 : .NET 4.5 프레임 워크를 사용해야하거나 이전 버전을 사용하는 경우 wpfshell을 사용하는 경우 쉘을 참조하고 사용하십시오. Shell:WindowChrome.WindowChrome 대신에.

나는 그것을 사용했다 WindowChrome 창의 속성, 흰색 "Resize Border"라는 것을 사용하는 경우 사라지지만 올바르게 작동하려면 일부 속성을 정의해야합니다.

CaptionHeight : 이것은 캡션 영역 (헤더 바)의 높이로, 일반 제목 막대처럼 에어로 스냅, 이중 클릭 동작을 허용합니다. 버튼을 작동 시키려면 이것을 0 (0)으로 설정하십시오.

resizeborderthickness : 이것은 창의 가장자리에있는 두께입니다. 나는 그 숫자가 마음에 들기 때문에 5에 넣었고, 당신이 0을 넣으면 창을 크기를 조정하기가 어렵 기 때문입니다.

이 짧은 코드를 사용한 후 결과는 다음과 같습니다.

The Solution

그리고 이제 흰색 경계는 사용하지 않고 사라졌습니다 ResizeMode="NoResize" 그리고 AllowsTransparency="True", 또한 창에 그림자가 표시됩니다.

나중에 간단하고 짧은 코드로 버튼 (버튼에 이미지를 사용하지 않았음)을 쉽게 작동시키는 방법을 설명 할 것입니다. 튜토리얼을 게시합니다.

어쩌면 또 다른 솔루션이있을 수 있습니다 (나와 같은 멍청한 솔루션이 어렵고 어려운 솔루션이 있다는 것을 알고 있습니다). 그러나 이것은 내 개인 프로젝트에 효과적입니다.

다음은 완전한 코드입니다

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Concursos"
    mc:Ignorable="d"
    Title="Concuros" Height="350" Width="525"
    WindowStyle="None"
    WindowState="Normal" 
    ResizeMode="CanResize"
    >
<WindowChrome.WindowChrome>
    <WindowChrome 
        CaptionHeight="0"
        ResizeBorderThickness="5" />
</WindowChrome.WindowChrome>

    <Grid>

    <Rectangle Fill="#D53736" HorizontalAlignment="Stretch" Height="35" VerticalAlignment="Top" PreviewMouseDown="Rectangle_PreviewMouseDown" />
    <Button x:Name="Btnclose" Content="r" HorizontalAlignment="Right" VerticalAlignment="Top" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
    <Button x:Name="Btnmax" Content="2" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,35,0" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>
    <Button x:Name="Btnmin" Content="0" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,0,70,0" Width="35" Height="35" Style="{StaticResource TempBTNclose}"/>

</Grid>

고맙습니다!

허용 된 대답은 매우 사실이지만, 허용 전환에 약간의 몰락이 있음을 지적하고 싶습니다. 하위 창 컨트롤이 나타나지 않으며, 즉 웹 브라우저가 나타나지 않으며, 일반적으로 부정적인 성능 효과를 가질 수있는 소프트웨어 렌더링을 강요합니다.

그래도 더 나은 작업이 있습니다.

테두리가없는 경계가없는 창을 만들고 웹 브라우저 컨트롤을 호스팅 할 수 있거나 단순히 URL을 가리키는 프레임 컨트롤을 호스팅 할 수있는 경우, 해당 제어의 내용이 비어있을 것입니다.

그래도 해결 방법을 찾았습니다. 창에서 Windows 스타일을 NONE으로 설정하고 Noresize로 resizemode (나와 함께 할 수 있으면 계속 조정할 수 있습니다). 그러면 검사되지 않은 허용 전환이 있는지 확인하십시오. 브라우저 컨트롤.

이제, 당신은 아마도 여전히 크기를 조정하고 싶습니까? 우리는 인터 로프 콜로 할 수 있습니다.

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

    [DllImportAttribute("user32.dll")]
    public static extern bool ReleaseCapture();

    //Attach this to the MouseDown event of your drag control to move the window in place of the title bar
    private void WindowDrag(object sender, MouseButtonEventArgs e) // MouseDown
    {
        ReleaseCapture();
        SendMessage(new WindowInteropHelper(this).Handle,
            0xA1, (IntPtr)0x2, (IntPtr)0);
    }

    //Attach this to the PreviewMousLeftButtonDown event of the grip control in the lower right corner of the form to resize the window
    private void WindowResize(object sender, MouseButtonEventArgs e) //PreviewMousLeftButtonDown
    {
        HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
        SendMessage(hwndSource.Handle, 0x112, (IntPtr)61448, IntPtr.Zero);
    }

그리고 Voila, 테두리가없고 웹 브라우저와 같은 컨트롤과의 호환성을 잃지 않고도 여전히 움직일 수 있고 방지 할 수 있습니다.

여기 샘플 :

<Style TargetType="Window" x:Key="DialogWindow">
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="ResizeMode" Value="CanResizeWithGrip"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border BorderBrush="Black" BorderThickness="3" CornerRadius="10" Height="{TemplateBinding Height}"
                            Width="{TemplateBinding Width}" Background="Gray">
                        <DockPanel>
                            <Grid DockPanel.Dock="Top">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition></ColumnDefinition>
                                    <ColumnDefinition Width="50"/>
                                </Grid.ColumnDefinitions>
                                <Label Height="35" Grid.ColumnSpan="2"
                                       x:Name="PART_WindowHeader"                                            
                                       HorizontalAlignment="Stretch" 
                                       VerticalAlignment="Stretch"/>
                                <Button Width="15" Height="15" Content="x" Grid.Column="1" x:Name="PART_CloseButton"/>
                            </Grid>
                            <Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                                        Background="LightBlue" CornerRadius="0,0,10,10" 
                                        Grid.ColumnSpan="2"
                                        Grid.RowSpan="2">
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition/>
                                        <ColumnDefinition Width="20"/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="*"/>
                                        <RowDefinition Height="20"></RowDefinition>
                                    </Grid.RowDefinitions>
                                    <ResizeGrip Width="10" Height="10" Grid.Column="1" VerticalAlignment="Bottom" Grid.Row="1"/>
                                </Grid>
                            </Border>
                        </DockPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top