C#绘制矩形窗口表单应用程序 [英] C# Drawing rectangle window form application

查看:86
本文介绍了C#绘制矩形窗口表单应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我需要在窗体应用程序(比如大约5个矩形)上绘制一个不同大小的矩形。当按下按钮时,我需要将矩形从最小到最大排序。请帮助。

Hi All,

I need to draw a rectangle on the window form application(say about 5 rectangles) different sizes. I need to sort the rectangle from smallest to biggest when a button is pressed. Please help.

推荐答案

绘制矩形很容易。

处理Paint事件(将Panel放在上面可能是个好主意然后处理Paint事件 - 这样你就可以在面板中绘制矩形,而不是试图绘制你可能拥有的任何其他控件,这是很难做到的。

然后在处理程序中:

Drawing a rectangle is easy.
Handle the Paint event (it's probably a good idea to put a Panel on the form, then handle the Paint event for that - that way you will draw the rectangles within the panel, rather than trying to draw over any other controls you may have, which is a lot harder to do).
Then in the handler:
private void myDrawingPanel_Paint(object sender, PaintEventArgs e)
    {
    Graphics g = e.Graphics;
    g.DrawRectangle(Pens.Red, new Rectangle(10, 10, 100, 75));
    }

其中(10,10)是矩形左上角的位置,with为100,高度为75.



如果您需要5个矩形,则调用DrawRectangle 5次,位置和大小不同。

Where (10, 10) is the location of the top left corner of the rectangle, and the with is 100, the height 75.

If you need 5 rectangles, then call DrawRectangle 5 times, with different locations and sizes.


启动项目涉及:

< br $>
1)启动Visual Studio。

2)创建一个新项目。

我的VS显示一个启动页面,提供创建一个快捷方式项目。

或者在菜单中查看您最有可能期望的功能。

3)出现对话框。在那里选择创建一个Windows窗体应用程序。

4)VS为你创建一些文件并打开Form1的设计器视图。

5)拖出一个面板将工具箱放到Form1上。它变为Panel1。

6)编辑Panel1的属性:

从属性选项卡切换到事件选项卡。

双击Panel1的画事件。

7VS为你创建一个事件处理程序并切换到它的代码视图。

8)应用OriginalGriff的 解决方案





由于排序而改变矩形的位置是可能,但需要另一层间接:您必须将矩形数据从硬编码更改为Paint()到您可以根据情况更改的数据结构并更改Paint()本身,以便它可以使用该数据结构。



创建列表 < 大小>保持数据的类级别。

更改Paint()以使用列表。
Starting the project involves this:

1) Fire up Visual Studio.
2) Create a new Project.
My VS shows a start page that offers a shortcut to create a project.
Or look in the menu where you would most likely expect that functionality.
3) A dialogue appears. In that choose to create a Windows Forms Application.
4) VS creates some files for you and opens the designer view for "Form1".
5) Drag a panel out of the toolbox onto Form1. It becomes "Panel1".
6) Edit Panel1's properties:
Switch from properties tab to events tab.
Double-click on Panel1's "Paint" event.
7) VS creates an event handler for you and switches to its code view.
8) Apply OriginalGriff's solution.


Changing the rectangles' locations as a result of sorting is possible, but needs another layer of indirection: You have to change the rectangle data from being hard-coded into Paint() to a data structure that you can change according to circumstances and change Paint() itself so it can work with that data structure.

Create a List<Size> on class level that holds your data.
Change Paint() to use the list.
List<System.Drawing.Size> _rectangleData = new List<System.Drawing.Size>;

private void Panel1_Paint(object sender, PaintEventArgs e)
{
    for(int i = 0; i < _rectangleData.Count; i++)
    {
        System.Drawing.Point location = new System.Drawing.Point(10, 20 + 50 * i);
        System.Drwaing.Size size = _rectangleData[i];
        e.Graphics.DrawRectangle(Pens.Red, new System.Drawing.Rectangle(location, size));
    }
}





现在,由于 _rectangleData中没有数据,因此不会显示任何内容的。由于我不知道你从哪里得到它,这里是一个在创建时填写列表的例子:



For now, this will draw nothing because there is no data in _rectangleData. Since I have no idea where you'll get it from, here's an example of filling the list right on creation:

List<System.Drawing.Size> _rectangleData = new List<System.Drawing.Size>
{
    new System.Drawing.Size(10, 20),
    new System.Drawing.Size(30, 40)
}





现在播放周围的位置算法,直到它符合您的需求。



排序列表可以通过 List.Sort(比较< T>) [ ^ ]方法。为此,您需要实现自定义比较。在MSDN的比较< T> [ ^ ]。请注意, T 目前意味着尺寸

[/编辑]



Now play around with the location algorithm until it fits your needs.

Sorting the list can be done via the List.Sort(Comparison<T>)[^] method. For that you need to implement your custom comparison. An example is given on MSDN's page on Comparison<T>[^]. Keep in mind that T means Size for now.
[/Edit]


这篇关于C#绘制矩形窗口表单应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