WPF全屏幕窗口創(chuàng)建方法介紹
WPF全屏幕窗口在實際使用中是一個比較常見的應用方法。如何才能快速簡單的實現(xiàn)這一功能,是一個初級開發(fā)人員必須掌握的技巧。#t#
WPF中用XAML創(chuàng)建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"
- >
- < Grid>
- < !--忽略建立動畫的代碼-->
- < /Grid>
- < /Window>
最后程序的運行結果卻出乎所料,在調用Storyboard.Begin之前,一切都很正常,但是一旦啟動動畫,程序運行及很慢,鼠標的運動很慢很慢。有興趣的朋友可以自己嘗試一下。
如果把窗口Style稍微修改,問題就得到了解決,把WindowStyle的None修改為其它的值似乎都可以正常運行。動畫的效率得到了極大的提高。
但是我們要的就是WPF全屏幕窗口,那怎么辦呢?時間比較緊急,咱就曲線救國繞過去吧!在XAML的Window屬性中WindowStyle保留其默認值,在窗口的加載響應函數(shù)里直接用了Win32 API函數(shù)來修改窗口的Style?,F(xiàn)在可以幾乎可以肯定這不像是正統(tǒng)的方法,或者還有其它的還沒有了解的知識。修改后的代碼如下:
- < 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, Routed
EventArgs e)- {
- int nStyle = Win32API.
GetWindowLong(new WindowInterop
Helper(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全屏幕窗口的創(chuàng)建代碼中使用的WindowInteropHelper類將在后續(xù)的隨筆中介紹。至于用C#調用Win32 API函數(shù)應該不需要進一步的介紹,不熟悉C#的朋友可以參考MSDN中的Interoperability相關內容