原來這並不難,儘管它可能會帶來不良的副作用,例如從遊戲中竊取資源,導致自發的滯後(稍微顯着),並使其完美運行,但這需要一些時間。
我所做的是創建一個新的Form
併爲其ResizeEnd
事件分配了一個事件處理程序。事件處理程序將public static Rectangle fakeWindowRect
設置爲虛擬窗口客戶區的新矩形,該區域在Form.RectangleToScreen()
方法的幫助下計算。
這裏有一些代碼段:
static void Main(string[] args)
{
System.Windows.Forms.Form f = new System.Windows.Forms.Form();
f.ClientSize = new System.Drawing.Size(800, 600);
f.TransparencyKey = f.BackColor;
((Action)(() => System.Windows.Forms.Application.Run(f))).BeginInvoke(
null, null);
using (Game1 game = new Game1())
{
f.ResizeEnd += new EventHandler(game.f_LocationChanged);
game.Run();
}
}
public class Game1 : Microsoft.Xna.Framework.Game
{
public static Rectangle windowRect;
/* ... */
protected override void Update(GameTime gameTime)
{
if (windowRect.X != this.Window.ClientBounds.X ||
windowRect.Y != this.Window.ClientBounds.Y ||
windowRect.Width != this.Window.ClientBounds.Width ||
windowRect.Height != this.Window.ClientBounds.Height)
{
// this method sets the game window size, but not location
InitGraphicsMode(windowRect.Width, windowRect.Height,
this.graphics.IsFullScreen);
var win = System.Windows.Forms.Control.FromHandle(
this.Window.Handle) as
System.Windows.Forms.Form;
win.SetBounds(windowRect.X,
windowRect.Y,
windowRect.Width,
windowRect.Height);
win.Activate();
}
}
public void f_LocationChanged(object sender, EventArgs e)
{
var FakeWindow = sender as System.Windows.Forms.Form;
var drawClientArea = FakeWindow.RectangleToScreen(
FakeWindow.ClientRectangle);
windowRect = new Rectangle(
drawClientArea.X,
drawClientArea.Y,
drawClientArea.Width,
drawClientArea.Height);
}
}
實現可能是可怕的,錯周圍的一切,但它的作品沒有在偷遊戲中的所有資源,甚至調整假形式的遊戲滴一些幀時,但不會完全停頓。
所以我測試了它,它的工作原理,但它主要是爲了科學,我不打算在短時間內使用這種方法。也許當我真的,真的需要它。