防止WPF窗口闪烁 [英] Prevent WPF window flicker

查看:2897
本文介绍了防止WPF窗口闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要能够隐藏其控件之一,并同时缩小窗口一个无国界的WPF窗口。



的问题是,它看起来很可怕。



下面就是我现在所做的:



<预类=郎-CSH prettyprint-覆盖> 私人无效btnShowHideTopBar_Click(对象发件人,RoutedEventArgs E)
{
如果(commandTopHide ==真)
{
txtblkShowHideTopBar.Text =显示顶酒吧;
commandTopHide = FALSE;
myWindow.Left = 1100;
myWindow.Width = 180;
RSide.Width =新GridLength(0,GridUnitType.Pixel);
}
,否则如果(commandTopHide ==假)
{
txtblkShowHideTopBar.Text =隐藏顶酒吧;
commandTopHide = TRUE;
myWindow.Left = 1030;
myWindow.Width = 250;
RSide.Width =新GridLength(70 Gri​​dUnitType.Pixel);
}
}

和这里是它看起来像慢动作:结果



要解决这个问题,我已经尝试了几件事情。每个显然只适用于的WinForms。结果
为例,我跟着的这个博客帖子在蜜蜂的Eee 禁用绘图和锁定窗口更新无济于事。



我也试着重写WM_PAINT消息所看到的这个帖子,但我被困在一旦我抓住它如何处理消息做。行 base.WndProc(REF味精); 引发错误System.Windows.Window'不包含定义的WndProc



当然,该代码是WinForms和我使用在<一个描述的 hwndSource.AddHook 方法HREF =htt​​p://stackoverflow.com/q/624367/2596334>如何在WPF处理的WndProc消息?)...









我的XAML是相当庞大由于我的自定义按钮,所以我离开了这一点。这里还剩下些什么:

 <窗​​口x:名称=mywindow的X:类=myNamespace.myWindowClass
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
的xmlns:D =htt​​p://schemas.microsoft.com/expression/blend/2008
的xmlns:MC =htt​​p://schemas.openxmlformats.org/markup-compatibility/2006MC:忽略=D
标题=威曼HEIGHT =210WIDTH =250ResizeMode =NoResizeWindowStyle =无了minWidth =180
了minHeight =210了maxWidth = 250了maxHeight =210左=1030顶级=66加载=ControlBoxLoadEvent
图标=包:// siteoforigin:,,, /资源/ App.ico关闭=MainWindowIsClosing 前景=白
背景={X:空}AllowsTransparency =真>
< Window.Resources>
<控件模板......东西,我离开了..>很多东西...< /控件模板>
< /Window.Resources>
<电网X:NAME =MainWindowGridRenderTransformOrigin =0.5,0.5>
< Grid.Background>
<一个LinearGradientBrush终点=160240StartPoint可以=160,-20MappingMode =绝对>
< LinearGradientBrush.RelativeTransform>
<&的TransformGroup GT;
将; ScaleTransform CenterY =0.5的centerX =0.5的scaleY =1的ScaleX =1/>
将; SkewTransform AngleY​​ =0的AngleX =0CenterY =0.5的centerX =0.5/>
< RotateTransform角= - 4.764CenterY =0.5的centerX =0.5/>
< TranslateTransform />
< /&的TransformGroup GT;
< /LinearGradientBrush.RelativeTransform>
将;渐变停止颜色=#FF1B1B1B偏移=1/>
<渐变停止颜色=#7FC3C3C3/>
< /一个LinearGradientBrush>
< /Grid.Background>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition />
< /Grid.RowDefinitions>
< Grid.ColumnDefinitions>
< ColumnDefinition NAME =RSideWIDTH =70/>
< ColumnDefinition WIDTH =84 */>
< ColumnDefinition WIDTH =83 */>
< /Grid.ColumnDefinitions>
<按钮X:NAME =btnShowHideTopBar的FontFamily =宋体圆形MT大胆字号=18点击=btnShowHideTopBar_Click
模板=...我冷落了的东西... 前景=白色背景={X:空}BorderBrush ={X:空}保证金=2Grid.Row =1Grid.Column =1>
< TextBlock的X:名称=txtblkShowHideTopBarTextWrapping =自动换行文本=隐藏的顶栏TextAlignment =中心/>
< /按钮>
<按钮X:NAME =btnShowHideSideBarGrid.Column =2Grid.Row =1的FontFamily =宋体圆形MT大胆字号=18点击=eventHideShowSideBar_Click
模板=...我离开了......东西前景=白色背景={X:空}BorderBrush ={X:空}保证金=2>
< TextBlock的X:名称=txtblkShowHideSideBarTextWrapping =自动换行文本=隐藏边栏TextAlignment =中心/>
< /按钮>
<按钮内容=配置点击=btnConfigure_Click的FontFamily =宋体圆形MT大胆字号=18
模板=...的东西,我离开了......前景=白色背景={X:空}BorderBrush ={X:空}保证金=2Grid.ColumnSpan =2Grid.Column =1/>
<电网Grid.RowSpan =2>
< Grid.Background>
<一个LinearGradientBrush终点=0.5,1StartPoint可以=0.5,0>
将;渐变停止颜色=#FF3C8AE8偏移=0/>
将;渐变停止颜色=#FF032440偏移=1/>
< /一个LinearGradientBrush>
< /Grid.Background>
< /网格和GT;
<按钮X:NAME =btnScrollLeftCONTENT =R端点击=RSideScrollButton_Click的FontFamily =宋体圆形MT大胆字号=18
模板=...的东西我离开了......前景=白色背景={X:空}BorderBrush ={X:空}保证金=3,2,3,3Grid.RowSpan =2/> ;
< /网格和GT;





