如何在 XNA 游戏中获得 IBeam 光标?

发布于 2024-09-07 23:43:14 字数 831 浏览 9 评论 0原文

我已经设法让光标在一个简单的 XNA“游戏”中更改为 IBeam,更新为:

protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        if (Keyboard.GetState().IsKeyDown(Keys.A))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Arrow;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.I))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.IBeam;
        }

        base.Update(gameTime);
    }

按下“I”时,鼠标更改为 IBeam 光标,但当您移动鼠标时,立即更改回箭头。有没有办法让它保持默认的 Windows IBeam,或者我需要创建和跟踪自定义光标?

[编辑] 我还应该指出,每帧设置光标都会导致鼠标移动时光标闪烁。似乎 XNA(或 Windows)在每帧内部将光标重置为箭头?

I've managed to get the cursor to change to an IBeam in a trivial XNA 'Game' with Update as:

protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here

        if (Keyboard.GetState().IsKeyDown(Keys.A))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Arrow;
        }
        if (Keyboard.GetState().IsKeyDown(Keys.I))
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.IBeam;
        }

        base.Update(gameTime);
    }

The mouse changes to the IBeam cursor when 'I' is pressed, but immediately changes back to an arrow when you move the mouse. Is there a way to get it to stay as the default Windows IBeam, or will I need to create and track a custom cursor?

[EDIT] I should also point out that setting the cursor every single frame causes it to flicker when the mouse is moved. It seems the XNA (or Windows) is internally resetting the cursor to an arrow every frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-09-14 23:43:14

在手动绘制之前,我会尝试设置底层 System.Windows.Forms.Form 的 Cursor 属性:

Form f = (Form)Control.FromHandle(Game.Window.Handle);
f.Cursor = System.Windows.Forms.Cursors.IBeam;

由于我现在没有安装 XNA,所以我不知道这是否可行,或者是否可行它将是永久性的,但我不明白为什么不会。

Before resorting to drawing it manually, I'd try to set the Cursor property of the underlying System.Windows.Forms.Form:

Form f = (Form)Control.FromHandle(Game.Window.Handle);
f.Cursor = System.Windows.Forms.Cursors.IBeam;

As I don't have XNA installed right now, I don't know if this will work, or if it will be permenant, but I don't see why it wouldn't.

半衾梦 2024-09-14 23:43:14

听起来你必须手动绘制它。它应该不会太难 - 只需通过 SpriteBatch 在光标位置绘制一个精灵即可。

// ex.
protected override void Draw(GameTime gameTime)
{
    // Other stuff...

    base.Draw(gameTime);

    // Retrieve mouse position
    MouseState mState = Mouse.GetState();
    Vector2 mousePos = new Vector2(mState.X, mState.Y);

    // Use this instead to optionally center the texture:
    // Vector2 mousePos = new Vector2(mState.X - cursorTexture.Width / 2, mState.Y - cursorTexture.Height / 2);

    // Draw cursor after base.Draw in order to draw it after any DrawableGameComponents.
    this.spriteBatch.Begin(); // Optionally save state
    this.spriteBatch.Draw(cursorTexture, mousePos, Color.White);
    this.spriteBatch.End();
}

这是一些提取光标图像的代码。请记住,您将需要对 System.Drawing 的额外引用。

protected override void LoadContent()
{
    // Other stuff...

    // The size of the cursor in pixels.
    int cursorSize = 32;

    // Draw the cursor to a Bitmap
    Cursor cursor = Cursors.IBeam;
    Bitmap image = new Bitmap(cursorSize, cursorSize);
    Graphics graphics = Graphics.FromImage(image);
    cursor.Draw(graphics, new Rectangle(0, 0, cursorSize, cursorSize));

    // Extract pixels from the bitmap, and copy into the texture.
    cursorTexture = new Texture2D(GraphicsDevice, cursorSize, cursorSize);
    Microsoft.Xna.Framework.Graphics.Color[] data = new Microsoft.Xna.Framework.Graphics.Color[cursorSize * cursorSize];
    for (int y = 0; y < cursorSize; y++)
    {
        for (int x = 0; x < cursorSize; x++)
        {
            System.Drawing.Color color = image.GetPixel(x, y);
            data[x + y * cursorSize] = new Microsoft.Xna.Framework.Graphics.Color(color.R, color.G, color.B, color.A);
        }
    }
    cursorTexture.SetData<Microsoft.Xna.Framework.Graphics.Color>(data);
}

请记住,这不是我的想法 - 不能保证有效。不过,基本的想法就在那里。

Sounds like your going to have to draw it manually. It shouldn't be too hard - just draw a sprite via SpriteBatch at the location of the cursor.

// ex.
protected override void Draw(GameTime gameTime)
{
    // Other stuff...

    base.Draw(gameTime);

    // Retrieve mouse position
    MouseState mState = Mouse.GetState();
    Vector2 mousePos = new Vector2(mState.X, mState.Y);

    // Use this instead to optionally center the texture:
    // Vector2 mousePos = new Vector2(mState.X - cursorTexture.Width / 2, mState.Y - cursorTexture.Height / 2);

    // Draw cursor after base.Draw in order to draw it after any DrawableGameComponents.
    this.spriteBatch.Begin(); // Optionally save state
    this.spriteBatch.Draw(cursorTexture, mousePos, Color.White);
    this.spriteBatch.End();
}

And here's some code to extract the cursor image. Keep in mind you'll need an extra reference to System.Drawing.

protected override void LoadContent()
{
    // Other stuff...

    // The size of the cursor in pixels.
    int cursorSize = 32;

    // Draw the cursor to a Bitmap
    Cursor cursor = Cursors.IBeam;
    Bitmap image = new Bitmap(cursorSize, cursorSize);
    Graphics graphics = Graphics.FromImage(image);
    cursor.Draw(graphics, new Rectangle(0, 0, cursorSize, cursorSize));

    // Extract pixels from the bitmap, and copy into the texture.
    cursorTexture = new Texture2D(GraphicsDevice, cursorSize, cursorSize);
    Microsoft.Xna.Framework.Graphics.Color[] data = new Microsoft.Xna.Framework.Graphics.Color[cursorSize * cursorSize];
    for (int y = 0; y < cursorSize; y++)
    {
        for (int x = 0; x < cursorSize; x++)
        {
            System.Drawing.Color color = image.GetPixel(x, y);
            data[x + y * cursorSize] = new Microsoft.Xna.Framework.Graphics.Color(color.R, color.G, color.B, color.A);
        }
    }
    cursorTexture.SetData<Microsoft.Xna.Framework.Graphics.Color>(data);
}

Keep in mind this is off the top of my head - not guaranteed to work. The basic idea is there, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文