wpf按钮闪烁,防止WPF窗口闪烁

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:

hSsVA.gif

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:

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">

lots of stuff...

Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="2" Grid.Row="1" Grid.Column="1">

Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="2">

Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="2" Grid.ColumnSpan="2" Grid.Column="1"/>

Template="...the stuff I left out..." Foreground="White" Background="{x:Null}" BorderBrush="{x:Null}" Margin="3,2,3,3" Grid.RowSpan="2"/>

@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:

RlUsm.jpg

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值