Forma de Ventana

17/07/2003 - 15:27 por Francisco Fagas | Informe spam
Hola alguien me podría decir como le cambio la forma a mi
ventana para que no sea la tipica ventana rectangular,
sino darle la forma que yo quiera a mi ventana de
formulario.
Gracias...
 

Leer las respuestas

#1 Daniel M
18/07/2003 - 15:16 | Informe spam
Tengo esto en ingles, aver si te sirve:







Changing the Shape of a Window

Why should windows always be square? One of the great things about .NET is
that it is easy

to make visual objects, such as Windows forms, any shape that you would
like. For example,

an oval window might make users think your application is really cool! The
trick is to assign a

GraphicsPath object to the Form's Region property. The GraphicsPath object
can be defined

as almost any kind of shape.

Let's do this with the form we've been working with in the message box
example. To start, at

the top of the form module, import the System.Drawing.Drawing2D namespace to
make

referring to the Drawing2D library members easier:

using System.Drawing.Drawing2D

Making a Square Window Round

Next, within the form class code, create a procedure, ApplyInitialRegion.
Within

ApplyInitialRegion, define a GraphicsPath object using its AddEllipse
method, and

assign it to the Region property of the Form object.

private void ApplyInitialRegion() {

GraphicsPath myGraphicsPath = new GraphicsPath();

myGraphicsPath.AddEllipse(new Rectangle(0, 0, 600, 450));

this.Region = new Region(myGraphicsPath);

}

All that remains is to create a way to call the form, which you can do in a
button Click event,

as shown in Listing 3.9. When the click event is run, the form turns into an
oval (Figure 3.15).

Listing 3.9: Making a Square Form Oval

...

using System.Drawing.Drawing2D;

...

private void ApplyInitialRegion() {

GraphicsPath myGraphicsPath = new GraphicsPath();

myGraphicsPath.AddEllipse(new Rectangle(0, 0, 600, 450));

this.Region = new Region(myGraphicsPath);

}

private void btnCircle_Click(object sender, System.EventArgs e) {

ApplyInitialRegion();

}

...

Figure 3.15: The Region property of the form can be used to make it any
shape you'd like.

Warning Depending on the shape you choose for your form, it may not have the
normal

mechanisms for closure, as in the example here, because the top part of form
has

been eliminated. Make sure that you provide a way to close the form (such as
a

Close button).

You can open a form with whatever shape you'd like by placing the call to
the method that

changes the form's shape-in this example, ApplyInitialRegion()-in the form
load event or in

the form's constructor.

Constructors

A constructor is a block of code that is executed when an object that is an
instance of a class

is created by using the new keyword. In C#, the constructor is a public
procedure within the

class with the same name as the class; for example, for the Form1 class (see
Listing 3.1), you

can find the constructor within the class:

public class Form1 : System.Windows.Forms.Form

{

...

public Form1()

{

...

}

...

}

You'll find more about constructors in Chapter 8.

A Polygon Form

By way of variation, let's turn the form into a cute polygon, rather than an
oval. To do this, we

need to create an array of points:

Point[] myArray = {

new Point(230, 200),

new Point(400, 100),

new Point(570, 200),

new Point(500, 400),

new Point(300, 400)

};

Next, instead of calling the AddEllipse method for the GraphicsPath, we
should call the

AddPolygon method, with the array of points as the argument:

myGraphicsPath.AddPolygon(myArray);

Listing 3.10 shows the revised ApplyInitialRegion() procedure; if you run
it, the form will be

a nice polygon, like the one in Figure 3.16.

Listing 3.10: The Form Is a Nice Polygon

private void ApplyInitialRegion() {

GraphicsPath myGraphicsPath = new GraphicsPath();

Point[] myArray = {

new Point(230, 200),

new Point(400, 100),

new Point(570, 200),

new Point(500, 400),

new Point(300, 400)

};

myGraphicsPath.AddPolygon(myArray);

this.Region = new Region(myGraphicsPath);

}

Figure 3.16: Calling the AddPolygon method produces a polygon path from the
array of

points you provide.






"Francisco Fagas" escribió en el mensaje
news:0b8a01c34c67$2e2401c0$
Hola alguien me podría decir como le cambio la forma a mi
ventana para que no sea la tipica ventana rectangular,
sino darle la forma que yo quiera a mi ventana de
formulario.
Gracias...

Preguntas similares