你好這可能是一個愚蠢的問題,但我無法弄清楚這裏的問題..這裏是我的代碼填寫的表格與單塊:表單打開時如何繪製?
private void drawBackground()
{
Graphics g = genPan.CreateGraphics();
Image Block = Image.FromFile(@"C:\Users\Administrator\Desktop\movment V1\movment V1\images\BrownBlock.png");
float recWidth = Block.Width;
// rectangle width didnt change the name from previous code, it's picture width.
float recHeight = Block.Height;
// rectangle Heightdidnt change the name from previous code, it's picture Height.
float WinWidth = genPan.Width; // genPan is a panel that docked to the form
float WinHeight = genPan.Height;
float curWidth = 0; //indicates where the next block will be placed int the X axis
float curHeight = 0;//indicates where the next block will be placed int the Y axis
while ((curHeight + recHeight) <= WinHeight)
{
if (curWidth >= WinWidth/3 || curWidth <= WinWidth/1.5 ||
curHeight >= WinHeight/3 || curHeight <= WinHeight/1.5)
{
g.DrawImage(Block, curWidth, curHeight, recWidth , recHeight);
}
curWidth += recWidth;
if ((WinWidth - curWidth) < recWidth)
{
curWidth = 0;
curHeight += 50;
}
}
}
如果我啓動本功能,通過一個按鈕,它將工作得很好。但是,如果我在InitializeComponent()之後啓動func,方法在構造函數中或在FORM顯示的事件中,而按鈕仍然在窗體上,它將執行func,但塊背景不會是可見的,但灰色將是。但如果我刪除按鈕的背景將是可見的。 = \
我不明白爲什麼會發生,如何解決它,以及我在做什麼錯誤..任何人都可以解釋請.. ..?
你有沒有試過OnLoad方法? – Sayse