0

私はから派生GraphicsControlControlます:

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使用するプロパティとして参照しているようです。

4

1 に答える 1

0

ここで問題が見つかったようです:

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 ( );
    }

}
于 2011-01-20T02:38:37.837 に答える