Rounded rectangles in GDI +

Source: Internet
Author: User

GDI + is an exciting extension of GDI, but for some reason, it doesn't provide a function to draw rounded rectangles, although we can use functions in GDI in GDI +, but we can't use new features such as textures and transparent colors in GDI + in rounded rectangles. What to do? GDI + has a GraphicsPath class that we can use to construct various shapes and fill them with any brush. The source code provides a function that can be added to any project and uses it to draw a rounded rectangle, which can be set by a slider bar, and the function is as follows:

graphicspath* Makeroundrect (Point topleft, point BottomRight, INT percentagerounded)
{
ASSERT (percent agerounded >= 1 && percentagerounded <= 100);
INT left = min (topleft.x, bottomright.x);
INT right = max (topleft.x, bottomright.x);
INT top = min (Topleft.y, BOTTOMRIGHT.Y);
INT bottom = max (Topleft.y, BOTTOMRIGHT.Y); 
INT OffsetX = (right-left) *percentagerounded/100;
INT OffsetY = (bottom-top) *percentagerounded/100;
GraphicsPath pt;
GraphicsPath * Path = pt. Clone ();
Path->addarc (Right-offsetx, Top, OffsetX, OffsetY, 270, 90);
Path->addarc (Right-offsetx, Bottom-offsety, OffsetX, OffsetY, 0, 90);
Path->addarc (left, bottom-offsety, OffsetX, OffsetY, 90, 90);
Path->addarc (left, top, OffsetX, OffsetY, 180, 90);
Path->addline (left + OffsetX, top, RIGHT-OFFSETX/2, top);
return path;
}

Something interesting to note here, I started with new to create the Gaphicspath object, but because GDI + used some ingenious methods to prevent the creation of the Gaphicspath object with new (try it), I created a local gaphicspath variable, Using its Clone method to generate the Gaphicspath object, I'm puzzled that, in order to be consistent with other classes in GDI +, it's best to use the method in GDI + instead of new when you create GDI + objects through pointers, I guess it's because of the others. NET language has no pointers.

That's what I'm going to say about this code, which has a design constraint--Creating an object in a function, and by the caller to remove, GDI + does not allow us to return a path object, only with pointers, I guess this and GDI + 's "return each class reference", but this is just guessing, for fun, You can add something else to the path-it's going to be fun.

Note: The author is familiar with C++/mfc, familiar with gdi/gdi+, English document translation, and do document translation, small system development, technical difficulties breakthrough, email:realman1981@sohu.com, Tel: 13679278016

This article supporting source code

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.