C # GDI + programming (III)

Source: Internet
Author: User
The alpha value of color

Color can also set the alpha value, which is transparency. such as Color.FromArgb (120,255,255,255). The FROMARGB has four parameters, and the first one specifies the alpha value.
The back three is the color value RGB.
The range of alpha values is 0~255,0 for full transparency and 255 for opacity.
Let's look at a translucent brush example:
private void Formpaint (Object sender, PaintEventArgs e)
{
Create path
GraphicsPath path = new GraphicsPath ();
Rectangle rect = new Rectangle (0, 0, 100, 100);
SolidBrush strbrush=new SolidBrush (color.orange);
E.graphics.drawstring ("Abcdefghijk", New Font ("Blackbody", 20f), Strbrush, rect);
Path. AddRectangle (rect);
Create a path paint brush
PathGradientBrush brush = new PathGradientBrush (path);
Center Point Color
Brush. Centercolor = Color.FromArgb (120,255,255,255);
The color on the path (point)
Brush. Surroundcolors = new color[] {Color.FromArgb (120,0,0,0)};
Fill a rectangle with a path paint brush
E.graphics.fillrectangle (brush, rect);
}

If the color of the brush does not have an alpha (transparent) value set, then the displayed string is not visible.

Anti-aliasing

Elimination of line aliasing is done by setting the SmoothingMode property member in the Graphics class, which is an enumeration type.

Example statement for setting anti-aliasing: E.graphics.smoothingmode = Smoothingmode.antialias;

With antialiasing, the lines look much smoother.

The corresponding code is:

Pen pen=new Pen (color.green,2);
Rectangle rect1 = new Rectangle (0, 0, 100, 100);
E.graphics.drawellipse (pen, rect1);
Anti-aliasing
E.graphics.smoothingmode = Smoothingmode.antialias;
100 wide, 100 high
Rectangle rect2 = new Rectangle (100, 0, 100, 100);
E.graphics.drawellipse (pen, rect2);
SmoothingMode enumeration There are other members that can set different degrees of anti-aliasing, or you can set non-antialiasing.

Text anti-aliasing
The text to be displayed is to be anti-aliasing set textrenderinghint on.
Example code:
private void Formpaint (Object sender, PaintEventArgs e)
{
SolidBrush brush = new SolidBrush (color.green);
E.graphics.drawstring ("Abcdefghijkl", New Font ("Arial", 15f), brush, 0, 20);
Anti-aliasing
E.graphics.textrenderinghint = System.Drawing.Text.TextRenderingHint.AntiAlias;
E.graphics.drawstring ("Abcdefghijkl", New Font ("Arial", 15f), brush, 0, 50);
}

Regional

These functions are best understood because C # can also invoke these APIs. And many of the concepts inside are interlinked. For example, a window handle, an area handle.

If the GETHRGN function in the region class, you can get the area handle.

Also talk about the relationship between the region and the path, so that you can better understand the area is what is the case, the relationship between the region and the path is like a graph of the relationship between the fill,

You can use paths and rectangles to create an area. Through the constructor of region.

The following example creates two regions with rectangles and fills the two regions.

private void Formpaint (Object sender, PaintEventArgs e)
{
Two rectangular areas with 100 width and height
Region Region1=new Region (new Rectangle (0,0,100,100));
Region Region2=new Region (new Rectangle (50,50,100,100));
Create a Brush 1, and fill the area 1, the alpha value of the color is 125
SolidBrush brush1 = new SolidBrush (Color.FromArgb (125, 255, 0, 0));
E.graphics.fillregion (BRUSH1, Region1);
Create a Brush 2 and fill the area 2
SolidBrush brush2 = new SolidBrush (Color.FromArgb (125, 0, 255, 0));
E.graphics.fillregion (BRUSH2, Region2);
}

Alternatively, you can merge two regions, and the merge area is also mentioned in the 95th function Combinergn, so you can refer to that function.

Combinergn the last parameter of the API function Ncombinemode indicates how to merge two regions, the merge in the region class should also have a similar function.

Indicates how to merge, but the region class is not indicated by a parameter, and region is called directly to a different function.

For example, Region.intersect function is Rgn_and, region.union is rgn_or way to merge. Region1. Exclude is Rgn_diff,

Region.xor is rgn_xor way to merge

Explanation of the Ncombinemode parameter:

>>>>>>>>>>>>>>>>>>>>>>>>>>> >>> from API Combinergn functions >>>>>>>>>>>>>>>>>>>> >>>>>>>>>>>>>>>>>>>>>>>>>>> >>>

Rgn_and overlapping portions of two regions

Rgn_or combination of two zones

Rgn_diff hSrcRgn1 non-overlapping parts

Rgn_xor HSrcRgn1 and hSrcRgn2 non-overlapping parts

<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<

There is also a function complement in the region class, and the area that is merged with this function is the part of Region 2 that is not overlapping.

Let's look at the example:

private void Formpaint (Object sender, PaintEventArgs e)
{
Two rectangular areas with 100 width and height
Region Region1=new Region (new Rectangle (0,0,100,100));
Region Region2=new Region (new Rectangle (50,50,100,100));
The region is merged with the XOR function, resulting in an area of the nonoverlapping portions of the two regions.
Region1. Xor (Region2);
Create a brush to fill the merged area
SolidBrush brush = new SolidBrush (color.green);
E.graphics.fillregion (brush, Region1);
}

To set the window display area:

Specifying a region attribute member in the form class is OK, such as displaying a circular window:

Public Form1 ()
{
InitializeComponent ();
Create a circular path
GraphicsPath path = new GraphicsPath ();
Path. AddEllipse (0, 0, 100, 100);
Create a region from a path
Region region = new region (path);
Specify window display Area
This. region = region;
}

Region.isvisible determines whether a point (or rectangle) is within a region
Looking at the example, the example creates a circular area that appears red as soon as the mouse enters the circular area.
public partial class Form1:form
{
Paint brushes filled with areas in normal state
Public SolidBrush norbrush=new SolidBrush (Color.green);
Brush that fills the mouse when it is inside the area
Public SolidBrush Hovbrush = new SolidBrush (color.red);
Indicates whether the mouse is within the region
public bool Hovflag = FALSE;
Circular area
public region;
Public Form1 ()
{
InitializeComponent ();
Create a circular path
GraphicsPath path = new GraphicsPath ();
Path. AddEllipse (50, 50, 100, 100);
Create a region from a path
Region = new Region (path);

Add Event Handling
This. Paint + = Formpaint;
Mouse Move Event
This. MouseMove + = Frommousemove;

}
private void Formpaint (Object sender, PaintEventArgs e)
{
if (Hovflag)
{
E.graphics.fillregion (Hovbrush, region);
}
Else
{
E.graphics.fillregion (Norbrush, region);
}
}
Mouse Move event handler function
private void Frommousemove (object sender, MouseEventArgs e)
{
Graphics graphics = this. CreateGraphics ();
Mouse first entry area
if (region. IsVisible (e.x, e.y) && Hovflag! = True)
{
Hovflag = true;
Graphics. FillRegion (Hovbrush, region);
}
The mouse has left the area.
else if (region. IsVisible (e.x, e.y)! = True && hovflag)
{
Hovflag = false;
Graphics. FillRegion (Norbrush, region);
}
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.