@Walt Ritscher,我不'T知道藏身之列,我只是将它设置宽度为0。但更大的问题似乎是,我需要调整和重新定位包含它的窗口。









@ErnodeWeerd,我已经实现了一个秒表,你可以在这里看到:

 秒表S =新的秒表(); 
串;字符串B:串c;

s.Start();
myWindow.Left = 1100;
s.Stop();一个= s.Elapsed.ToString(); s.Reset();

s.Start();
myWindow.Width = 180;
s.Stop(); B = s.Elapsed.ToString(); s.Reset();

s.Start();
RSide.Width =新GridLength(0,GridUnitType.Pixel);
s.Stop(); C = s.Elapsed.ToString(); s.Reset();

MessageBox.Show(A + Environment.NewLine +
B + Environment.NewLine +
C + Environment.NewLine);

这是结果:结果


解决方案

您可以使用一个透明的窗口(ALLOWTRANSPARENCY = TRUE)具一格,那么当你的控制需要被隐藏,你可以设置控制的知名度,崩溃了。不会有闪烁,没有窗口的移动,但不会有任何那里要么所以你会看到通过量,如果你会调整,感动你的窗口。


I've got a borderless WPF window that needs to be able to hide one of its controls and shrink the window at the same time.

The problem is that it looks terrible.

Here's what I am doing now:

private void btnShowHideTopBar_Click(object sender, RoutedEventArgs e)
{
    if (commandTopHide == true)
    {
        txtblkShowHideTopBar.Text = "Show Top Bar";
        commandTopHide = false;
        myWindow.Left = 1100;
        myWindow.Width = 180;
        RSide.Width = new GridLength(0, GridUnitType.Pixel);
    }
    else if (commandTopHide == false)
    {
        txtblkShowHideTopBar.Text = "Hide Top Bar";
        commandTopHide = true;
        myWindow.Left = 1030;
        myWindow.Width = 250;
        RSide.Width = new GridLength(70, GridUnitType.Pixel);
    }
}

And here is what it looks like in slow motion:

To correct this, I have tried a few things. Each of which apparently only apply to Winforms.
For example, I followed this blog post at Bee Eee to disable drawing and 'lock window update' to no avail.

I've also tried overriding the WM_PAINT message as seen in this post but I got stuck on what to do with the message once I catch it. The line base.WndProc(ref msg); throws the error "'System.Windows.Window' does not contain a definition for 'WndProc'"

Granted, that code was for Winforms and I am using the hwndSource.AddHook method as described in How to handle WndProc messages in WPF?)...



My xaml is fairly massive due to a my custom buttons, so I'll leave that out. Here is what's left:

