How come GraphicsDevice.Viewport is in the wrong place?
Question
I derive GraphicsControl
from Control
:
public abstract class GraphicsControl : Control
{
GraphicsDeviceService graphicsDeviceService;
protected override void OnCreateControl ( )
{
// Don't initialize the graphics device if we are running in the designer.
if (!DesignMode)
{
graphicsDeviceService =
GraphicsDeviceService.AddReference
(
Handle,
new System.Drawing.Size ( ClientSize.Width,
ClientSize.Height )
);
// Register the service, so components like ContentManager can find it.
services.AddService ( graphicsDeviceService );
// Give derived classes a chance to initialize themselves.
Initialize ( );
}
base.OnCreateControl ( );
}
string DeviceBeginDraw ( )
{
// ensure drawing is valid
// set up viewport
Viewport viewport = new Viewport
(
ClientRectangle.X,
ClientRectangle.Y,
ClientSize.Width,
ClientSize.Height
);
viewport.MinDepth = 0;
viewport.MaxDepth = 1;
GraphicsDevice.Viewport = viewport;
if (viewport.Bounds.Contains ( mouse.X, mouse.Y ))
{
// fire event
OnMouseMoved(this,
new MouseMovedEventArgs(new Microsoft.Xna.Framework.Point(mouse.X, mouse.Y)));
}
}
}
Then the derived Canvas
control as follows:
public sealed class Canvas : GraphicsControl
{
// subscribe to MousedMoved event
}
The area responding to the mouse movement is located at the screens upper left (0, 0). The overall area overlaps the intended base control but is not Docked, filling the Parent control. See Image:
Can anyone tell me what I might be doing wrong? If additional code is required, just ask.
Also, MSDN seems to reference ClientRectangle
as the property to use.
Solution
Looks like I found the problem here:
graphicsDeviceService =
GraphicsDeviceService.AddReference
(
Handle,
new System.Drawing.Size ( ClientSize.Width,
ClientSize.Height )
);
Since GraphicControl derives from Control
, base.Handle
is passing in the handle of the base control class rather than the handle of the owning System.Windows.Forms.Form
. Once the owning Form's Handle is passed to graphicsDeviceService, the mouse tracks perfectly.
public abstract class GraphicsControl : Control
{
protected GraphicsControl ( System.Windows.Forms.Form Owner )
{
owner = Owner;
}
protected override void OnCreateControl ( )
{
// construction logic
graphicsDeviceService =
GraphicsDeviceService.AddReference
(
owner.Handle,
vpRectangle
);
// more construction logic
// Give derived classes a chance to initialize themselves.
Initialize ( );
}
base.OnCreateControl ( );
}
}