开发者

Drawing lines/squares with c#

开发者 https://www.devze.com 2023-02-14 10:21 出处:网络
I am working on a Windows application for one customer. He wants to be able to enter the height, width and the length of a cardboard box, and then have the application automatically draw the cardboar

I am working on a Windows application for one customer. He wants to be able to enter the height, width and the length of a cardboard box, and then have the application automatically draw the cardboard box on the screen. I want to know if that's possible to be done with C#. Here is a sample how the cardboard box should look alike:

Drawing lines/squares with c#

Please let m开发者_运维问答e know your opinion and if anyone else did something similar in the past. Thanks in advance for any help.


I haven't done anything like that, but it's definitely possible in C#. Looks like your box needs two sides that are width * height and two sides that are length * height, and the flaps all need to be either width / 2 or length / 2 (long / tall), whichever is shorter. Everything you need for drawing should be in the System.Drawing namespace, including Rectangle and Brush.


Some code to draw a rectangle on a Form (same works for UserControl):

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);       
    }

    private void Form1_Paint(object sender, PaintEventArgs e)   
    {
        e.Graphics.DrawRectangle(Pens.Black, 10, 10, 100, 100);
    }
}

Look at the other e.Graphs.Draw* methods. Combine these to make your work of art!


Sure. You want methods in the System.Drawing namespace, coupled with the CreateGraphics() method of most Winform controls.

The basic setup is to put some control, like a Panel or a PictureBox, on your form. When it comes time to draw the box, you'll use the CreateGraphics() method of that control to get an object representing the space, the same size on screen as the control, in which you can draw. Then, you'll draw lines and/or rectangles in the proper shapes on the screen (I'll leave you the math of figuring out how the box would break down into connected cardboard panels). Look up the MSDN docs for DrawRect, and also for Brush (which you will use to determine the appearance of the lines).

EDIT: OK, I'll give you the math. For a box with length X, height Y, depth Z, the panels break down as follows:

 --------- ------------- --------- ------------- 
|  Zx.5Z  |    Xx.5Z    |  Zx.5Z  |    Xx.5Z    |
|---------+-------------+---------+-------------+
|         |             |         |             |
|   ZxY   |     XxY     |   ZxY   |     XxY     |
|         |             |         |             |
|---------+-------------+---------+-------------+
|  Zx.5Z  |    Xx.5Z    |  Zx.5Z  |    Xx.5Z    |
 --------- ------------- --------- -------------
0

精彩评论

暂无评论...
验证码 换一张
取 消