Graphicsdevice.viewportが間違った場所にあるのはどうしてですか?
質問
私は導きます 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)にあります。全体の領域は、意図したベースコントロールと重複していますが、ドッキングされておらず、親制御が埋められます。画像を参照してください:
誰かが私が間違っているかもしれないことを教えてもらえますか?追加のコードが必要な場合は、尋ねてください。
また、 MSDNは参照しているようです ClientRectangle
使用するプロパティとして。
解決
ここで問題を見つけたようです:
graphicsDeviceService =
GraphicsDeviceService.AddReference
(
Handle,
new System.Drawing.Size ( ClientSize.Width,
ClientSize.Height )
);
GraphicControlは派生しているためです Control
, base.Handle
所有のハンドルではなく、ベースコントロールクラスのハンドルを渡しています System.Windows.Forms.Form
. 。所有フォームのハンドルが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 ( );
}
}
所属していません StackOverflow