所以,我有:
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);
}
这应该在加载表单时加载此矩形,但它不会。 为什么?如何正确地写这个?
答案 0 :(得分: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 (无帽子)的堆栈语义,以确保刷子自动处理。