WPF疑难杂症之二(全屏幕窗口)

近日的学习中遇到一个非常奇怪的问题:用XAML文件创建了一个全屏幕窗口,然后,在窗口中建立了一个非常简单的动画。一切都在我的掌控之中,实现非常的顺利。

WPF中用XAML创建全屏幕窗口非常简单,只需要简单地设置Window元素的一些属性即可:

<Window x:Class="WindowsApp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    WindowState="Maximized"

    Topmost="True"    

    WindowStyle="None"

    AllowsTransparency="true"
    >

    <Grid>

      <!--忽略建立动画的代码-->  

    </Grid>

</Window>

 

最后程序的运行结果却出乎所料,在调用Storyboard.Begin之前,一切都很正常,但是一旦启动动画,程序运行及很慢,鼠标的运动很慢很慢。有兴趣的朋友可以自己尝试一下。

 

如果把窗口Style稍微修改,问题就得到了解决,把WindowStyleNone修改为其它的值似乎都可以正常运行。动画的效率得到了极大的提高。

 

但是我们要的就是全屏幕,那怎么办呢?时间比较紧急,咱就曲线救国绕过去吧!在XAMLWindow属性中WindowStyle保留其默认值,在窗口的加载响应函数里直接用了Win32 API函数来修改窗口的Style。现在可以几乎可以肯定这不像是正统的方法,或者还有其它的还没有了解的知识。修改后的代码如下:

 

<Window x:Class="WindowsApp.Window1"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    WindowState="Maximized"

    Topmost="True"    

    Loaded="OnMainLoad"

    >

    <Grid>

      <!--忽略建立动画的代码-->  

    </Grid>

</Window>

 

private void OnMainLoad(object sender, RoutedEventArgs e)

{

int nStyle = Win32API.GetWindowLong(new WindowInteropHelper(this).Handle;,Win32API.GWL_STYLE);

nStyle &= ~Win32API.WS_CAPTION;

Win32API.SetWindowLong(new WindowInteropHelper(this).Handle;, Win32API.GWL_STYLE, nStyle);

}

 

public class Win32API

{

     [DllImport("user32.dll")]

     public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int New);

       

     [DllImport("user32.dll")]

     public  static extern int GetWindowLong(IntPtr hWnd, int nIndex);

}

 

public const int GWL_STYLE = -16;

public const int GWL_EXSTYLE = -20;       

public const int WS_CAPTION = 0x00C00000;

 

代码中使用的 WindowInteropHelper 类将在后续的随笔中介绍。至于用 C# 调用 Win32 API 函数应该不需要进一步的介绍,不熟悉 C# 的朋友可以参考 MSDN 中的 Interoperability 相关内容。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值