WPF疑难杂症之二(全屏幕窗口)
近日的學(xué)習(xí)中遇到一個(gè)非常奇怪的問(wèn)題:用XAML文件創(chuàng)建了一個(gè)全屏幕窗口,然后,在窗口中建立了一個(gè)非常簡(jiǎn)單的動(dòng)畫(huà)。一切都在我的掌控之中,實(shí)現(xiàn)非常的順利。
WPF中用XAML創(chuàng)建全屏幕窗口非常簡(jiǎn)單,只需要簡(jiǎn)單地設(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)畫(huà)的代碼-->??
??? </Grid>
</Window>
?
最后程序的運(yùn)行結(jié)果卻出乎所料,在調(diào)用Storyboard.Begin之前,一切都很正常,但是一旦啟動(dòng)動(dòng)畫(huà),程序運(yùn)行及很慢,鼠標(biāo)的運(yùn)動(dòng)很慢很慢。有興趣的朋友可以自己嘗試一下。
?
如果把窗口Style稍微修改,問(wèn)題就得到了解決,把WindowStyle的None修改為其它的值似乎都可以正常運(yùn)行。動(dòng)畫(huà)的效率得到了極大的提高。
?
但是我們要的就是全屏幕,那怎么辦呢?時(shí)間比較緊急,咱就曲線(xiàn)救國(guó)繞過(guò)去吧!在XAML的Window屬性中WindowStyle保留其默認(rèn)值,在窗口的加載響應(yīng)函數(shù)里直接用了Win32 API函數(shù)來(lái)修改窗口的Style。現(xiàn)在可以幾乎可以肯定這不像是正統(tǒng)的方法,或者還有其它的還沒(méi)有了解的知識(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)畫(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;
?
代碼中使用的WindowInteropHelper類(lèi)將在后續(xù)的隨筆中介紹。至于用C#調(diào)用Win32 API函數(shù)應(yīng)該不需要進(jìn)一步的介紹,不熟悉C#的朋友可以參考MSDN中的Interoperability相關(guān)內(nèi)容。轉(zhuǎn)載于:https://www.cnblogs.com/YilingLai/archive/2006/12/16/594107.html
總結(jié)
以上是生活随笔為你收集整理的WPF疑难杂症之二(全屏幕窗口)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 进入Google:《Google成功的七
- 下一篇: 火车站经典留言