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相关内容。
posted @ 2006-12-16 12:21  赖仪灵  阅读(8347)  评论(9编辑  收藏  举报