近日的學(xué)習(xí)中遇到一個(gè)非常奇怪的問題:用XAML文件創(chuàng)建了一個(gè)全屏幕窗口,然后,在窗口中建立了一個(gè)非常簡單的動(dòng)畫。一切都在我的掌控之中,實(shí)現(xiàn)非常的順利。
WPF中用XAML創(chuàng)建全屏幕窗口非常簡單,只需要簡單地設(shè)置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>
<!--忽略建立動(dòng)畫的代碼-->
</Grid>
</Window>
最后程序的運(yùn)行結(jié)果卻出乎所料,在調(diào)用Storyboard.Begin之前,一切都很正常,但是一旦啟動(dòng)動(dòng)畫,程序運(yùn)行及很慢,鼠標(biāo)的運(yùn)動(dòng)很慢很慢。有興趣的朋友可以自己嘗試一下。
如果把窗口Style稍微修改,問題就得到了解決,把WindowStyle的None修改為其它的值似乎都可以正常運(yùn)行。動(dòng)畫的效率得到了極大的提高。
但是我們要的就是全屏幕,那怎么辦呢?時(shí)間比較緊急,咱就曲線救國繞過去吧!在XAML的Window屬性中WindowStyle保留其默認(rèn)值,在窗口的加載響應(yīng)函數(shù)里直接用了Win32 API函數(shù)來修改窗口的Style?,F(xiàn)在可以幾乎可以肯定這不像是正統(tǒng)的方法,或者還有其它的還沒有了解的知識(shí)。修改后的代碼如下:
<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>
<!--忽略建立動(dòng)畫的代碼-->
</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;
聯(lián)系客服