WPF全屏幕窗口创建方法介绍

开发 开发工具
当我们想在开发程序中创建一个WPF全屏幕窗口的时候,需要做一些什么操作呢?在这篇文章中我们就为大家详细介绍了相关应用技巧。

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

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

 

  1. < Window x:Class=
    "WindowsApp.Window1" 
  2. xmlns="http://schemas.
    microsoft.com/winfx/2006/
    xaml/presentation"
     
  3. xmlns:x="http://schemas.
    microsoft.com/winfx/2006/xaml"
     
  4. WindowState="Maximized" 
  5. Topmost="True"   
  6. WindowStyle="None" 
  7. AllowsTransparency="true" 
  8. > 
  9. < Grid> 
  10. < !--忽略建立动画的代码-->   
  11. < /Grid> 
  12. < /Window> 

 

 

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

 

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

 

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

 

 

  1. < Window x:Class="WindowsApp.
    Window1"
     
  2. xmlns="http://schemas.
    microsoft.com/winfx/2006/
    xaml/presentation"
     
  3. xmlns:x="http://schemas.
    microsoft.com/winfx/2006/xaml"
     
  4. WindowState="Maximized" 
  5. Topmost="True"   
  6. Loaded="OnMainLoad" 
  7. > 
  8. < Grid> 
  9. < !--忽略建立动画的代码-->   
  10. < /Grid> 
  11. < /Window> 
  12. private void OnMainLoad
    (object sender, Routed
    EventArgs e)  
  13. {  
  14. int nStyle = Win32API.
    GetWindowLong(new WindowInterop
    Helper(this).Handle;,Win32API.
    GWL_STYLE);  
  15. nStyle &= ~Win32API.WS_CAPTION;  
  16. Win32API.SetWindowLong
    (new WindowInteropHelper(this).
    Handle;, Win32API.GWL_STYLE, nStyle);  
  17. }  
  18. public class Win32API  
  19. {  
  20. [DllImport("user32.dll")]  
  21. public static extern int 
    SetWindowLong(IntPtr hWnd, 
    int nIndex, int New);  
  22. [DllImport("user32.dll")]  
  23. public static extern int 
    GetWindowLong(IntPtr hWnd, 
    int nIndex);   
  24. }  
  25. public const int GWL_STYLE = -16;  
  26. public const int GWL_EXSTYLE = -20;   
  27. public const int WS_CAPTION = 
    0x00C00000

 

 

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

责任编辑:曹凯 来源: 博客园
相关推荐

2009-07-16 16:09:51

Swing全屏幕模式

2013-07-03 13:54:26

jQuery

2009-12-24 15:22:10

WPF继承自定义窗口

2017-08-11 19:02:21

Android全屏幕适配

2018-05-04 15:26:10

Android开发全屏幕

2009-12-28 15:39:33

WPF滑动条

2009-12-23 14:19:07

WPF单向绑定

2009-11-10 14:52:13

VB.NET实现

2009-12-28 15:48:14

WPF窗口颜色

2010-01-26 17:36:17

Android实现全屏

2009-12-25 10:05:06

WPF资源

2009-12-23 18:06:25

WPF模板

2009-12-24 14:18:57

WPF类型转换

2009-12-28 15:08:12

WPF字体

2009-12-28 11:14:29

WPF显示文本

2009-12-24 16:11:07

WPF图像处理

2009-12-25 16:10:31

WPF内存

2009-12-25 17:10:51

WPF动态资源

2009-12-23 14:49:46

WPF面板

2009-12-28 17:48:01

WPF界面布局
点赞
收藏

51CTO技术栈公众号