2013-06-20 21 views
1

好吧,所以我需要在c#中做一個簡單的動畫作爲加載圖標使用。這個工作都很好,好,所以讓我們這個廣場爲例圖形從一個類到表格

PictureBox square = new PictureBox(); 
    Bitmap bm = new Bitmap(square.Width, square.Height); 
    Graphics baseImage = Graphics.FromImage(bm); 
    baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100); 
    square.Image = bm; 
與我做我的動畫,一切都在這裏工作

所以,後來我意識到,我需要我的動畫是在一個類,所以我可以從我的合作者計劃中調用它來使用動畫。這是問題出現的地方,我做了我的課程,我做了所有事情,但是在一個班級而不是表格中,然後我從表格中調用我的班級,但屏幕是空白的,沒有動畫。有什麼需要通過才能做到這一點?

namespace SpinningLogo 
{//Here is the sample of my class 
    class test 
    { 
     public void square() 
     { 
      PictureBox square = new PictureBox(); 
      Bitmap bm = new Bitmap(square.Width, square.Height); 
      Graphics baseImage = Graphics.FromImage(bm); 
      baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100); 
      square.Image = bm; 
     } 

    } 
} 
private void button1_Click(object sender, EventArgs e) 
{//Here is how I call my class 
    Debug.WriteLine("11"); 
    test square = new test(); 
    square.square(); 
} 
+1

我沒有看到你在哪裏顯示'PictureBox'。作爲一個孩子控制之前,它是「在形式上」嗎?但現在你沒有這樣做? – DonBoitnott

+0

您是試圖在窗體上新建一個「PictureBox」還是在現有的「PictureBox」上寫? – CodeCamper

回答

1

通過你的test類的PictureBox是窗體上的一個參考:

namespace SpinningLogo 
{ 
    class test 
    { 
     public void square(PictureBox thePB) 
     { 
      Bitmap bm = new Bitmap(thePB.Width, thePB.Height); 
      Graphics baseImage = Graphics.FromImage(bm); 
      baseImage.DrawRectangle(Pens.Black, 0, 0, 100, 100); 
      thePB.Image = bm; 
     } 

    } 
} 

private void button1_Click(object sender, EventArgs e) 
{ 
    test square = new test(); 
    square.square(myPictureBox); //whatever the PictureBox is really named 
} 

您也可以通過Form本身(使用this),但你仍然不得不ID的PictureBox控制(我假設)。

0

您應該傳遞給您的測試類Form實例,而不是在測試類中定義PictureBox。 PictureBox應該是Form的字段,通過Form實例你將可以訪問你的PictureBox。

相關問題