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