WPF全屏幕窗口

WPF全屏幕窗口在实际使用中是一个比较常见的应用方法。如何才能快速简单的实现这一功能,是一个初级开发人员必须掌握的技巧。

WPF中用XAML创建WPF全屏幕窗口非常简单,只需要简单地设置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"
>

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

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

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

< Window x:Class="WindowsApp.Window1"
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;

WPF全屏幕窗口的创建代码中使用的WindowInteropHelper类将在后续的随笔中介绍。至于用C#调用Win32 API函数应该不需要进一步的介绍,不熟悉C#的朋友可以参考MSDN中的Interoperability相关内容

posted on 2011-04-25 08:38  Y#  阅读(4269)  评论(0编辑  收藏  举报

导航