<Window x:Name="myWindow" x:Class="myNamespace.myWindowClass"
    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" mc:Ignorable="d"
    Title="WiMan" Height="210" Width="250" ResizeMode="NoResize" WindowStyle="None" MinWidth="180" 
    MinHeight="210" MaxWidth="250" MaxHeight="210" Left="1030" Top="66" Loaded="ControlBoxLoadEvent" 
    Icon="pack://siteoforigin:,,,/Resources/App.ico" Closing="MainWindowIsClosing" Foreground="White" 
    Background="{x:Null}" AllowsTransparency="True">
<Window.Resources>
    <ControlTemplate ...stuff I left out.. > lots of stuff... </ControlTemplate>
</Window.Resources>
<Grid x:Name="MainWindowGrid" RenderTransformOrigin="0.5,0.5">
    <Grid.Background>
        <LinearGradientBrush EndPoint="160,240" StartPoint="160,-20" MappingMode="Absolute">
            <LinearGradientBrush.RelativeTransform>
                <TransformGroup>
                    <ScaleTransform CenterY="0.5" CenterX="0.5" ScaleY="1" ScaleX="1"/>
                    <SkewTransform AngleY="0" AngleX="0" CenterY="0.5" CenterX="0.5"/>
                    <RotateTransform Angle="-4.764" CenterY="0.5" CenterX="0.5"/>
                    <TranslateTransform/>
                </TransformGroup>
            </LinearGradientBrush.RelativeTransform>
            <GradientStop Color="#FF1B1B1B" Offset="1"/>
            <GradientStop Color="#7FC3C3C3"/>
        </LinearGradientBrush>
    </Grid.Background>
    <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Name="RSide" Width="70"/>
        <ColumnDefinition Width="84*"/>
        <ColumnDefinition Width="83*"/>
    </Grid.ColumnDefinitions>
    <Button x:Name="btnShowHideTopBar" FontFamily="Arial Rounded MT Bold" FontSize="18" Click="btnShowHideTopBar_Click" 
      Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="2" Grid.Row="1" Grid.Column="1">
        <TextBlock x:Name="txtblkShowHideTopBar" TextWrapping="Wrap" Text="Hide Topbar" TextAlignment="Center"/>
    </Button>
    <Button x:Name="btnShowHideSideBar" Grid.Column="2" Grid.Row="1" FontFamily="Arial Rounded MT Bold" FontSize="18" Click="eventHideShowSideBar_Click" 
     Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="2">
        <TextBlock x:Name="txtblkShowHideSideBar" TextWrapping="Wrap" Text="Hide Sidebar" TextAlignment="Center"/>
    </Button>
    <Button Content="Configure" Click="btnConfigure_Click" FontFamily="Arial Rounded MT Bold" FontSize="18" 
     Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="2" Grid.ColumnSpan="2" Grid.Column="1"/>
    <Grid Grid.RowSpan="2">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF3C8AE8" Offset="0"/>
                <GradientStop Color="#FF032440" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
    </Grid>
    <Button x:Name="btnScrollLeft" Content="R Side" Click="RSideScrollButton_Click" FontFamily="Arial Rounded MT Bold" FontSize="18" 
      Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="3,2,3,3" Grid.RowSpan="2"/>
</Grid>

@Walt Ritscher, I don't know about 'hiding' the column, I'm just setting it's width to 0. But the bigger problem seems to be that I need to resize and reposition the window that contains it.



@ErnodeWeerd, I've implemented a stopwatch as you can see here:

    Stopwatch s = new Stopwatch();
    string a; string b; string c; 

    s.Start();
    myWindow.Left = 1100;
    s.Stop(); a = s.Elapsed.ToString(); s.Reset();

    s.Start();
    myWindow.Width = 180;
    s.Stop(); b = s.Elapsed.ToString(); s.Reset();

    s.Start();
    RSide.Width = new GridLength(0, GridUnitType.Pixel);
    s.Stop(); c = s.Elapsed.ToString(); s.Reset();

    MessageBox.Show(a + Environment.NewLine +
            b + Environment.NewLine +
            c + Environment.NewLine);

This is the result:

解决方案

You can use a transparent window (AllowTransparency = True) with a grid, then when your control needs to be hidden you can set the visibility of the control to collapsed. There will be no flickering and no moving of the window but there won't be anything there either so you will see throug as if you would have resized, moved your window.

这篇关于防止WPF窗口闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