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