只有一个Paint事件被调用 [英] Only one Paint event is being called
问题描述
我的问题是,我有8 pictureboxes,只有其中一个得到他们称为一次抽签方法。
My problem is that I have 8 pictureboxes and only one of them is getting their draw method called at a time.
我的代码是有点偏大,所以我试着将它缩小到受影响的部分,我可以为多。
My code is a bit too large so I tried to narrow it down to the affected portions as much as I could.
我最好的猜测是,它是没有这么多我的代码中的错误,但在平局的事件是如何工作的误解。
My best guess is that it's not so much a bug in my code, but a misunderstanding in how the draw event works.
我有一个从我使用的,而不是图片框图片框继承的类。唯一的变化是构造
I have a class which inherits from PictureBox which I am using instead of Picturebox. The only change is the constructor
using System.Drawing;
using System.Windows.Forms;
public class DrawingSurface : PictureBox
{
public DrawingSurface(Rectangle bounds) : base() {
base.Dock = DockStyle.Fill;
base.SizeMode = PictureBoxSizeMode.Zoom;
//I set the size and bounds which is probably redundant because
//I was trying random things to fix the problem
base.Size = new Size(bounds.Width, bounds.Height);
base.Bounds = bounds;
base.Margin = new Padding(0) ;
base.BackColor = Color.Transparent;
}
}
public class MyForm:Form
{
public MyForm():base()
{
base.FormBorderStyle = FormBorderStyle.Sizable;
base.MaximizeBox = false;
base.MinimizeBox = false;
base.Bounds = Screen.PrimaryScreen.Bounds;
}
}
下面是我的主类。
class Program
{
static int Main()
{
short boardOffsetX, boardOffsetY, trayOffsetX, trayOffsetY;
MyForm gameImage = null;
Tray playerTray = null;
ScrabbleBoard board = null;
BagOfLetters bag = null;
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);//this line of code doesn't work either if any of you can spot anything obvious here.
//this is my hacky way of centering the images, wouldn't mind you telling me
//a better way of doing this either.
boardOffsetX = (short)(Screen.PrimaryScreen.Bounds.Width/4);
boardOffsetY = (short)(Screen.PrimaryScreen.Bounds.Height/8);
trayOffsetX = (short)(Screen.PrimaryScreen.Bounds.Width / 3.3);
trayOffsetY = (short)(Screen.PrimaryScreen.Bounds.Height / 24);
try
{
gameImage = new MyForm();
bag = new BagOfLetters();
board = new ScrabbleBoard(gameImage, new Point(boardOffsetX, boardOffsetY));
playerTray = new Tray(bag, trayOffsetX, trayOffsetY, gameImage);
gameImage.ShowDialog();
}
finally
{
//Dispose all
}
}
}
我的图片绘制这样。我有一个大的图像一个大的类持有的7小班和自己的形象。我尝试初始化一切从我的大类
My images are drawn like this. I have one large class with a large image holds the 7 smaller classes and their images. I try to initialize everything from my big class.
public class Tray{
private Image TrayImage;
private DrawingSurface TraySurface;
private short OffsetY = 0;
private short OffsetX = 0;
public List<LB> Tray;
public Tray(Bag bag, short offsetX, short offsetY, MyForm gameImage)
{
Letter tmp;
//paramater validation
TrayImage = Image.FromFile(PATHOFTRAY);
//GetBoundsAsRectangle is just getting the image dimensions as a rectangle
TraySurface = new DrawingSurface(GetBoundsAsRectangle()) ;
TraySurface.Location = new Point(offsetX, offsetY);
this.OffsetX = offsetX;
this.OffsetY = offsetY;
do
{
tmp = bag.PullLetter();
}
while (AddLetter(tmp, gameImage));
//make this image draw
TraySurface.Paint += new PaintEventHandler(this.Draw);
gameImage.Controls.Add(TraySurface);
TraySurface.SendToBack();
}
public bool AddLetter(Letter letter, MyForm gameImage) {
//argument validation
Count++;
letter.PutOnTray(Count, OffsetX, OffsetY);
gameImage.Controls.Add(letter.GetDrawingSurface());
if (letterCount >= MAXLETTERS)
{
return false;
}
return true;
}
public void Paint(Object sender, PaintEventArgs drawEvent)
{
//paramater validation here
Rectangle rectangleAreaToDrawImage = new Rectangle(OffsetX,
OffsetY,
TrayImage.Width,
TrayImage.Height);
// Draw image to screen.
drawEvent.Graphics.DrawImage(TrayImage, rectangleAreaToDrawImage);
}
}
public class Letter
{
private Image LetterImage;
private DrawingSurface LetterSurface;
private int PositionOnTray;
public Letter(char value, String fName) {
LetterImage = Image.FromFile(fName);
LetterSurface = new DrawingSurface(GetBoundsAsRectangle());
}
public void PutOnTray(short position, short x, short y)
{
//validation
PositionOnTray = position;
TrayOffsetX = (short)(x + (position*20));
TrayOffsetY = y;
IsOnTray = true;
LetterSurface.Location = new Point(TrayOffsetX, TrayOffsetY);
LetterSurface.Paint += new PaintEventHandler(this.Draw);
}
public void Paint(Object sender, PaintEventArgs drawEvent)
{
int x = 0, y = 0;
//paramater validation here
x = (TrayOffsetX + (LetterImage.Width * PositionOnTray));
y = (TrayOffsetY + 6);
Location = new Rectangle(x,
y,
LetterImage.Width,
LetterImage.Image.Height);
drawEvent.Graphics.DrawImage(LetterImage, Location);
}
所以基本上托盘具有图像和7个字母也有图像。这些信件现在绘制在托盘的顶部,但在自己的pictureboxes。我添加了一个平局的方法到系统托盘和信,并添加这些到pictureboxes涂料啄。我需要给它们单独pictureboxes因为后来我想添加的功能,让他们被拾起,并用鼠标点击在屏幕上拖动。
So basically the tray has an image and the 7 letters also have images. The letters get drawn on top of the tray for now, but in their own pictureboxes. I added a draw method to tray and letter and added those to the pictureboxes Paint thingy. I need the letters to have seperate pictureboxes because later on I wanna add the functionality to have them be picked up and dragged around the screen with a mouse click.
的问题是,涂料的方法不被调用。只是理论明智的伪码,你会怎么去画这个?我是从Java / C / C ++的背景和图形是不是我对焦点,所以我敢肯定,而不是一个错误,它的我怎么会对此这是错误的。
The problem is that the paint methods aren't being called. Just theory wise with psuedocode, how would you go about painting this? I am from a Java/C/C++ background and graphics were never my point of focus, so I am sure that rather than a bug, its how I am going about this that's wrong.
推荐答案
解决的办法是删除所有pictureboxes ......所有这些事件,可以直接添加到窗体没有问题。我原本想,我需要通过pictureboxes一切提供了不同的处理器,但是我错了。这似乎Pictureboxes的情况下部分除了当你把它们添加到一个TableLayoutPanel像极少数情况下几乎没有使用。
The solution was to remove all the pictureboxes... All those events could be added directly to the form without issue. I was originally thinking that I needed separate handlers provided through the pictureboxes for everything, but I was wrong. It seems the event portion of Pictureboxes is pretty much unused except in rare instances like when you add them to a tablelayoutpanel.
这篇关于只有一个Paint事件被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!