我从GraphicsControl
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)));
}
}
}
然后派生的Canvas
控件如下:
public sealed class Canvas : GraphicsControl
{
// subscribe to MousedMoved event
}
响应鼠标移动的区域位于屏幕左上角(0,0)。整个区域与预期的基本控件重叠,但不是Docked,填充Parent控件。见图:
谁能告诉我自己可能做错了什么?如果需要其他代码,请询问。
此外,MSDN seems to reference ClientRectangle
作为要使用的属性。
答案 0 :(得分:0)
看起来我在这里发现了问题:
graphicsDeviceService =
GraphicsDeviceService.AddReference
(
Handle,
new System.Drawing.Size ( ClientSize.Width,
ClientSize.Height )
);
由于GraphicControl派生自Control
,base.Handle
传递基本控件类的句柄而不是拥有System.Windows.Forms.Form
的句柄。一旦拥有Form的Handle传递给graphicsDeviceService,鼠标就会完美地跟踪。
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 ( );
}
}