覆盖标题双击

时间:2015-03-23 21:53:33

标签: c# wpf

我试图覆盖双击WPF应用程序中标题栏时发生的情况。我试图调查这个,我希望尽可能简单,因为它是一个小程序,并且不希望包含任何库。我的想法是,我想双击标题栏,如果它被隐藏,窗口的内容将变为隐藏或可见。我有一些示例应用程序实际上改变了一些事情,例如从WPF中绘制标题栏。但我无法理解这一点,任何帮助都会非常感激!

<Window x:Class="TestWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" ResizeMode="NoResize" WindowStyle="None">
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="{Binding ActualHeight,ElementName=Titlebar}"/>
    </WindowChrome.WindowChrome>
    <DockPanel LastChildFill="True">
        <Border Background="DarkGray" DockPanel.Dock="Top" Height="25" x:Name="Titlebar">
            <TextBlock Text="{Binding Title, RelativeSource={RelativeSource FindAncestor,AncestorType=Window},FallbackValue=Title}" 
                       Margin="10,0,0,0"
                       VerticalAlignment="Center">
                <TextBlock.Effect>
                    <DropShadowEffect Color="White" ShadowDepth="3"/>
                </TextBlock.Effect>
            </TextBlock>
        </Border>
        <Border BorderBrush="LightGray" BorderThickness="1" Padding="4" x:Name="Content">
            <TextBlock Text="Window content" x:Name="Text"/>
        </Border>
    </DockPanel>
</Window>

1 个答案:

答案 0 :(得分:1)

好吧,多亏了Hans Passant,我做了一些挖掘他所提到的事情并想出了这一点。您需要在源代码中添加以下内容。

private const int WM_NCLBUTTONDBLCLK = 0x00A3;

protected override void OnSourceInitialized(EventArgs e)
{
    base.OnSourceInitialized(e);
    HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
    source.AddHook(WndProc);
}

private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
    if (msg == WM_NCLBUTTONDBLCLK)
    {
        //Do stuff here
    }

    return IntPtr.Zero;
}

这应该仅在No Client Area中双击时触发,但正如他所提到的那样,它没有正常显示,因为没有人希望双击标题栏来做一些不同的事情。我只想要它,因为我需要一种简单的方法来隐藏窗口的内容并将其折叠到标题栏,因为你实际上无法调整我的窗口大小。

相关问题