user1974555
user1974555

Reputation: 89

XNA Game does not update while "dragging" window, any event handlers?

I'm new with XNA and C#, and I've come to the point in my XNA project where I need event handlers to predict when the game loses focus so that I can sync music and visuals once it gains focus again. But I got one problem; the game does not "update" while being dragged, but I can't seem to find an suitable event listener for this. I've tried:

System.Windows.Forms.Control.FromHandle(Window.Handle).Move += new EventHandler(DeactivateGame);

This one calls "DeactivateGame" tons of times while moving the window. But even if it works, despite the fact it calls the function more than once, I can't see a event handler that calls a function when the window handle is released so that the game can resume again by calling "ActivateGame"

A sidenote (if it helps);

this.Activated += new EventHandler<EventArgs>(NotifyActivated);
this.Deactivated += new EventHandler<EventArgs>(NotifyDeactivated); 

These event handlers works fine when minimizing the window or putting focus on something else than the game window, but it does not register the window being dragged. Maybe obvious for the one used to programming, but I just want to make sure I've given enough information

EDIT: The function I want to add as the result of the event handler is a DateTime/TimeSpan that gets called when the window is out of focus or dragged. When dropped or gets focus again, this will compare the current time with the time set when the window lost focus to calculate the lost time in between.

Upvotes: 3

Views: 1087

Answers (1)

deadlydog
deadlydog

Reputation: 24394

For detecting when the XNA window is being dragged, you were on the right track using the Window.Handle with Windows Forms. You can simply listen to the ResizeBegin and ResizeEnd events to know when the user starts moving the window and when they release it.

var xnaWinForm = (System.Windows.Forms.Control.FromHandle(Window.Handle) as System.Windows.Forms.Form);
if (xnaWinForm != null)
{
    xnaWinForm.ResizeBegin += new EventHandler(xnaWinForm_ResizeBegin);
    xnaWinForm.ResizeEnd += new EventHandler(xnaWinForm_ResizeEnd);
}

And here's what the event handlers look like.

void xnaWinForm_ResizeBegin(object sender, EventArgs e)
{
    // XNA window is starting to be moved.
}

void xnaWinForm_ResizeEnd(object sender, EventArgs e)
{
    // XNA window was released and is no longer being moved.
}

Then just combine this with the other events you mentioned for determining when the window is minimized/restored/active to determine how long the window has been "inactive" for.

Upvotes: 1

Related Questions