0

我正在尝试将游戏的图像绘制到 C# 的面板上。我没有绘制任何图像,也无法弄清楚为什么永远不会调用此方法:

私人无效playerPanel_Paint(对象发送者,PaintEventArgs e)

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlightOfTheNavigator
{
    public partial class Form1 : Form
    {
        // Load Sprites
        public Bitmap playerShip = new Bitmap(FlightOfTheNavigator.Properties.Resources.testship);

        public Form1()
        {
            InitializeComponent();        
            SetupGame();
        }

        public void SetupGame()
        {
            // Setup Console
            txtConsole.Text = "Loading Ship Bios v3.4.12c ..." + 
                               Environment.NewLine + 
                               "Console Ready" + 
                               Environment.NewLine + 
                               "----------------------------------------------------------------------------------------" + 
                               Environment.NewLine +
                               Environment.NewLine;

            // Setup Basic Weapons
            listWeapons.Items.Add("Pulse Lazers");
            listWeapons.Items.Add("Cluster Missiles");

            // Set Shield Perecentage
            txtShields.Text = "0%";
        }

        private void trackShield_Scroll(object sender, EventArgs e)
        {
            txtShields.Text = "" + trackShield.Value + "%";
        }

        private void playerPanel_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = playerPanel.CreateGraphics();
            g.DrawImage(playerShip, 0, 0,100,100);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Invalidate the panel. This will lead to a call of 'playerPanel_Paint'
            playerPanel.Refresh();
        }
    }
}
4

1 回答 1

1

确保Paint面板的事件附加到playerPanel_Paint方法。

打开 Desinger,选择面板 ( playerPanel),按下F4以调出属性窗口,然后单击属性窗口上方的闪电以显示事件。检查Paint那里的事件。如果为空,请打开下拉菜单并选择playerPanel_Paint方法。

您也可以在代码中执行此操作。将其放入表单的构造函数中InitializeComponent()

this.playerPanel.Paint += PaintEventHandler(playerPanel_Paint);
于 2013-03-20T18:35:18.637 回答