我試圖通過類在表單上繪製事物。這是代碼。對象引用對於非靜態字段,方法或屬性是必需的
public static void DrawStatBars()
{
Graphics g = Graphics.FromImage(Graphic.StatBarBackbuffer);
Font fnt = new Font("Microsoft Sans Serif", 8, FontStyle.Bold);
g.DrawImage(Graphic.EmptyHPBar, new Point(12, 12));
g.DrawImage(Graphic.EmptyManaBar, new Point(12, 35));
g.DrawImage(Graphic.EmptyEXPBar, new Point(12, 58));
g.DrawImage(Graphic.HPBar, new Rectangle(12, 15, (int)picHpWidth, Graphic.HPBar.Height), new Rectangle(0, 0, (int)picHpWidth, Graphic.HPBar.Height), GraphicsUnit.Pixel);
g.DrawImage(Graphic.ManaBar, new Rectangle(12, 38, (int)picManaWidth, Graphic.ManaBar.Height), new Rectangle(0, 0, (int)picManaWidth, Graphic.ManaBar.Height), GraphicsUnit.Pixel);
g.DrawImage(Graphic.EXPBar, new Rectangle(12, 61, (int)picEXPWidth, Graphic.EXPBar.Height), new Rectangle(0, 0, (int)picEXPWidth, Graphic.EXPBar.Height), GraphicsUnit.Pixel);
g.DrawString(lblHPText, fnt, new SolidBrush(Color.Black), 40, 15);
g.DrawString(lblManaText, fnt, new SolidBrush(Color.Black), 40, 38);
g.DrawString(lblEXPText, fnt, new SolidBrush(Color.Black), 40, 63);
g.Dispose();
g = frmMainGame.picGeneral.CreateGraphics;
g.DrawImage(Graphic.StatBarBackbuffer, new Point(0, 0));
g.Dispose();
}
問題是g = frmMainGame.picGeneral.CreateGrpahics;
。由於控件不是靜態的,我將如何通過類來訪問它,而不是移動代碼,而必須將代碼重新編碼爲表單本身的代碼。
把它作爲一個參數的函數? – Magnus