【问题标题】:Drawing in Form_Load在 Form_Load 中绘图
【发布时间】:2012-01-18 00:20:01
【问题描述】:

所以,我有:

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
     DrawBackground();
}
private: System::Void DrawBackground(){ 
     Graphics^ g1=this->CreateGraphics();
     SolidBrush^ p1 = gcnew SolidBrush(System::Drawing::Color::Gray);
     g1->FillRectangle(p1,90,150,600,150);
}

这应该在表单加载时加载这个矩形,但它没有。 为什么 ??这个怎么写?

【问题讨论】:

    标签: forms visual-c++ c++-cli drawing


    【解决方案1】:

    您不能在 Load 事件中绘图,表单还不可见。使用 CreateGraphics() 也不正确。无论你画什么都不会在下一次重画之前存活下来。改写 OnPaintBackground 方法。像这样:

    protected:
        virtual void OnPaintBackground(PaintEventArgs^ e) override {
            __super::OnPaintBackground(e);
            SolidBrush brush(System::Drawing::Color::Gray);
            e->Graphics->FillRectangle(%brush,90,150,600,150);
        }
    

    注意 brush(没有帽子)的堆栈语义的使用,以确保自动处理画笔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-13
      • 2011-12-11
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多