問題:如何在面板上繪製矩形,而不是在表單上繪製矩形。這裏是我的代碼如下所示:在面板C#上繪製矩形表格
/*
* based on a some flags i determine which shape i want to draw.
* All shapes are stored in a list. I loop through the list
* and call each shape specific draw method - as shown below:.
*
*/
namespace myDrawProgram
{
private void panelArea_Paint(object sender, PaintEventArgs e)
{
if (drawWithPaint == true)
{
Pen p = new Pen(Color.Blue);
p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
if (IsShapeRectangle == true)
{
e.Graphics.DrawRectangle(p, rect);
}
else if (IsShapeCircle == true)
{
e.Graphics.DrawEllipse(p, rect);
}
}
foreach (Shapes shape in listOfShapes)
{
shape.Draw(e.Graphics);
}
}
}
/*
* In another file i have my class which deals with
* drawing rectangles. It is as follows:
*
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SETPaint;
namespace myDrawProgram
{
class TheRectangles : Shapes
{
public Rectangle MyRect { set; get; }
public TheRectangles(Rectangle rect, Color colour, Color boarderColour, Int32 brushThickness)
: base(colour, boarderColour, brushThickness)
{
MyRect = rect;
}
public override void Draw(Graphics g)
{
base.Draw(g);
g.FillRectangle(new SolidBrush(Shapes.c), MyRect);
g.DrawRectangle(new Pen(bc, MyBrushThickness), MyRect);
}
}
}
我假設我需要做的是這樣的:使用
(圖形G = this.panel1.CreateGraphics()){}
我只是不知道如何實現這與我的代碼...
你是說你想把你的面板類的繪圖邏輯,而不是在你的窗體?事實上,你的問題對我來說毫無意義。 –
我不想在窗體上繪製矩形。我希望它在面板上顯示出來 - 目前,當我在面板上畫畫時,什麼也沒有顯示出來......這有道理嗎?希望如此.. – BigBug
你看看這個:[http://stackoverflow.com/questions/282838/drawing-on-top-of-controls-inside-a-panel-c-winforms](http://stackoverflow .com/questions/282838/drawing-on-top-of-controls-inside-a-panel-c-winforms) – Udomaki