Initial winforms content migrated (#18)

* Merge winforms framework content to working branch (#14)

* Breadcrumb / TOC / Move net5 to net folder (#15)

* change path from net5 to net

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Mess with bread/toc

* Add .net 5 winforms placeholder article (#16)

* added some metadata and adjusted net5 placeholder

* corrections

* corrections

* corrections

* Test1

* Swapping landing page vs concept

* fix links

* Fix links

* Fix desc
This commit is contained in:
Andy De George
2020-09-01 16:26:21 -07:00
committed by GitHub
parent 1a27c9b107
commit c0ba284473
1557 changed files with 129980 additions and 13 deletions
+1 -2
View File
@@ -5,8 +5,7 @@
"build_source_folder": "dotnet-desktop-guide",
"build_output_subfolder": "dotnet-desktop-guide",
"locale": "en-us",
"monikers": [],
"moniker_ranges": [],
"moniker_ranges": ["netframeworkdesktop-4.8", "netdesktop-5.0"],
"filemap_share_depots": [
"VS.dotnet-api-docs"
],
+8 -2
View File
@@ -1,2 +1,8 @@
- name: Index
href: index.md
items:
- name: Windows Forms
expanded: true
items:
- name: ".NET 5 (and .NET Core 3.1)"
href: https://docs.microsoft.com/dotnet/desktop/winforms/?view=netdesktop-5.0
- name: .NET Framework
href: https://docs.microsoft.com/dotnet/desktop/winforms/?view=netframework-4-8
-3
View File
@@ -1,3 +0,0 @@
- name: Docs
tocHref: /
topicHref: /
+103 -5
View File
@@ -16,7 +16,53 @@
"README.md",
"LICENSE",
"LICENSE-CODE",
"ThirdPartyNotices.md"
"ThirdPartyNotices.md",
"samples/**/*.*"
],
"group": "framework",
"src": "framework"
},
{
"files": [
"**/*.md",
"**/*.yml"
],
"exclude": [
"**/obj/**",
"**/includes/**",
"_themes/**",
"_themes.pdf/**",
"**/docfx.json",
"_repo.en-us/**",
"README.md",
"LICENSE",
"LICENSE-CODE",
"ThirdPartyNotices.md",
"samples/**/*.*"
],
"group": "net",
"src": "net"
},
{
"files": [
"**/*.md",
"**/*.yml"
],
"exclude": [
"framework",
"net",
"**/obj/**",
"**/includes/**",
"_themes/**",
"_themes.pdf/**",
"**/docfx.json",
"_repo.en-us/**",
"README.md",
"LICENSE",
"LICENSE-CODE",
"ThirdPartyNotices.md",
"samples/**/*.*"
]
}
],
@@ -33,7 +79,47 @@
"_themes/**",
"_themes.pdf/**",
"**/docfx.json",
"_repo.en-us/**"
"_repo.en-us/**",
"samples/**/*.*"
],
"group": "framework",
"src": "framework"
},
{
"files": [
"**/*.png",
"**/*.gif",
"**/*.jpg"
],
"exclude": [
"**/obj/**",
"**/includes/**",
"_themes/**",
"_themes.pdf/**",
"**/docfx.json",
"_repo.en-us/**",
"samples/**/*.*"
],
"group": "net",
"src": "net"
},
{
"files": [
"**/*.png",
"**/*.gif",
"**/*.jpg"
],
"exclude": [
"framework",
"net",
"**/obj/**",
"**/includes/**",
"_themes/**",
"_themes.pdf/**",
"**/docfx.json",
"_repo.en-us/**",
"samples/**/*.*"
]
}
],
@@ -41,10 +127,12 @@
"externalReference": [],
"globalMetadata": {
"breadcrumb_path": "/dotnet/desktop/breadcrumb/toc.json",
"extendBreadcrumb": true,
"extendBreadcrumb": false,
"feedback_system": "GitHub",
"feedback_github_repo": "dotnet/docs-desktop",
"feedback_product_url": "https://developercommunity.visualstudio.com/spaces/61/index.html"
"feedback_product_url": "https://developercommunity.visualstudio.com/spaces/61/index.html",
"author": "adegeo",
"ms.author": "adegeo"
},
"fileMetadata": {
"titleSuffix": {
@@ -56,6 +144,16 @@
},
"template": [],
"dest": "dotnet-desktop-guide",
"markdownEngineName": "markdig"
"markdownEngineName": "markdig",
"groups": {
"framework": {
"dest": "framework-dest",
"moniker_range": "netframeworkdesktop-4.8"
},
"net": {
"dest": "net-dest",
"moniker_range": "netdesktop-5.0"
}
}
}
}
@@ -0,0 +1,25 @@
items:
- name: Docs
tocHref: /
topicHref: /
items:
- name: .NET
tocHref: /dotnet/
topicHref: /dotnet/index
items:
- name: Desktop Guide
tocHref: /dotnet/desktop
topicHref: /dotnet/desktop/index
items:
- name: .NET Framework
items:
- name: Windows Forms
tocHref: /dotnet/desktop/winforms
topicHref: /dotnet/desktop/winforms/index
items:
- name: Enhance Windows Forms applications
tocHref: /dotnet/desktop/winforms/advanced/
topicHref: /dotnet/desktop/winforms/advanced/index
- name: Controls
tocHref: /dotnet/desktop/winforms/controls/
topicHref: /dotnet/desktop/winforms/controls/index
@@ -0,0 +1,91 @@
---
title: "Additional Security Considerations"
ms.date: "03/30/2017"
helpviewer_keywords:
- "Windows Forms, secure calls to Windows API"
- "security [Windows Forms]"
- "security [Windows Forms], calling APIs"
- "Clipboard [Windows Forms], securing access"
ms.assetid: 15abda8b-0527-47c7-aedb-77ab595f2bf1
---
# Additional Security Considerations in Windows Forms
.NET Framework security settings might cause your application to run differently in a partial trust environment than on your local computer. The .NET Framework restricts access to such critical local resources as the file system, network, and unmanaged APIs, among other things. The security settings affect the ability to call the Microsoft Windows API or other APIs that cannot be verified by the security system. Security also affects other aspects of your application, including file and data access, and printing. For more information about file and data access in a partial trust environment, see [More Secure File and Data Access in Windows Forms](more-secure-file-and-data-access-in-windows-forms.md). For more information about printing in a partial trust environment, see [More Secure Printing in Windows Forms](more-secure-printing-in-windows-forms.md).
The following sections discuss how to work with the Clipboard, perform window manipulation, and call the Windows API from applications that are running in a partial trust environment.
## Clipboard Access
The <xref:System.Security.Permissions.UIPermission> class controls access to the Clipboard, and the associated <xref:System.Security.Permissions.UIPermissionClipboard> enumeration value indicates the level of access. The following table shows the possible permission levels.
|UIPermissionClipboard value|Description|
|---------------------------------|-----------------|
|<xref:System.Security.Permissions.UIPermissionClipboard.AllClipboard>|The Clipboard can be used without restriction.|
|<xref:System.Security.Permissions.UIPermissionClipboard.OwnClipboard>|The Clipboard can be used with some restrictions. The ability to put data on the Clipboard (Copy or Cut command operations) is unrestricted. Intrinsic controls that accept paste, such as a text box, can accept Clipboard data, but user controls cannot programmatically read from the Clipboard.|
|<xref:System.Security.Permissions.UIPermissionClipboard.NoClipboard>|The Clipboard cannot be used.|
By default, the Local Intranet zone receives <xref:System.Security.Permissions.UIPermissionClipboard.AllClipboard> access and the Internet zone receives <xref:System.Security.Permissions.UIPermissionClipboard.OwnClipboard> access. This means that the application can copy data to the Clipboard, but the application cannot programmatically paste to or read from the Clipboard. These restrictions prevent programs without full trust from reading content copied to the Clipboard by another application. If your application requires full Clipboard access but you do not have the permissions, you will have to elevate the permissions for your application. For more information about elevating permissions, see [General Security Policy Administration](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/ed5htz45(v=vs.100)).
## Window Manipulation
The <xref:System.Security.Permissions.UIPermission> class also controls permission to perform window manipulation and other UI-related actions, and the associated <xref:System.Security.Permissions.UIPermissionWindow> enumeration value indicates the level of access. The following table shows the possible permission levels.
By default, the Local Intranet zone receives <xref:System.Security.Permissions.UIPermissionWindow.AllWindows> access and the Internet zone receives <xref:System.Security.Permissions.UIPermissionWindow.SafeTopLevelWindows> access. This means that in the Internet zone, the application can perform most windowing and UI actions, but the window's appearance will be modified. The modified window displays a balloon notification when first run, contains modified title bar text, and requires a close button on the title bar. The balloon notification and the title bar identify to the user of the application that the application is running under partial trust.
|UIPermissionWindow value|Description|
|------------------------------|-----------------|
|<xref:System.Security.Permissions.UIPermissionWindow.AllWindows>|Users can use all windows and user input events without restriction.|
|<xref:System.Security.Permissions.UIPermissionWindow.SafeTopLevelWindows>|Users can use only safer top-level windows and safer subwindows for drawing, and can use only user input events for the user interface within those top-level windows and subwindows. These safer windows are clearly labeled and have minimum and maximum size restrictions. The restrictions prevent potentially harmful spoofing attacks, such as imitating system logon screens or the system desktop, and restricts programmatic access to parent windows, focus-related APIs, and use of the <xref:System.Windows.Forms.ToolTip> control,|
|<xref:System.Security.Permissions.UIPermissionWindow.SafeSubWindows>|Users can use only safer subwindows for drawing, and can use only user input events for the user interface within that subwindow. A control displayed within a browser is an example of a safer subwindow.|
|<xref:System.Security.Permissions.UIPermissionWindow.NoWindows>|Users cannot use any windows or user interface events. No user interface can be used.|
Each permission level identified by the <xref:System.Security.Permissions.UIPermissionWindow> enumeration allows fewer actions than the level above it. The following tables indicate the actions that are restricted by the <xref:System.Security.Permissions.UIPermissionWindow.SafeTopLevelWindows> and <xref:System.Security.Permissions.UIPermissionWindow.SafeSubWindows> values. For exact permissions that are required for each member, see the reference for that member in the .NET Framework class library documentation.
<xref:System.Security.Permissions.UIPermissionWindow.SafeTopLevelWindows> permission restricts the actions listed in the following table.
|Component|Restricted actions|
|---------------|------------------------|
|<xref:System.Windows.Forms.Application>|- Setting the <xref:System.Windows.Forms.Application.SafeTopLevelCaptionFormat%2A> property.|
|<xref:System.Windows.Forms.Control>|- Getting the <xref:System.Windows.Forms.Control.Parent%2A> property.<br />- Setting the `Region` property.<br />- Calling the <xref:System.Windows.Forms.Control.FindForm%2A> , <xref:System.Windows.Forms.Control.Focus%2A>, <xref:System.Windows.Forms.Control.FromChildHandle%2A> and <xref:System.Windows.Forms.Control.FromHandle%2A>, <xref:System.Windows.Forms.Control.PreProcessMessage%2A>, <xref:System.Windows.Forms.Control.ReflectMessage%2A>, or <xref:System.Windows.Forms.Control.SetTopLevel%2A> method.<br />- Calling the <xref:System.Windows.Forms.Control.GetChildAtPoint%2A> method if the control returned is not a child of the calling control.<br />- Modify control focus inside a container control.|
|<xref:System.Windows.Forms.Cursor>|- Setting the <xref:System.Windows.Forms.Cursor.Clip%2A> property.<br />- Calling the <xref:System.Windows.Forms.Control.Hide%2A> method.|
|<xref:System.Windows.Forms.DataGrid>|- Calling the <xref:System.Windows.Forms.ContainerControl.ProcessTabKey%2A> method.|
|<xref:System.Windows.Forms.Form>|- Getting the <xref:System.Windows.Forms.Form.ActiveForm%2A> or <xref:System.Windows.Forms.Form.MdiParent%2A> property.<br />- Setting the <xref:System.Windows.Forms.Form.ControlBox%2A>, <xref:System.Windows.Forms.Form.ShowInTaskbar%2A>, or <xref:System.Windows.Forms.Form.TopMost%2A> property.<br />- Setting the <xref:System.Windows.Forms.Form.Opacity%2A> property below 50%.<br />- Setting the <xref:System.Windows.Forms.Form.WindowState%2A> property to <xref:System.Windows.Forms.FormWindowState.Minimized> programmatically.<br />- Calling the <xref:System.Windows.Forms.Form.Activate%2A> method.<br />- Using the <xref:System.Windows.Forms.FormBorderStyle.None>, <xref:System.Windows.Forms.FormBorderStyle.FixedToolWindow>, and <xref:System.Windows.Forms.FormBorderStyle.SizableToolWindow><xref:System.Windows.Forms.FormBorderStyle> enumeration values.|
|<xref:System.Windows.Forms.NotifyIcon>|- Using the <xref:System.Windows.Forms.NotifyIcon> component is completely restricted.|
The <xref:System.Security.Permissions.UIPermissionWindow.SafeSubWindows> value restricts the actions listed in the following table, in addition to the restrictions placed by the <xref:System.Security.Permissions.UIPermissionWindow.SafeTopLevelWindows> value.
|Component|Restricted actions|
|---------------|------------------------|
|<xref:System.Windows.Forms.CommonDialog>|- Showing a dialog box derived from the <xref:System.Windows.Forms.CommonDialog> class.|
|<xref:System.Windows.Forms.Control>|- Calling the <xref:System.Windows.Forms.Control.CreateGraphics%2A> method.<br />- Setting the <xref:System.Windows.Forms.Control.Cursor%2A> property.|
|<xref:System.Windows.Forms.Control.Cursor%2A>|- Setting the <xref:System.Windows.Forms.Cursor.Current%2A> property.|
|<xref:System.Windows.Forms.MessageBox>|- Calling the <xref:System.Windows.Forms.Form.Show%2A> method.|
### Hosting Third-Party Controls
Another kind of window manipulation can occur if your forms host third-party controls. A third-party control is any custom <xref:System.Windows.Forms.UserControl> that you have not developed and compiled yourself. Although the hosting scenario is hard to exploit, it is theoretically possible for a third-party control to expand its rendering surface to cover the entire area of your form. This control could then mimic a critical dialog box, and request information such as username/password combinations or bank account numbers from your users.
To limit this potential risk, use third-party controls only from vendors you can trust. If you use third-party controls you have downloaded from an unverifiable source, we recommend that you review the source code for potential exploits. After you've verified that the source is non-malicious, you should compile the assembly yourself to ensure that the source matches the assembly.
## Windows API Calls
If your application design requires calling a function from the Windows API, you are accessing unmanaged code. In this case the code's actions to the window or operating system cannot be determined when you are working with Windows API calls or values. The <xref:System.Security.Permissions.SecurityPermission> class and the <xref:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode> value of the <xref:System.Security.Permissions.SecurityPermissionFlag> enumeration control access to unmanaged code. An application can access unmanaged code only when it is granted the <xref:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode> permission. By default, only applications that are running locally can call unmanaged code.
Some Windows Forms members provide unmanaged access that requires the <xref:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode> permission. The following table lists the members in the <xref:System.Windows.Forms> namespace that require the permission. For more information about the permissions that are required for a member, see the .NET Framework class library documentation.
|Component|Member|
|---------------|------------|
|<xref:System.Windows.Forms.Application>|- <xref:System.Windows.Forms.Application.AddMessageFilter%2A> method<br />- <xref:System.Windows.Forms.Application.CurrentInputLanguage%2A> property<br />- `Exit` method<br />- <xref:System.Windows.Forms.Application.ExitThread%2A> method<br />- <xref:System.Windows.Forms.Application.ThreadException> event|
|<xref:System.Windows.Forms.CommonDialog>|- <xref:System.Windows.Forms.CommonDialog.HookProc%2A> method<br />- <xref:System.Windows.Forms.CommonDialog.OwnerWndProc%2A>\ method<br />- <xref:System.Windows.Forms.CommonDialog.Reset%2A> method<br />- <xref:System.Windows.Forms.CommonDialog.RunDialog%2A> method|
|<xref:System.Windows.Forms.Control>|- <xref:System.Windows.Forms.Control.CreateParams%2A> method<br />- <xref:System.Windows.Forms.Control.DefWndProc%2A> method<br />- <xref:System.Windows.Forms.Control.DestroyHandle%2A> method<br />- <xref:System.Windows.Forms.Control.WndProc%2A> method|
|<xref:System.Windows.Forms.Help>|- <xref:System.Windows.Forms.Help.ShowHelp%2A> methods<br />- <xref:System.Windows.Forms.Help.ShowHelpIndex%2A> method|
|<xref:System.Windows.Forms.NativeWindow>|- <xref:System.Windows.Forms.NativeWindow> class|
|<xref:System.Windows.Forms.Screen>|- <xref:System.Windows.Forms.Screen.FromHandle%2A> method|
|<xref:System.Windows.Forms.SendKeys>|- <xref:System.Windows.Forms.SendKeys.Send%2A> method<br />- <xref:System.Windows.Forms.SendKeys.SendWait%2A> method|
If your application does not have permission to call unmanaged code, your application must request <xref:System.Security.Permissions.SecurityPermissionFlag.UnmanagedCode> permission, or you must consider alternative ways of implementing features; in many cases, Windows Forms provides a managed alternative to Windows API functions. If no alternative means exist and the application must access unmanaged code, you will have to elevate the permissions for the application.
Permission to call unmanaged code allows an application to perform most anything. Therefore, permission to call unmanaged code should only be granted for applications that come from a trusted source. Alternatively, depending on the application, the piece of application functionality that makes the call to unmanaged code could be optional, or enabled in the full trust environment only. For more information about dangerous permissions, see [Dangerous Permissions and Policy Administration](https://docs.microsoft.com/dotnet/framework/misc/dangerous-permissions-and-policy-administration). For more information about elevating permissions, see [General Security Policy Administration](https://docs.microsoft.com/previous-versions/dotnet/netframework-4.0/ed5htz45(v=vs.100)).
## See also
- [More Secure File and Data Access in Windows Forms](more-secure-file-and-data-access-in-windows-forms.md)
- [More Secure Printing in Windows Forms](more-secure-printing-in-windows-forms.md)
- [Security in Windows Forms Overview](security-in-windows-forms-overview.md)
- [Windows Forms Security](windows-forms-security.md)
- [Securing ClickOnce Applications](/visualstudio/deployment/securing-clickonce-applications)
@@ -0,0 +1,35 @@
---
title: "Adjust the size and scale"
ms.date: "04/07/2017"
helpviewer_keywords:
- "Windows Forms, changing size"
- "Windows Forms, adjusting size"
- "Windows Forms, changing scale"
ms.assetid: a014d2b3-3273-4521-95e1-00171033bf58
---
# Adjusting the size and scale of Windows Forms
This topic provides links to information about resizing Windows Forms.
## In This Section
[How to: Resize Windows Forms](how-to-resize-windows-forms.md)
Provides instructions for specifying the size of Windows Forms.
[Automatic Scaling in Windows Forms](automatic-scaling-in-windows-forms.md)
Discusses how automatic scaling enables a form and its controls to be displayed appropriately between machines.
[High DPI Support in Windows Forms](high-dpi-support-in-windows-forms.md)
Discusses Windows Forms' support for High DPI and dynamic scaling.
## Reference
<xref:System.Drawing.Size>
Describes this class and has links to all of its members.
<xref:System.Windows.Forms.TableLayoutPanel>
Describes this class and has links to all of its members.
<xref:System.Windows.Forms.FlowLayoutPanel>
Describes this class and has links to all of its members.
## Related sections
[Changing the appearance of Windows Forms](changing-the-appearance-of-windows-forms.md)
Provides links to topics describing other ways to change the appearance of Windows Forms.
@@ -0,0 +1,61 @@
---
title: "About GDI+ Managed Code"
ms.date: "03/30/2017"
helpviewer_keywords:
- "GDI+, about GDI+"
- "GDI+"
- "graphics [Windows Forms], GDI+"
ms.assetid: a98a76ab-e455-49c9-891c-0491ac932f2c
---
# About GDI+ Managed Code
GDI+ is the portion of the Windows operating system that provides two-dimensional vector graphics, imaging, and typography. GDI+ improves on GDI (the Graphics Device Interface included with earlier versions of Windows) by adding new features and by optimizing existing features.
The GDI+ managed class interface (a set of wrappers) is part of the .NET Framework, an environment for building, deploying, and running XML Web services and other applications.
This section provides information about the GDI+ API for programmers using managed code.
## In this section
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
Discusses vector graphics.
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
Discusses the type of images available and how to work with them.
- [Coordinate Systems and Transformations](coordinate-systems-and-transformations.md)
Discusses how to transform graphics with GDI+.
## Reference
- <xref:System.Drawing.Graphics?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Image?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Bitmap?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Imaging.Metafile?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Font?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Brush?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Color?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Drawing.Drawing2D.Matrix?displayProperty=nameWithType>
Describes this class and has links to all its members.
- <xref:System.Windows.Forms.TextRenderer?displayProperty=nameWithType>
Describes this class and has links to all its members.
## Related sections
[Using Managed Graphics Classes](using-managed-graphics-classes.md)\
Contains links to topics that demonstrate how to use the `Graphics` programming interface.
@@ -0,0 +1,37 @@
---
title: "Alpha Blending Lines and Fills"
ms.date: "03/30/2017"
helpviewer_keywords:
- "lines [Windows Forms], adding transparency"
- "examples [Windows Forms], alpha blending"
- "alpha blending [Windows Forms], using with lines"
- "alpha blending"
- "lines [Windows Forms], alpha blending"
- "fills [Windows Forms], alpha blending"
- "alpha blending [Windows Forms], using with fills"
- "shapes [Windows Forms], adding transparency"
ms.assetid: 5440f48c-3ac9-44c3-b170-c1c110bdbab8
---
# Alpha Blending Lines and Fills
In GDI+, a color is a 32-bit value with 8 bits each for alpha, red, green, and blue. The alpha value indicates the transparency of the color — the extent to which the color is blended with the background color. Alpha values range from 0 through 255, where 0 represents a fully transparent color, and 255 represents a fully opaque color.
Alpha blending is a pixel-by-pixel blending of source and background color data. Each of the three components (red, green, blue) of a given source color is blended with the corresponding component of the background color according to the following formula:
displayColor = sourceColor × alpha / 255 + backgroundColor × (255 alpha) / 255
For example, suppose the red component of the source color is 150 and the red component of the background color is 100. If the alpha value is 200, the red component of the resultant color is calculated as follows:
150 × 200 / 255 + 100 × (255 200) / 255 = 139
## In This Section
[How to: Draw Opaque and Semitransparent Lines](how-to-draw-opaque-and-semitransparent-lines.md)
Shows how to draw alpha-blended lines.
[How to: Draw with Opaque and Semitransparent Brushes](how-to-draw-with-opaque-and-semitransparent-brushes.md)
Explains how to alpha-blend with brushes.
[How to: Use Compositing Mode to Control Alpha Blending](how-to-use-compositing-mode-to-control-alpha-blending.md)
Describes how to control alpha blending using <xref:System.Drawing.Drawing2D.CompositingMode>.
[How to: Use a Color Matrix to Set Alpha Values in Images](how-to-use-a-color-matrix-to-set-alpha-values-in-images.md)
Explains how to use a <xref:System.Drawing.Imaging.ColorMatrix> object to control alpha blending.
@@ -0,0 +1,45 @@
---
title: "Antialiasing with Lines and Curves"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "antialiasing"
- "antialiasing [Windows Forms], smoothing modes"
- "GDI+, antialiasing"
ms.assetid: 810da1a4-c136-4abf-88df-68e49efdd8d4
---
# Antialiasing with Lines and Curves
When you use GDI+ to draw a line, you provide the starting point and ending point of the line, but you do not have to provide any information about the individual pixels on the line. GDI+ works in conjunction with the display driver software to determine which pixels will be turned on to show the line on a particular display device.
## Aliasing
Consider the straight red line that goes from the point (4, 2) to the point (16, 10). Assume the coordinate system has its origin in the upper-left corner and that the unit of measure is the pixel. Also assume that the x-axis points to the right and the y-axis points down. The following illustration shows an enlarged view of the red line drawn on a multicolored background.
![Line, no antialiasing](./media/aboutgdip02-art33.gif "AboutGdip02_Art33")
The red pixels used to render the line are opaque. There are no partially transparent pixels in the line. This type of line rendering gives the line a jagged appearance, and the line looks somewhat like a staircase. This technique of representing a line with a staircase is called aliasing; the staircase is an alias for the theoretical line.
## Antialiasing
A more sophisticated technique for rendering a line involves using partially transparent pixels along with opaque pixels. Pixels are set to pure red, or to some blend of red and the background color, depending on how close they are to the line. This type of rendering is called antialiasing and results in a line that the human eye perceives as more smooth. The following illustration shows how certain pixels are blended with the background to produce an antialiased line.
![Antialiasing a Line](./media/aboutgdip02-art34.gif "AboutGdip02_Art34")
Antialiasing, also called smoothing, can also be applied to curves. The following illustration shows an enlarged view of a smoothed ellipse.
![Antialiasing Curves](./media/aboutgdip02-art35.gif "AboutGdip02_Art35")
The following illustration shows the same ellipse in its actual size, once without antialiasing and once with antialiasing.
![Antialiasing example](./media/aboutgdip02-art36.gif "AboutGdip02_Art36")
To draw lines and curves that use antialiasing, create an instance of the <xref:System.Drawing.Graphics> class and set its <xref:System.Drawing.Graphics.SmoothingMode%2A> property to <xref:System.Drawing.Drawing2D.SmoothingMode.AntiAlias> or <xref:System.Drawing.Drawing2D.SmoothingMode.HighQuality>. Then call one of the drawing methods of that same <xref:System.Drawing.Graphics> class.
[!code-csharp[LinesCurvesAndShapes#81](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#81)]
[!code-vb[LinesCurvesAndShapes#81](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#81)]
## See also
- <xref:System.Drawing.Drawing2D.SmoothingMode?displayProperty=nameWithType>
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [How to: Use Antialiasing with Text](how-to-use-antialiasing-with-text.md)
@@ -0,0 +1,151 @@
---
title: "Application Settings Architecture"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "application settings [Windows Forms], architecture"
ms.assetid: c8eb2ad0-fac6-4ea2-9140-675a4a44d562
---
# Application Settings Architecture
This topic describes how the Application Settings architecture works, and explores advanced features of the architecture, such as grouped settings and settings keys.
The application settings architecture supports defining strongly typed settings with either application or user scope, and persisting the settings between application sessions. The architecture provides a default persistence engine for saving settings to and loading them from the local file system. The architecture also defines interfaces for supplying a custom persistence engine.
Interfaces are provided that enable custom components to persist their own settings when they are hosted in an application. By using settings keys, components can keep settings for multiple instances of the component separate.
## Defining Settings
The application settings architecture is used within both ASP.NET and Windows Forms, and it contains a number of base classes that are shared across both environments. The most important is <xref:System.Configuration.SettingsBase>, which provides access to settings through a collection, and provides low-level methods for loading and saving settings. Each environment implements its own class derived from <xref:System.Configuration.SettingsBase> to provide additional settings functionality for that environment. In a Windows Forms-based application, all application settings must be defined on a class derived from the <xref:System.Configuration.ApplicationSettingsBase> class, which adds the following functionality to the base class:
- Higher-level loading and saving operations
- Support for user-scoped settings
- Reverting a user's settings to the predefined defaults
- Upgrading settings from a previous application version
- Validating settings, either before they are changed or before they are saved
The settings can be described using a number of attributes defined within the <xref:System.Configuration> namespace; these are described in [Application Settings Attributes](application-settings-attributes.md). When you define a setting, you must apply it with either <xref:System.Configuration.ApplicationScopedSettingAttribute> or <xref:System.Configuration.UserScopedSettingAttribute>, which describes whether the setting applies to the entire application or just to the current user.
The following code example defines a custom settings class with a single setting, `BackgroundColor`.
[!code-csharp[ApplicationSettings.Create#1](~/samples/snippets/csharp/VS_Snippets_Winforms/ApplicationSettings.Create/CS/MyAppSettings.cs#1)]
[!code-vb[ApplicationSettings.Create#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ApplicationSettings.Create/VB/MyAppSettings.vb#1)]
## Settings Persistence
The <xref:System.Configuration.ApplicationSettingsBase> class does not itself persist or load settings; this job falls to the settings provider, a class that derives from <xref:System.Configuration.SettingsProvider>. If a derived class of <xref:System.Configuration.ApplicationSettingsBase> does not specify a settings provider through the <xref:System.Configuration.SettingsProviderAttribute>, then the default provider, <xref:System.Configuration.LocalFileSettingsProvider>, is used.
The configuration system that was originally released with the .NET Framework supports providing static application configuration data through either the local computer's machine.config file or within an `app.`exe.config file that you deploy with your application. The <xref:System.Configuration.LocalFileSettingsProvider> class expands this native support in the following ways:
- Application-scoped settings can be stored in either the machine.config or `app.`exe.config files. Machine.config is always read-only, while `app`.exe.config is restricted by security considerations to read-only for most applications.
- User-scoped settings can be stored in `app`.exe.config files, in which case they are treated as static defaults.
- Non-default user-scoped settings are stored in a new file, *user*.config, where *user* is the user name of the person currently executing the application. You can specify a default for a user-scoped setting with <xref:System.Configuration.DefaultSettingValueAttribute>. Because user-scoped settings often change during application execution, `user`.config is always read/write.
All three configuration files store settings in XML format. The top-level XML element for application-scoped settings is `<appSettings>`, while `<userSettings>` is used for user-scoped settings. An `app`.exe.config file which contains both application-scoped settings and defaults for user-scoped settings would look like this:
```xml
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WindowsApplication1.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" />
</sectionGroup>
</configSections>
<applicationSettings>
<WindowsApplication1.Properties.Settings>
<setting name="Cursor" serializeAs="String">
<value>Default</value>
</setting>
<setting name="DoubleBuffering" serializeAs="String">
<value>False</value>
</setting>
</WindowsApplication1.Properties.Settings>
</applicationSettings>
<userSettings>
<WindowsApplication1.Properties.Settings>
<setting name="FormTitle" serializeAs="String">
<value>Form1</value>
</setting>
<setting name="FormSize" serializeAs="String">
<value>595, 536</value>
</setting>
</WindowsApplication1.Properties.Settings>
</userSettings>
</configuration>
```
For a definition of the elements within the application settings section of a configuration file, see [Application Settings Schema](https://docs.microsoft.com/dotnet/framework/configure-apps/file-schema/application-settings-schema).
### Settings Bindings
Application settings uses the Windows Forms data binding architecture to provide two-way communication of settings updates between the settings object and components. If you use Visual Studio to create application settings and assign them to component properties, these bindings are generated automatically.
You can only bind an application setting to a component that supports the <xref:System.Windows.Forms.IBindableComponent> interface. Also, the component must implement a change event for a specific bound property, or notify application settings that the property has changed through the <xref:System.ComponentModel.INotifyPropertyChanged> interface. If the component does not implement <xref:System.Windows.Forms.IBindableComponent> and you are binding through Visual Studio, the bound properties will be set the first time, but will not update. If the component implements <xref:System.Windows.Forms.IBindableComponent> but does not support property change notifications, the binding will not update in the settings file when the property is changed.
Some Windows Forms components, such as <xref:System.Windows.Forms.ToolStripItem>, do not support settings bindings.
### Settings Serialization
When <xref:System.Configuration.LocalFileSettingsProvider> must save settings to disk, it performs the following actions:
1. Uses reflection to examine all of the properties defined on your <xref:System.Configuration.ApplicationSettingsBase> derived class, finding those that are applied with either <xref:System.Configuration.ApplicationScopedSettingAttribute> or <xref:System.Configuration.UserScopedSettingAttribute>.
2. Serializes the property to disk. It first attempts to call the <xref:System.ComponentModel.TypeConverter.ConvertToString%2A> or <xref:System.ComponentModel.TypeConverter.ConvertFromString%2A> on the type's associated <xref:System.ComponentModel.TypeConverter>. If this does not succeed, it uses XML serialization instead.
3. Determines which settings go in which files, based on the setting's attribute.
If you implement your own settings class, you can use the <xref:System.Configuration.SettingsSerializeAsAttribute> to mark a setting for either binary or custom serialization using the <xref:System.Configuration.SettingsSerializeAs> enumeration. For more information on creating your own settings class in code, see [How to: Create Application Settings](how-to-create-application-settings.md).
### Settings File Locations
The location of the `app`.exe.config and *user*.config files will differ based on how the application is installed. For a Windows Forms-based application copied onto the local computer, `app`.exe.config will reside in the same directory as the base directory of the application's main executable file, and *user*.config will reside in the location specified by the <xref:System.Windows.Forms.Application.LocalUserAppDataPath%2A?displayProperty=nameWithType> property. For an application installed by means of ClickOnce, both of these files will reside in the ClickOnce Data Directory underneath %InstallRoot%\Documents and Settings\\*username*\Local Settings.
The storage location of these files is slightly different if a user has enabled roaming profiles, which enables a user to define different Windows and application settings when they are using other computers within a domain. In that case, both ClickOnce applications and non-ClickOnce applications will have their `app`.exe.config and *user*.config files stored under %InstallRoot%\Documents and Settings\\*username*\Application Data.
For more information about how the Application Settings feature works with the new deployment technology, see [ClickOnce and Application Settings](/visualstudio/deployment/clickonce-and-application-settings). For more information about the ClickOnce Data Directory, see [Accessing Local and Remote Data in ClickOnce Applications](/visualstudio/deployment/accessing-local-and-remote-data-in-clickonce-applications).
## Application Settings and Security
Application settings are designed to work in partial trust, a restricted environment that is the default for Windows Forms applications hosted over the Internet or an intranet. No special permissions beyond partial trust are needed to use application settings with the default settings provider.
When application settings are used in a ClickOnce application, the `user`.config file is stored in the ClickOnce data directory. The size of the application's `user`.config file cannot exceed the data directory quota set by ClickOnce. For more information, see [ClickOnce and Application Settings](/visualstudio/deployment/clickonce-and-application-settings).
## Custom Settings Providers
In the Application Settings architecture, there is a loose coupling between the applications settings wrapper class, derived from <xref:System.Configuration.ApplicationSettingsBase>, and the associated settings provider or providers, derived from <xref:System.Configuration.SettingsProvider>. This association is defined only by the <xref:System.Configuration.SettingsProviderAttribute> applied to the wrapper class or its individual properties. If a settings provider is not explicitly specified, the default provider, <xref:System.Configuration.LocalFileSettingsProvider>, is used. As a result, this architecture supports creating and using custom settings providers.
For example, suppose that you want to develop and use `SqlSettingsProvider`, a provider that will store all settings data in a Microsoft SQL Server database. Your <xref:System.Configuration.SettingsProvider>-derived class would receive this information in its `Initialize` method as a parameter of type <xref:System.Collections.Specialized.NameValueCollection?displayProperty=nameWithType>. You would then implement the <xref:System.Configuration.SettingsProvider.GetPropertyValues%2A> method to retrieve your settings from the data store, and <xref:System.Configuration.SettingsProvider.SetPropertyValues%2A> to save them. Your provider can use the <xref:System.Configuration.SettingsPropertyCollection> supplied to <xref:System.Configuration.SettingsProvider.GetPropertyValues%2A> to determine the property's name, type, and scope, as well as any other settings attributes defined for that property.
Your provider will need to implement one property and one method whose implementations may not be obvious. The <xref:System.Configuration.SettingsProvider.ApplicationName%2A> property is an abstract property of <xref:System.Configuration.SettingsProvider>; you should program it to return the following:
[!code-csharp[ApplicationSettings.Architecture#2](~/samples/snippets/csharp/VS_Snippets_Winforms/ApplicationSettings.Architecture/CS/DummyClass.cs#2)]
[!code-vb[ApplicationSettings.Architecture#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ApplicationSettings.Architecture/VB/DummyProviderClass.vb#2)]
Your derived class must also implement an `Initialize` method that takes no arguments and returns no value. This method is not defined by <xref:System.Configuration.SettingsProvider>.
Finally, you implement <xref:System.Configuration.IApplicationSettingsProvider> on your provider to provide support for refreshing settings, reverting settings to their defaults, and upgrading settings from one application version to another.
Once you have implemented and compiled your provider, you need to instruct your settings class to use this provider instead of the default. You accomplish this through the <xref:System.Configuration.SettingsProviderAttribute>. If applied to an entire settings class, the provider is used for each setting that the class defines; if applied to individual settings, Application Settings architecture uses that provider for those settings only, and uses <xref:System.Configuration.LocalFileSettingsProvider> for the rest. The following code example shows how to instruct the settings class to use your custom provider.
[!code-csharp[ApplicationSettings.Architecture#1](~/samples/snippets/csharp/VS_Snippets_Winforms/ApplicationSettings.Architecture/CS/DummyClass.cs#1)]
[!code-vb[ApplicationSettings.Architecture#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ApplicationSettings.Architecture/VB/DummyProviderClass.vb#1)]
A provider may be called from multiple threads simultaneously, but it will always write to the same storage location; therefore, the Application Settings architecture will only ever instantiate a single instance of your provider class.
> [!IMPORTANT]
> You should ensure that your provider is thread-safe, and only allows one thread at a time to write to the configuration files.
Your provider does not need to support all of the settings attributes defined in the <xref:System.Configuration?displayProperty=nameWithType> namespace, though it must at a minimum support <xref:System.Configuration.ApplicationScopedSettingAttribute> and <xref:System.Configuration.UserScopedSettingAttribute>, and should also support <xref:System.Configuration.DefaultSettingValueAttribute>. For those attributes that it does not support, your provider should just fail without notification; it should not throw an exception. If the settings class uses an invalid combination of attributes, however — such as applying <xref:System.Configuration.ApplicationScopedSettingAttribute> and <xref:System.Configuration.UserScopedSettingAttribute> to the same setting — your provider should throw an exception and cease operation.
## See also
- <xref:System.Configuration.ApplicationSettingsBase>
- <xref:System.Configuration.SettingsProvider>
- <xref:System.Configuration.LocalFileSettingsProvider>
- [Application Settings Overview](application-settings-overview.md)
- [Application Settings for Custom Controls](application-settings-for-custom-controls.md)
- [ClickOnce and Application Settings](/visualstudio/deployment/clickonce-and-application-settings)
- [Application Settings Schema](https://docs.microsoft.com/dotnet/framework/configure-apps/file-schema/application-settings-schema)
@@ -0,0 +1,39 @@
---
title: "Application Settings Attributes"
ms.date: "03/30/2017"
helpviewer_keywords:
- "application settings [Windows Forms], attributes"
- "attributes [Windows Forms], application settings"
- "wrapper classes [Windows Forms], application settings"
ms.assetid: 53caa66c-a9fb-43a5-953c-ad092590098d
---
# Application Settings Attributes
The Application Settings architecture provides many attributes that can be applied either to the applications settings wrapper class or its individual properties. These attributes are examined at run time by the application settings infrastructure, often specifically the settings provider, in order to tailor its functioning to the stated needs of the custom wrapper.
The following table lists the attributes that can be applied to the application settings wrapper class, this class's individual properties, or both. By definition, only a single scope attribute—**UserScopedSettingAttribute** or **ApplicationScopedSettingAttribute**—must be applied to each and every settings property.
> [!NOTE]
> A custom settings provider, derived from the <xref:System.Configuration.SettingsProvider> class, is only required to recognize the following three attributes: **ApplicationScopedSettingAttribute**, **UserScopedSettingAttribute**, and **DefaultSettingValueAttribute**.
|Attribute|Target|Description|
|---------------|------------|-----------------|
|<xref:System.Configuration.SettingsProviderAttribute>|Both|Specifies the short name of the settings provider to use for persistence.<br /><br /> If this attribute is not supplied, the default provider, <xref:System.Configuration.LocalFileSettingsProvider>, is assumed.|
|<xref:System.Configuration.UserScopedSettingAttribute>|Both|Defines a property as a user-scoped application setting.|
|<xref:System.Configuration.ApplicationScopedSettingAttribute>|Both|Defines a property as an application-scoped application setting.|
|<xref:System.Configuration.DefaultSettingValueAttribute>|Property|Specifies a string that can be deserialized by the provider into the hard-coded default value for this property.<br /><br /> The <xref:System.Configuration.LocalFileSettingsProvider> does not require this attribute, and will override any value provided by this attribute if there is a value already persisted.|
|<xref:System.Configuration.SettingsDescriptionAttribute>|Property|Provides the descriptive test for an individual setting, used primarily by run-time and design-time tools.|
|<xref:System.Configuration.SettingsGroupNameAttribute>|Class|Provides an explicit name for a settings group. If this attribute is missing, <xref:System.Configuration.ApplicationSettingsBase> uses the wrapper class name.|
|<xref:System.Configuration.SettingsGroupDescriptionAttribute>|Class|Provides the descriptive test for a settings group, used primarily by run-time and design-time tools.|
|<xref:System.Configuration.SettingsManageabilityAttribute>|Both|Specifies zero or more manageability services that should be provided to the settings group or property. The available services are described by the <xref:System.Configuration.SettingsManageability> enumeration.|
|<xref:System.Configuration.SpecialSettingAttribute>|Property|Indicates that a setting belongs to a special, predefined category, such as a connection string, that suggests special processing by the settings provider. The predefined categories for this attribute are defined by the <xref:System.Configuration.SpecialSetting> enumeration.|
|<xref:System.Configuration.SettingsSerializeAsAttribute>|Both|Specifies a preferred serialization mechanism for a settings group or property. The available serialization mechanisms are defined by the <xref:System.Configuration.SettingsSerializeAs> enumeration.|
|<xref:System.Configuration.NoSettingsVersionUpgradeAttribute>|Property|Specifies that a settings provider should disable all application upgrade functionality for the marked property.|
*Class* indicates that the attribute can be applied only to an application settings wrapper class. *Property* indicates that the attribute can be applied only settings properties. *Both* indicates that the attribute can be applied at either level.
## See also
- <xref:System.Configuration.ApplicationSettingsBase>
- <xref:System.Configuration.SettingsProvider>
- [Application Settings Architecture](application-settings-architecture.md)
- [How to: Create Application Settings](how-to-create-application-settings.md)
@@ -0,0 +1,46 @@
---
title: "Application Settings for Custom Controls"
ms.date: "03/30/2017"
helpviewer_keywords:
- "custom controls [Windows Forms], application settings"
- "application settings [Windows Forms], custom controls"
ms.assetid: f44afb74-76cc-44f2-890a-44b7cdc211a1
---
# Application Settings for Custom Controls
You must complete certain tasks to give your custom controls the ability to persist application settings when the controls are hosted in third-party applications.
Most of the documentation about the Application Settings feature is written under the assumption that you are creating a standalone application. However, if you are creating a control that other developers will host in their applications, you need to take a few additional steps for your control to persist its settings properly.
## Application Settings and Custom Controls
For your control to properly persist its settings, it must encapsulate the process by creating its own dedicated applications settings wrapper class, derived from <xref:System.Configuration.ApplicationSettingsBase>. Additionally, the main control class must implement the <xref:System.Configuration.IPersistComponentSettings>. The interface contains several properties as well as two methods, <xref:System.Configuration.IPersistComponentSettings.LoadComponentSettings%2A> and <xref:System.Configuration.IPersistComponentSettings.SaveComponentSettings%2A>. If you add your control to a form using the **Windows Forms Designer** in Visual Studio, Windows Forms will call <xref:System.Configuration.IPersistComponentSettings.LoadComponentSettings%2A> automatically when the control is initialized; you must call <xref:System.Configuration.IPersistComponentSettings.SaveComponentSettings%2A> yourself in the `Dispose` method of your control.
In addition, you should implement the following in order for application settings for custom controls to work properly in design-time environments such as Visual Studio:
1. A custom application settings class with a constructor that takes an <xref:System.ComponentModel.IComponent> as a single parameter. Use this class to save and load all of your application settings. When you create a new instance of this class, pass your custom control using the constructor.
2. Create this custom settings class after the control has been created and placed on a form, such as in the form's <xref:System.Windows.Forms.Form.Load> event handler.
For instructions on creating a custom settings class, see [How to: Create Application Settings](how-to-create-application-settings.md).
## Settings Keys and Shared Settings
Some controls can be used multiple times within the same form. Most of the time, you will want these controls to persist their own individual settings. With the <xref:System.Configuration.IPersistComponentSettings.SettingsKey%2A> property on <xref:System.Configuration.IPersistComponentSettings>, you can supply a unique string that acts to disambiguate multiple versions of a control on a form.
The simplest way to implement <xref:System.Configuration.IPersistComponentSettings.SettingsKey%2A> is to use the <xref:System.Windows.Forms.Control.Name%2A> property of the control for the <xref:System.Configuration.IPersistComponentSettings.SettingsKey%2A>. When you load or save the control's settings, you pass the value of <xref:System.Configuration.IPersistComponentSettings.SettingsKey%2A> on to the <xref:System.Configuration.ApplicationSettingsBase.SettingsKey%2A> property of the <xref:System.Configuration.ApplicationSettingsBase> class. Application Settings uses this unique key when it persists the user's settings to XML. The following code example shows how a `<userSettings>` section may look for an instance of a custom control named `CustomControl1` that saves a setting for its `Text` property.
```xml
<userSettings>
<CustomControl1>
<setting name="Text" serializedAs="string">
<value>Hello, World</value>
</setting>
</CustomControl1>
</userSettings>
```
Any instances of a control that do not supply a value for <xref:System.Configuration.ApplicationSettingsBase.SettingsKey%2A> will share the same settings.
## See also
- <xref:System.Configuration.ApplicationSettingsBase>
- <xref:System.Configuration.IPersistComponentSettings>
- [Application Settings Architecture](application-settings-architecture.md)
@@ -0,0 +1,40 @@
---
title: "Application Settings"
ms.date: "04/07/2017"
f1_keywords:
- "ClientApplicationSettings"
helpviewer_keywords:
- "application settings [Windows Forms]"
- "Windows Forms, application settings"
ms.assetid: 64090a34-8556-4904-8ea0-20efe9f8c886
---
# Application Settings for Windows Forms
The Applications Settings feature of Windows Forms makes it easy to create, store, and maintain custom application and user preferences on the client. With Application Settings, you can store not only application data such as database connection strings, but also user-specific data, such as toolbar positions and most-recently used lists.
## In This Section
[Application Settings Overview](application-settings-overview.md)
Discusses how to create and store settings data on behalf of your application and your users.
[Application Settings Architecture](application-settings-architecture.md)
Describes how the Application Settings feature works, and explores advanced features of the architecture such as grouped settings and settings keys.
[Application Settings Attributes](application-settings-attributes.md)
Lists and describes the attributes that can be applied to an application settings wrapper class or its settings properties.
[Application Settings for Custom Controls](application-settings-for-custom-controls.md)
Discusses what must be done to give your custom controls the ability to persist application settings when hosted in third-party applications.
[How to: Create Application Settings](how-to-create-application-settings.md)
Demonstrates creating new application settings that are persisted between application sessions.
[How to: Validate Application Settings](how-to-validate-application-settings.md)
Demonstrates validating application settings before they are persisted.
## Related topics
[Windows Forms Configuration Section](https://docs.microsoft.com/dotnet/framework/configure-apps/file-schema/winforms/index)
Documents the settings to enable High DPI support in Windows Forms Application starting with the .NET Framework 4.7.
## See also
- [Windows Forms](../index.yml)
@@ -0,0 +1,57 @@
---
title: "Application Settings Overview"
description: Learn about the Application Settings feature of Windows Forms, for example how to create and store settings data on behalf of your application and your users.
ms.date: "03/30/2017"
f1_keywords:
- "ApplicationsSettingsOverview"
helpviewer_keywords:
- "application settings [Windows Forms], about application settings"
- "dynamic properties"
- "user preferences [Windows Forms], tracking"
ms.assetid: 0dd8bca5-a6bf-4ac4-8eec-5725d08b38dc
---
# Application Settings Overview
This article discusses how to create and store settings data on behalf of your application and your users.
The Application Settings feature of Windows Forms makes it easy to create, store, and maintain custom application and user preferences on the client computer. With Windows Forms application settings, you can store not only application data such as database connection strings, but also user-specific data, such as user application preferences. Using Visual Studio or custom managed code, you can create new settings, read them from and write them to disk, bind them to properties on your forms, and validate settings data prior to loading and saving.
Application settings enables developers to save state in their application using very little custom code, and is a replacement for dynamic properties in previous versions of the .NET Framework. Application settings contains many improvements over dynamic properties, which are read-only, late-bound, and require more custom programming. The dynamic property classes have been retained in .NET Framework 2.0, but they are just shell classes that thinly wrap the application settings classes.
## What Are Application Settings?
Your Windows Forms applications will often require data that's critical to running the application, but which you don't want to include directly in the application's code. If your application uses a Web Service or a database server, you may want to store this information in a separate file, so that you can change it in the future without recompiling. Similarly, your applications may require storing data that is specific to the current user. Most applications, for example, have user preferences that customize the application's appearance and behavior.
Application settings addresses both needs by providing an easy way to store both application-scoped and user-scoped settings on the client computer. Using Visual Studio or a code editor, you define a setting for a given property by specifying its name, data type, and scope (application or user). You can even place related settings into named groups for easier use and readability. Once defined, these settings are persisted and read back into memory automatically at run time. A pluggable architecture enables the persistence mechanism to be changed, but by default, the local file system is used.
Application settings works by persisting data as XML to different configuration (.config) files, corresponding to whether the setting is application-scoped or user-scoped. In most cases, the application-scoped settings are read-only; because they are program information, you will typically not need to overwrite them. By contrast, user-scoped settings can be read and written safely at run time, even if your application runs under partial trust. For more information about partial trust, see [Security in Windows Forms Overview](../security-in-windows-forms-overview.md).
Settings are stored as XML fragments in configuration files. Application-scoped settings are represented by the `<applicationSettings>` element, and generally are placed in *app*.exe.config, where *app* is the name of your main executable file. User-scoped settings are represented by the `<userSettings>` element and are placed in *user*.config, where *user* is the user name of the person currently running the application. You must deploy the *app*.exe.config file with your application; the settings architecture will create the *user*.config files on demand the first time the application saves settings for that user. You can also define a `<userSettings>` block within *app*.exe.config to provide default values for user-scoped settings.
Custom controls can also save their own settings by implementing the <xref:System.Configuration.IPersistComponentSettings> interface, which exposes the <xref:System.Configuration.IPersistComponentSettings.SaveSettings%2A> method. The Windows Forms <xref:System.Windows.Forms.ToolStrip> control implements this interface to save the position of toolbars and toolbar items between application sessions. For more information about custom controls and application settings, see [Application Settings for Custom Controls](application-settings-for-custom-controls.md).
## Limitations of Application Settings
You cannot use application settings in an unmanaged application that hosts the .NET Framework. Settings will not work in such environments as Visual Studio add-ins, C++ for Microsoft Office, control hosting in Internet Explorer, or Microsoft Outlook add-ins and projects.
You currently cannot bind to some properties in Windows Forms. The most notable example is the <xref:System.Windows.Forms.Form.ClientSize%2A> property, as binding to this property would cause unpredictable behavior at run time. You can usually work around these issues by saving and loading these settings programmatically.
Application settings has no built-in facility for encrypting information automatically. You should never store security-related information, such as database passwords, in clear text. If you want to store such sensitive information, you as the application developer are responsible for making sure it is secure. If you want to store connection strings, we recommend that you use Windows Integrated Security and not resort to hard-coding passwords into the URL. For more information, see [Code Access Security and ADO.NET](https://docs.microsoft.com/dotnet/framework/data/adonet/code-access-security).
## Getting Started with Application Settings
If you use Visual Studio, you can define settings within the Windows Forms Designer using the **(ApplicationSettings)** property in the **Properties** window. When you define settings this way, Visual Studio automatically creates a custom managed wrapper class that associates each setting with a class property. Visual Studio also takes care of binding the setting to a property on a form or control so that the control's settings are restored automatically when its form is displayed, and saved automatically when the form is closed.
If you want more detailed control over your settings, you can define your own custom applications settings wrapper class. This is accomplished by deriving a class from <xref:System.Configuration.ApplicationSettingsBase>, adding a property that corresponds to each setting, and applying special attributes to these properties. For details about creating wrapper classes, see [Application Settings Architecture](application-settings-architecture.md).
You can also use the <xref:System.Windows.Forms.Binding> class to bind settings programmatically to properties on forms and controls.
## See also
- <xref:System.Configuration.ApplicationSettingsBase>
- <xref:System.Configuration.SettingsProvider>
- <xref:System.Configuration.LocalFileSettingsProvider>
- <xref:System.Configuration.IPersistComponentSettings>
- [How to: Validate Application Settings](how-to-validate-application-settings.md)
- [Managing Application Settings (.NET)](/visualstudio/ide/managing-application-settings-dotnet)
- [How To: Read Settings at Run Time With C#](how-to-read-settings-at-run-time-with-csharp.md)
- [Using Application Settings and User Settings](using-application-settings-and-user-settings.md)
- [Application Settings Architecture](application-settings-architecture.md)
- [Application Settings for Custom Controls](application-settings-for-custom-controls.md)
@@ -0,0 +1,41 @@
---
title: "B&#233;zier Splines in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "Bezier splines"
- "splines [Windows Forms], Bezier"
- "GDI+, Bezier splines"
ms.assetid: 5774ce1e-87d4-4bc7-88c4-4862052781b8
---
# B&#233;zier Splines in GDI+
A Bézier spline is a curve specified by four points: two end points (p1 and p2) and two control points (c1 and c2). The curve begins at p1 and ends at p2. The curve does not pass through the control points, but the control points act as magnets, pulling the curve in certain directions and influencing the way the curve bends. The following illustration shows a Bézier curve along with its endpoints and control points.
![Bezier Splines](./media/aboutgdip02-art11a.gif "Aboutgdip02_art11a")
The curve starts at p1 and moves toward the control point c1. The tangent line to the curve at p1 is the line drawn from p1 to c1. The tangent line at the endpoint p2 is the line drawn from c2 to p2.
## Drawing Bézier Splines
To draw a Bézier spline, you need an instance of the <xref:System.Drawing.Graphics> class and a <xref:System.Drawing.Pen>. The instance of the <xref:System.Drawing.Graphics> class provides the <xref:System.Drawing.Graphics.DrawBezier%2A> method, and the <xref:System.Drawing.Pen> stores attributes, such as width and color, of the line used to render the curve. The <xref:System.Drawing.Pen> is passed as one of the arguments to the <xref:System.Drawing.Graphics.DrawBezier%2A> method. The remaining arguments passed to the <xref:System.Drawing.Graphics.DrawBezier%2A> method are the endpoints and the control points. The following example draws a Bézier spline with starting point (0, 0), control points (40, 20) and (80, 150), and ending point (100, 10):
[!code-csharp[LinesCurvesAndShapes#71](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#71)]
[!code-vb[LinesCurvesAndShapes#71](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#71)]
The following illustration shows the curve, the control points, and two tangent lines.
![Bezier Splines](./media/aboutgdip02-art12.gif "Aboutgdip02_art12")
Bézier splines were originally developed by Pierre Bézier for design in the automotive industry. They have since proven to be useful in many types of computer-aided design and are also used to define the outlines of fonts. Bézier splines can yield a wide variety of shapes, some of which are shown in the following illustration.
![Paths](./media/aboutgdip02-art13.gif "Aboutgdip02_art13")
## See also
- <xref:System.Drawing.Graphics?displayProperty=nameWithType>
- <xref:System.Drawing.Pen?displayProperty=nameWithType>
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [Constructing and Drawing Curves](constructing-and-drawing-curves.md)
- [How to: Create Graphics Objects for Drawing](how-to-create-graphics-objects-for-drawing.md)
- [How to: Create a Pen](how-to-create-a-pen.md)
@@ -0,0 +1,114 @@
---
title: Bi-Directional Support
ms.date: "09/30/2017"
helpviewer_keywords:
- "globalization [Windows Forms], bi-directional support in Windows"
- "Windows Forms, international"
- "localization [Windows Forms], bi-directional support in Windows"
- "bi-directional language support [Windows Forms], Windows applications"
- "Windows Forms, bi-directional support"
---
# Bi-Directional Support for Windows Forms Applications
You can use Visual Studio to create Windows-based applications that support bi-directional (right-to-left) languages such as Arabic and Hebrew. This includes standard forms, dialog boxes, MDI forms, and all the controls you can work with in these forms—that is, all the objects in the <xref:System.Windows.Forms.Control> namespace.
## Culture Support
Culture and UI culture settings determine how an application works with dates, times, currency, and other information. Support for culture and UI culture is the same for bi-directional languages as it is for any other languages. For more information, see [Culture-specific classes for global Windows forms and web forms](/visualstudio/ide/globalizing-and-localizing-applications).
## RightToLeft and RightToLeftLayout Properties
The base <xref:System.Windows.Forms.Control> class, from which forms derive, includes a <xref:System.Windows.Forms.Control.RightToLeft%2A> property that you can set to change the reading order of a form and its controls. If you set the form's <xref:System.Windows.Forms.Control.RightToLeft%2A> property, by default controls on the form inherit this setting. However, you can also set the <xref:System.Windows.Forms.Control.RightToLeft%2A> property individually on most controls. Also see [How to: Display Right-to-Left Text in Windows Forms for Globalization](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/7d3337xw(v=vs.100)).
The effect of the <xref:System.Windows.Forms.Control.RightToLeft%2A> property can differ from one control to another. In some controls the property only sets the reading order, as in the <xref:System.Windows.Forms.Button>, <xref:System.Windows.Forms.TreeView> and <xref:System.Windows.Forms.ToolTip> controls. In other controls, the <xref:System.Windows.Forms.Control.RightToLeft%2A> property changes both reading order and layout. This includes the <xref:System.Windows.Forms.RadioButton>, <xref:System.Windows.Forms.ComboBox> and <xref:System.Windows.Forms.CheckBox> controls. Other controls require that the <xref:System.Windows.Forms.Form.RightToLeftLayout%2A> property be applied to mirror its layout from right to left. The following table provides details on how the <xref:System.Windows.Forms.Control.RightToLeft%2A> and <xref:System.Windows.Forms.Form.RightToLeftLayout%2A> properties affect individual Windows Forms controls.
|Control/Component|Effect of RightToLeft property|Effect of RightToLeftLayout property|Requires mirroring?|
|------------------------|------------------------------------|------------------------------------------|-------------------------|
|<xref:System.Windows.Forms.Button>|Sets the RTL reading order. Reverses <xref:System.Windows.Forms.ButtonBase.TextAlign%2A>, <xref:System.Windows.Forms.ButtonBase.ImageAlign%2A>, and <xref:System.Windows.Forms.ButtonBase.TextImageRelation%2A>|No effect|No|
|<xref:System.Windows.Forms.CheckBox>|The check box is displayed on the right side of the text|No effect|No|
|<xref:System.Windows.Forms.CheckedListBox>|All the check boxes are displayed on the right side of the text|No effect|No|
|<xref:System.Windows.Forms.ColorDialog>|Not affected; depends on the language of the operating system|No effect|No|
|<xref:System.Windows.Forms.ComboBox>|Items in combo box control are right-aligned|No effect|No|
|<xref:System.Windows.Forms.ContextMenu>|Appears right-aligned with RTL reading order|No effect|No|
|<xref:System.Windows.Forms.DataGrid>|Appears right-aligned with RTL reading order|No effect|No|
|<xref:System.Windows.Forms.DataGridView>|Affects both RTL reading order and control layout|No effect|No|
|<xref:System.Windows.Forms.DateTimePicker>|Not affected; depends on the language of the operating system|Mirrors the control|Yes|
|<xref:System.Windows.Forms.DomainUpDown>|Left-aligns the up and down buttons|No effect|No|
|<xref:System.Windows.Forms.ErrorProvider>|Not supported|No effect|No|
|<xref:System.Windows.Forms.FontDialog>|Depends on the language of the operating system|No effect|No|
|<xref:System.Windows.Forms.Form>|Sets RTL reading order, and reverses scrollbars|Mirrors the form|Yes|
|<xref:System.Windows.Forms.GroupBox>|The caption is displayed right aligned. Child controls may inherit this property.|Use a <xref:System.Windows.Forms.TableLayoutPanel> within the control for right-to-left mirroring support|No|
|<xref:System.Windows.Forms.HScrollBar>|Starts with the scroll box (thumb) right-aligned|No effect|No|
|<xref:System.Windows.Forms.ImageList>|Not required|No effect|No|
|<xref:System.Windows.Forms.Label>|Displayed right-aligned. Reverses <xref:System.Windows.Forms.Label.TextAlign%2A> and <xref:System.Windows.Forms.Label.ImageAlign%2A>|No effect|No|
|<xref:System.Windows.Forms.LinkLabel>|Displayed right-aligned. Reverses <xref:System.Windows.Forms.Label.TextAlign%2A> and <xref:System.Windows.Forms.Label.ImageAlign%2A>|No effect|No|
|<xref:System.Windows.Forms.ListBox>|Items are right-aligned|No effect|No|
|<xref:System.Windows.Forms.ListView>|Sets the reading order to RTL; elements stay left-aligned|Mirrors the control|Yes|
|<xref:System.Windows.Forms.MainMenu>|Displayed right-aligned with RTL reading order at run time (not at design time)|No effect|No|
|<xref:System.Windows.Forms.MaskedTextBox>|Displays text from right to left.|No effect|No|
|<xref:System.Windows.Forms.MonthCalendar>|Not affected; depends on the language of the operating system|Mirrors the control|Yes|
|<xref:System.Windows.Forms.NotifyIcon>|Not supported|Not supported|No|
|<xref:System.Windows.Forms.NumericUpDown>|Up and down buttons are left-aligned|No effect|No|
|<xref:System.Windows.Forms.OpenFileDialog>|On right-to-left operating systems, setting the containing form's <xref:System.Windows.Forms.Control.RightToLeft> property to <xref:System.Windows.Forms.RightToLeft.Yes?displayProperty=nameWithType> localizes the dialog |No effect|No|
|<xref:System.Windows.Forms.PageSetupDialog>|Not affected; depends on the language of the operating system|No effect|No|
|<xref:System.Windows.Forms.Panel>|Child controls may inherit this property|Use <xref:System.Windows.Forms.TableLayoutPanel> within the control for right to left support|Yes|
|<xref:System.Windows.Forms.PictureBox>|Not supported|No effect|No|
|<xref:System.Windows.Forms.PrintDialog>|Not affected; depends on the language of the operating system|No effect|No|
|<xref:System.Drawing.Printing.PrintDocument>|The vertical scroll bar become left-aligned and the horizontal scroll bar starts from the left|No effect|No|
|<xref:System.Windows.Forms.PrintPreviewDialog>|Not supported|Not supported|No|
|<xref:System.Windows.Forms.ProgressBar>|Not affect by this property|Mirrors the control|Yes|
|<xref:System.Windows.Forms.RadioButton>|The radio button is displayed on the right side of the text|No effect|No|
|<xref:System.Windows.Forms.RichTextBox>|Control elements that include text are displayed from right to left with RTL reading order|No effect|No|
|<xref:System.Windows.Forms.SaveFileDialog>|Not affected; depends on the language of the operating system|No effect|No|
|<xref:System.Windows.Forms.SplitContainer>|Panel layout is reversed; vertical scrollbar appears on the left; horizontal scrollbar starts from the right|Use a <xref:System.Windows.Forms.TableLayoutPanel> to mirror order of child controls|No|
|<xref:System.Windows.Forms.Splitter>|Not supported|No effect|No|
|<xref:System.Windows.Forms.StatusBar>|Not supported; use <xref:System.Windows.Forms.StatusStrip> instead|No effect; use <xref:System.Windows.Forms.StatusStrip> instead|No|
|<xref:System.Windows.Forms.TabControl>|Not affected by this property|Mirrors the control|Yes|
|<xref:System.Windows.Forms.TextBox>|Displays text from right to left with RTL reading order|No effect|No|
|<xref:System.Windows.Forms.Timer>|Not required|Not required|No|
|<xref:System.Windows.Forms.ToolBar>|Not affected by this property; use <xref:System.Windows.Forms.ToolStrip> instead|No effect; use <xref:System.Windows.Forms.ToolStrip> instead|Yes|
|<xref:System.Windows.Forms.ToolTip>|Sets the RTL reading order|No effect|No|
|<xref:System.Windows.Forms.TrackBar>|The scroll or track starts from the right; when <xref:System.Windows.Forms.TrackBar.Orientation%2A> is vertical, ticks occur from the right|No effect|No|
|<xref:System.Windows.Forms.TreeView>|Sets the RTL reading order only|Mirrors the control|Yes|
|<xref:System.Windows.Forms.UserControl>|Vertical scrollbar appears on the left; horizontal scrollbar has thumb on the right|No direct support; use a <xref:System.Windows.Forms.TableLayoutPanel>|No|
|<xref:System.Windows.Forms.VScrollBar>|Displayed on the left side instead of right side of scrollable controls|No effect|No|
## Encoding
Windows Forms support Unicode, so you can include any character set when you create your bi-directional applications. However, not all Windows Forms controls support Unicode on all platforms.
## GDI+
You can use GDI+ to draw text with right-to-left reading order. The <xref:System.Drawing.Graphics.DrawString%2A> method, which is used to draw text, supports a `StringFormat` parameter that you can set to the <xref:System.Drawing.StringFormatFlags.DirectionRightToLeft> member of the <xref:System.Drawing.StringFormatFlags> enumeration in order to reverse the point of origin for the text.
## Common Dialog Boxes
System tools such as the File Open dialog box are under the control of Windows. They inherit language elements from the operating system. If you are using a version of Windows with the correct language settings, these dialog boxes will work correctly with bi-directional languages.
Similarly, message boxes go through the operating system and support bi-directional text. The captions on message box buttons are based on the current language setting. By default, message boxes do not use right-to-left reading order, but you can specify a parameter to change the reading order when the message boxes are displayed.
## RightToLeft, Scrollbars, and ScrollableControl
There is currently a limitation in Windows Forms that prevents all classes derived from <xref:System.Windows.Forms.ScrollableControl> from acting properly when both <xref:System.Windows.Forms.Control.RightToLeft%2A> is enabled and <xref:System.Windows.Forms.ScrollableControl.AutoScroll%2A> is set to <xref:System.Windows.Forms.RightToLeft.Yes>. For example, let's say that you place a control such as <xref:System.Windows.Forms.Panel>—or a container class derived from <xref:System.Windows.Forms.Panel> (such as <xref:System.Windows.Forms.FlowLayoutPanel> or <xref:System.Windows.Forms.TableLayoutPanel>)—on your form. If you set <xref:System.Windows.Forms.ScrollableControl.AutoScroll%2A> on the container to <xref:System.Windows.Forms.RightToLeft.Yes> and then set the <xref:System.Windows.Forms.Control.Anchor%2A> property on one or more of the controls inside of the container to <xref:System.Windows.Forms.AnchorStyles.Right>, then no scrollbar ever appears. The class derived from <xref:System.Windows.Forms.ScrollableControl> acts as if <xref:System.Windows.Forms.ScrollableControl.AutoScroll%2A> were set to <xref:System.Windows.Forms.RightToLeft.No>.
Currently, the only workaround is to nest the <xref:System.Windows.Forms.ScrollableControl> inside another <xref:System.Windows.Forms.ScrollableControl>. For instance, if you need <xref:System.Windows.Forms.TableLayoutPanel> to work in this situation, you can place it inside of a <xref:System.Windows.Forms.Panel> control and set <xref:System.Windows.Forms.ScrollableControl.AutoScroll%2A> on the <xref:System.Windows.Forms.Panel> to <xref:System.Windows.Forms.RightToLeft.Yes>.
## Mirroring
*Mirroring* refers to reversing the layout of UI elements so that they flow from right to left. In a mirrored Windows Form, for example, the Minimize, Maximize, and Close buttons appear left-most on the title bar, not right-most.
Setting a form or control's <xref:System.Windows.Forms.Control.RightToLeft%2A> property to `true` reverses the reading order of elements on a form, but this setting does not reverse the layout to be right-to-left— that is, it does not cause mirroring. For example, setting this property does not move the **Minimize**, **Maximize**, and **Close** buttons in the form's title bar to the left side of the form. Similarly, some controls, such as the <xref:System.Windows.Forms.TreeView> control, require mirroring in order to change their display to be appropriate for Arabic or Hebrew. You can mirror these controls by settings the <xref:System.Windows.Forms.Form.RightToLeftLayout%2A> property.
You can create mirrored versions of the following controls:
- <xref:System.Windows.Forms.ColumnHeader.ListView%2A>
- <xref:System.Windows.Forms.Panel>
- <xref:System.Windows.Forms.StatusBar>
- <xref:System.Windows.Forms.TabControl>
- <xref:System.Windows.Forms.TabPage>
- <xref:System.Windows.Forms.ToolBar>
- <xref:System.Windows.Forms.TreeView>
Some controls are sealed. Therefore, you cannot derive a new control from them. These include the <xref:System.Windows.Forms.ImageList> and <xref:System.Windows.Forms.ProgressBar> controls.
## See also
- [Bidirectional Support for ASP.NET Web Applications](https://docs.microsoft.com/previous-versions/aspnet/6eedwbtt(v=vs.100))
@@ -0,0 +1,81 @@
---
title: "Brushes and Filled Shapes in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "brushes [Windows Forms], GDI+"
- "filled shapes [Windows Forms], GDI+"
- "shapes [Windows Forms], GDI+"
- "GDI+, brushes"
- "GDI+, filled shapes"
- "gradient brushes"
- "brushes [Windows Forms], gradient"
ms.assetid: e863e2a7-0294-4130-99b6-f1ea3201e7cd
---
# Brushes and Filled Shapes in GDI+
A closed shape, such as a rectangle or an ellipse, consists of an outline and an interior. The outline is drawn with a pen and the interior is filled with a brush. GDI+ provides several brush classes for filling the interiors of closed shapes: <xref:System.Drawing.SolidBrush>, <xref:System.Drawing.Drawing2D.HatchBrush>, <xref:System.Drawing.TextureBrush>, <xref:System.Drawing.Drawing2D.LinearGradientBrush>, and <xref:System.Drawing.Drawing2D.PathGradientBrush>. All of these classes inherit from the <xref:System.Drawing.Brush> class. The following illustration shows a rectangle filled with a solid brush and an ellipse filled with a hatch brush.
![Filled Shapes](./media/aboutgdip02-art17.gif "Aboutgdip02_art17")
## Solid Brushes
To fill a closed shape, you need an instance of the <xref:System.Drawing.Graphics> class and a <xref:System.Drawing.Brush>. The instance of the <xref:System.Drawing.Graphics> class provides methods, such as <xref:System.Drawing.Graphics.FillRectangle%2A> and <xref:System.Drawing.Graphics.FillEllipse%2A>, and the <xref:System.Drawing.Brush> stores attributes of the fill, such as color and pattern. The <xref:System.Drawing.Brush> is passed as one of the arguments to the fill method. The following code example shows how to fill an ellipse with a solid red color.
[!code-csharp[LinesCurvesAndShapes#121](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#121)]
[!code-vb[LinesCurvesAndShapes#121](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#121)]
> [!NOTE]
> In the preceding example, the brush is of type <xref:System.Drawing.SolidBrush>, which inherits from <xref:System.Drawing.Brush>.
## Hatch Brushes
When you fill a shape with a hatch brush, you specify a foreground color, a background color, and a hatch style. The foreground color is the color of the hatching.
[!code-csharp[LinesCurvesAndShapes#122](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#122)]
[!code-vb[LinesCurvesAndShapes#122](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#122)]
GDI+ provides more than 50 hatch styles; the three styles shown in the following illustration are <xref:System.Drawing.Drawing2D.HatchStyle.Horizontal>, <xref:System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal>, and <xref:System.Drawing.Drawing2D.HatchStyle.Cross>.
![Filled Shapes](./media/aboutgdip02-art18.gif "Aboutgdip02_art18")
## Texture Brushes
With a texture brush, you can fill a shape with a pattern stored in a bitmap. For example, suppose the following picture is stored in a disk file named `MyTexture.bmp`.
![Filled Shape](./media/aboutgdip02-art19.gif "Aboutgdip02_Art19")
The following code example shows how to fill an ellipse by repeating the picture stored in `MyTexture.bmp`.
[!code-csharp[LinesCurvesAndShapes#123](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#123)]
[!code-vb[LinesCurvesAndShapes#123](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#123)]
The following illustration shows the filled ellipse.
![Filled Shape](./media/aboutgdip02-art20.gif "AboutGdip02_Art20")
## Gradient Brushes
GDI+ provides two kinds of gradient brushes: linear and path. You can use a linear gradient brush to fill a shape with color that changes gradually as you move across the shape horizontally, vertically, or diagonally. The following code example shows how to fill an ellipse with a horizontal gradient brush that changes from blue to green as you move from the left edge of the ellipse to the right edge.
[!code-csharp[LinesCurvesAndShapes#124](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#124)]
[!code-vb[LinesCurvesAndShapes#124](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#124)]
The following illustration shows the filled ellipse.
![Filled Shape](./media/aboutgdip02-art21.gif "AboutGdip02_Art21")
A path gradient brush can be configured to change color as you move from the center of a shape toward the edge.
![Filled Shape](./media/aboutgdip02-art22.gif "AboutGdip02_Art22")
Path gradient brushes are quite flexible. The gradient brush used to fill the triangle in the following illustration changes gradually from red at the center to each of three different colors at the vertices.
![Filled Shape](./media/aboutgdip02-art23.gif "AboutGdip02_Art23")
## See also
- <xref:System.Drawing.SolidBrush?displayProperty=nameWithType>
- <xref:System.Drawing.Drawing2D.HatchBrush?displayProperty=nameWithType>
- <xref:System.Drawing.TextureBrush?displayProperty=nameWithType>
- <xref:System.Drawing.Drawing2D.LinearGradientBrush?displayProperty=nameWithType>
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [How to: Draw a Filled Rectangle on a Windows Form](how-to-draw-a-filled-rectangle-on-a-windows-form.md)
- [How to: Draw a Filled Ellipse on a Windows Form](how-to-draw-a-filled-ellipse-on-a-windows-form.md)
@@ -0,0 +1,35 @@
---
title: "Cardinal Splines in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "splines [Windows Forms], cardinal"
- "GDI+, cardinal splines"
- "cardinal splines"
ms.assetid: 09b3797a-6294-422d-9adf-a5a0a7695c0c
---
# Cardinal Splines in GDI+
A cardinal spline is a sequence of individual curves joined to form a larger curve. The spline is specified by an array of points and a tension parameter. A cardinal spline passes smoothly through each point in the array; there are no sharp corners and no abrupt changes in the tightness of the curve. The following illustration shows a set of points and a cardinal spline that passes through each point in the set.
![Cardinal Spline](./media/aboutgdip02-art09.gif "Aboutgdip02_art09")
## Physical and Mathematical Splines
A physical spline is a thin piece of wood or other flexible material. Before the advent of mathematical splines, designers used physical splines to draw curves. A designer would place the spline on a piece of paper and anchor it to a given set of points. The designer could then create a curve by drawing along the spline with a pen or pencil. A given set of points could yield a variety of curves, depending on the properties of the physical spline. For example, a spline with a high resistance to bending would produce a different curve than an extremely flexible spline.
The formulas for mathematical splines are based on the properties of flexible rods, so the curves produced by mathematical splines are similar to the curves that were once produced by physical splines. Just as physical splines of different tension will produce different curves through a given set of points, mathematical splines with different values for the tension parameter will produce different curves through a given set of points. The following illustration shows four cardinal splines passing through the same set of points. The tension is shown for each spline. A tension of 0 corresponds to infinite physical tension, forcing the curve to take the shortest way (straight lines) between points. A tension of 1 corresponds to no physical tension, allowing the spline to take the path of least total bend. With tension values greater than 1, the curve behaves like a compressed spring, pushed to take a longer path.
![Cardinal Splines](./media/aboutgdip02-art10.gif "Aboutgdip02_art10")
The four splines in the preceding illustration share the same tangent line at the starting point. The tangent is the line drawn from the starting point to the next point along the curve. Likewise, the shared tangent at the ending point is the line drawn from the ending point to the previous point on the curve.
To draw a cardinal spline, you need an instance of the <xref:System.Drawing.Graphics> class, a <xref:System.Drawing.Pen>, and an array of <xref:System.Drawing.Point> objects The instance of the <xref:System.Drawing.Graphics> class provides the <xref:System.Drawing.Graphics.DrawCurve%2A> method, which draws the spline, and the <xref:System.Drawing.Pen> stores attributes of the spline, such as line width and color. The array of <xref:System.Drawing.Point> objects stores the points that the curve will pass through. The following code example shows how to draw a cardinal spline that passes through the points in `myPointArray`. The third parameter is the tension.
[!code-csharp[LinesCurvesAndShapes#31](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#31)]
[!code-vb[LinesCurvesAndShapes#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#31)]
## See also
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [Constructing and Drawing Curves](constructing-and-drawing-curves.md)
@@ -0,0 +1,38 @@
---
title: "How to: Support COM Interop by Displaying a Windows Form with the ShowDialog Method"
ms.date: "03/30/2017"
helpviewer_keywords:
- "COM [Windows Forms]"
- "Windows Forms, unmanaged"
- "COM interop [Windows Forms], calling methods"
- "ActiveX controls [Windows Forms], COM interop"
- "Windows Forms, interop"
ms.assetid: 87aac8ad-3c04-43b3-9b0c-d0b00df9ee74
---
# How to: Support COM Interop by Displaying a Windows Form with the ShowDialog Method
You can resolve Component Object Model (COM) interoperability problems by displaying your Windows Form on a .NET Framework message loop, which is created by using the <xref:System.Windows.Forms.Application.Run%2A?displayProperty=nameWithType> method.
To make a form work correctly from a COM client application, you must run it on a Windows Forms message loop. To do this, use one of the following approaches:
- Use the <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method to display the Windows Form;
- Display each Windows Form on a separate thread. For more information, see [How to: Support COM Interop by Displaying Each Windows Form on Its Own Thread](how-to-support-com-interop-by-displaying-each-windows-form-on-its-own-thread.md).
## Procedure
Using the <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method can be the easiest way to display a form on a .NET Framework message loop because, of all the approaches, it requires the least code to implement.
The <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method suspends the unmanaged application's message loop and displays the form as a dialog box. Because the host application's message loop has been suspended, the <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method creates a new .NET Framework message loop to process the form's messages.
The disadvantage of using the <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method is that the form will be opened as a modal dialog box. This behavior blocks any user interface (UI) in the calling application while the Windows Form is open. When the user exits the form, the .NET Framework message loop closes and the earlier application's message loop starts running again.
You can create a class library in Windows Forms which has a method to show the form, and then build the class library for COM interop. You can use this DLL file from Visual Basic 6.0 or Microsoft Foundation Classes (MFC), and from either of these environments you can call the <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method to display the form.
#### To support COM interop by displaying a windows form with the ShowDialog method
- Replace all calls to the <xref:System.Windows.Forms.Form.Show%2A?displayProperty=nameWithType> method with calls to the <xref:System.Windows.Forms.Form.ShowDialog%2A?displayProperty=nameWithType> method in your .NET Framework component.
## See also
- [Exposing .NET Framework Components to COM](https://docs.microsoft.com/dotnet/framework/interop/exposing-dotnet-components-to-co)
- [How to: Support COM Interop by Displaying Each Windows Form on Its Own Thread](how-to-support-com-interop-by-displaying-each-windows-form-on-its-own-thread.md)
- [Windows Forms and Unmanaged Applications](windows-forms-and-unmanaged-applications.md)
@@ -0,0 +1,21 @@
---
title: "Constructing and Drawing Curves"
ms.date: "03/30/2017"
helpviewer_keywords:
- "drawing [Windows Forms], curves"
- "examples [Windows Forms], drawing curves"
- "curves [Windows Forms], drawing"
ms.assetid: 76e92623-4130-4644-b867-faca58bdb3a2
---
# Constructing and Drawing Curves
GDI+ supports several types of curves: ellipses, arcs, cardinal splines, and Bézier splines. An ellipse is defined by its bounding rectangle; an arc is a portion of an ellipse defined by a starting angle and a sweep angle. A cardinal spline is defined by an array of points and a tension parameter — the curve passes smoothly through each point in the array, and the tension parameter influences the way the curve bends. A Bézier spline is defined by two endpoints and two control points the curve does not pass through the control points, but the control points influence the direction and bend as the curve goes from one endpoint to the other.
## In This Section
[How to: Draw Cardinal Splines](how-to-draw-cardinal-splines.md)
Describes cardinal splines and how to draw them.
[How to: Draw a Single Bézier Spline](how-to-draw-a-single-bezier-spline.md)
Describes a Bézier spline and how to draw one.
[How to: Draw a Sequence of Bézier Splines](how-to-draw-a-sequence-of-bezier-splines.md)
Explains how to draw several Bézier splines in sequence.
@@ -0,0 +1,29 @@
---
title: "Constructing and Drawing Paths"
ms.date: "03/30/2017"
helpviewer_keywords:
- "paths [Windows Forms], drawing"
- "drawing paths [Windows Forms]"
- "graphics paths [Windows Forms], creating"
- "graphics paths [Windows Forms], drawing"
- "examples [Windows Forms], drawing paths"
ms.assetid: f16ec921-56cf-46d1-9741-d7316ad06b23
---
# Constructing and Drawing Paths
A path is a sequence of graphics primitives (lines, rectangles, curves, text, and the like) that can be manipulated and drawn as a single unit. A path can be divided into *figures* that are either open or closed. A figure can contain several primitives.
You can draw a path by calling the <xref:System.Drawing.Graphics.DrawPath%2A> method of the <xref:System.Drawing.Graphics> class, and you can fill a path by calling the <xref:System.Drawing.Graphics.FillPath%2A> method of the <xref:System.Drawing.Graphics> class.
## In This Section
[How to: Create Figures from Lines, Curves, and Shapes](how-to-create-figures-from-lines-curves-and-shapes.md)
Shows how to use a <xref:System.Drawing.Drawing2D.GraphicsPath> to create figures.
[How to: Fill Open Figures](how-to-fill-open-figures.md)
Explains how to fill a <xref:System.Drawing.Drawing2D.GraphicsPath>.
[How to: Flatten a Curved Path into a Line](how-to-flatten-a-curved-path-into-a-line.md)
Shows how to flatten a <xref:System.Drawing.Drawing2D.GraphicsPath>.
## Reference
<xref:System.Drawing.Drawing2D.GraphicsPath>
Describes this class and contains links to all of its members.
@@ -0,0 +1,19 @@
---
title: "Control Help Using ToolTips"
ms.date: "03/30/2017"
helpviewer_keywords:
- "Help [Windows Forms], tooltips for controls"
- "controls [Windows Forms], tooltips"
- "tooltips [Windows Forms], for controls"
- "Help [Windows Forms], adding to dialog boxes"
ms.assetid: 03263f94-e539-440b-a67c-8b9110c96dcd
---
# Control Help Using ToolTips
You can use the <xref:System.Windows.Forms.ToolTip> component to display a brief, specialized Help message for individual controls on Windows Forms. The <xref:System.Windows.Forms.ToolTip> component provides a property that specifies the text displayed for each control on the form. For more information about working with the <xref:System.Windows.Forms.ToolTip> component in this way, see [How to: Set ToolTips for Controls on a Windows Form at Design Time](../controls/how-to-set-tooltips-for-controls-on-a-windows-form-at-design-time.md). Additionally, you can configure the <xref:System.Windows.Forms.ToolTip> component so that there is a delay before it is shown. For more information, see [How to: Change the Delay of the Windows Forms ToolTip Component](../controls/how-to-change-the-delay-of-the-windows-forms-tooltip-component.md).
## See also
- [How to: Display Pop-up Help](how-to-display-pop-up-help.md)
- [ToolTip Component](../controls/tooltip-component-windows-forms.md)
- [Integrating User Help in Windows Forms](integrating-user-help-in-windows-forms.md)
- [Windows Forms](../index.yml)
@@ -0,0 +1,32 @@
---
title: "Coordinate Systems and Transformations"
ms.date: "03/30/2017"
helpviewer_keywords:
- "transformations"
- "graphics [Windows Forms], transformations"
- "matrix transformations"
ms.assetid: 9fef7451-bddf-4c68-9350-400d1f1f25a3
---
# Coordinate Systems and Transformations
GDI+ provides a world transformation and a page transformation so that you can transform (rotate, scale, translate, and so on) the items you draw. The two transformations also allow you to work in a variety of coordinate systems.
## In This Section
[Types of Coordinate Systems](types-of-coordinate-systems.md)
Introduces coordinates systems and transformations.
[Matrix Representation of Transformations](matrix-representation-of-transformations.md)
Discusses using matrices for coordinate transformations.
[Global and Local Transformations](global-and-local-transformations.md)
Discusses global and local transformations.
## Reference
<xref:System.Drawing.Drawing2D.Matrix>
Encapsulates a 3-by-3 affine matrix that represents a geometric transform.
## Related Sections
[Using Transformations in Managed GDI+](using-transformations-in-managed-gdi.md)
Provides a list of topics that provide more information about how to use matrix transformations.
[About GDI+ Managed Code](about-gdi-managed-code.md)
Contains a list of topics describing the graphics constructs you can use in the .NET Framework.
@@ -0,0 +1,46 @@
---
title: "Cropping and Scaling Images in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "GDI+, scaling images"
- "GDI+, cropping images"
- "images [Windows Forms], cropping"
- "compressing data [Windows Forms], images"
- "images [Windows Forms], expansion"
- "images [Windows Forms], scaling"
- "rectangles [Windows Forms], source"
- "rectangles [Windows Forms], destination"
- "images [Windows Forms], compression"
ms.assetid: ad5daf26-005f-45bc-a2af-e0e97777a21a
---
# Cropping and Scaling Images in GDI+
You can use the <xref:System.Drawing.Graphics.DrawImage%2A> method of the <xref:System.Drawing.Graphics> class to draw and position vector images and raster images. <xref:System.Drawing.Graphics.DrawImage%2A> is an overloaded method, so there are several ways you can supply it with arguments.
## DrawImage Variations
One variation of the <xref:System.Drawing.Graphics.DrawImage%2A> method receives a <xref:System.Drawing.Bitmap> and a <xref:System.Drawing.Rectangle>. The rectangle specifies the destination for the drawing operation; that is, it specifies the rectangle in which to draw the image. If the size of the destination rectangle is different from the size of the original image, the image is scaled to fit the destination rectangle. The following code example shows how to draw the same image three times: once with no scaling, once with an expansion, and once with a compression:
[!code-csharp[System.Drawing.ImagesBitmapsMetafiles#31](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/CS/Class1.cs#31)]
[!code-vb[System.Drawing.ImagesBitmapsMetafiles#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/VB/Class1.vb#31)]
The following illustration shows the three pictures.
![Scaling](./media/aboutgdip03-art06.gif "AboutGdip03_Art06")
Some variations of the <xref:System.Drawing.Graphics.DrawImage%2A> method have a source-rectangle parameter as well as a destination-rectangle parameter. The source-rectangle parameter specifies the portion of the original image to draw. The destination rectangle specifies the rectangle in which to draw that portion of the image. If the size of the destination rectangle is different from the size of the source rectangle, the picture is scaled to fit the destination rectangle.
The following code example shows how to construct a <xref:System.Drawing.Bitmap> from the file Runner.jpg. The entire image is drawn with no scaling at (0, 0). Then a small portion of the image is drawn twice: once with a compression and once with an expansion.
[!code-csharp[System.Drawing.ImagesBitmapsMetafiles#32](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/CS/Class1.cs#32)]
[!code-vb[System.Drawing.ImagesBitmapsMetafiles#32](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/VB/Class1.vb#32)]
The following illustration shows the unscaled image, and the compressed and expanded image portions.
![Cropping and Scaling](./media/aboutgdip03-art07.gif "AboutGdip03_Art07")
## See also
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
- [Working with Images, Bitmaps, Icons, and Metafiles](working-with-images-bitmaps-icons-and-metafiles.md)
@@ -0,0 +1,24 @@
---
title: "Display of Asian Characters with the ImeMode Property"
ms.date: "03/30/2017"
helpviewer_keywords:
- "Asian languages [Windows Forms], displaying with ImeMode"
- "Chinese characters [Windows Forms], displaying with ImeMode"
- "IME mode"
- "Japanese characters [Windows Forms], displaying with ImeMode"
- "international applications [Windows Forms], character display"
- "international characters"
- "Korean characters"
- "Asian languages"
- "Input Method Editor (IME), mode"
- "localization [Windows Forms], character sets"
- "globalization [Windows Forms], character sets"
ms.assetid: c60ae399-0dab-4f07-9dea-6dbfb15ec0ae
---
# Display of Asian Characters with the ImeMode Property
The <xref:System.Windows.Forms.Control.ImeMode%2A> property is used by forms and controls to force a specific mode for an input method editor (IME). The IME is an essential component for writing Chinese, Japanese, and Korean scripts, since these writing systems have more characters than can be encoded for a regular keyboard. For example, you may want to allow only ASCII characters in a particular text box. In such a case you can set the <xref:System.Windows.Forms.Control.ImeMode%2A> property to <xref:System.Windows.Forms.ImeMode> and users will only be able to enter ASCII characters for that particular text box. The default value of the <xref:System.Windows.Forms.Control.ImeMode%2A> property is <xref:System.Windows.Forms.ImeMode>, so if you set the property for a form, all controls on the form will inherit that setting. For more information, see <xref:System.Windows.Forms.Control.ImeMode%2A>
) and <xref:System.Windows.Forms.ImeMode>.
## See also
- [Globalizing Windows Forms applications](globalizing-windows-forms.md)
@@ -0,0 +1,37 @@
---
title: "Double Buffered Graphics"
ms.date: "03/30/2017"
helpviewer_keywords:
- "double buffering"
- "graphics [Windows Forms], double-buffered"
- "flicker [Windows Forms], reducing with double buffering"
- "examples [Windows Forms], double-buffered graphics"
ms.assetid: 4f6fef99-0972-436e-9d73-0167e4033f71
---
# Double Buffered Graphics
Flicker is a common problem when programming graphics. Graphics operations that require multiple complex painting operations can cause the rendered images to appear to flicker or have an otherwise unacceptable appearance. To address these problems, the .NET Framework provides access to double buffering.
Double buffering uses a memory buffer to address the flicker problems associated with multiple paint operations. When double buffering is enabled, all paint operations are first rendered to a memory buffer instead of the drawing surface on the screen. After all paint operations are completed, the memory buffer is copied directly to the drawing surface associated with it. Because only one graphics operation is performed on the screen, the image flickering associated with complex painting operations is eliminated.
## Default Double Buffering
The easiest way to use double buffering in your applications is to use the default double buffering for forms and controls that is provided by the .NET Framework. You can enable default double buffering for your Windows Forms and authored Windows controls by setting the <xref:System.Windows.Forms.Control.DoubleBuffered%2A> property to `true` or by using the <xref:System.Windows.Forms.Control.SetStyle%2A> method. For more information, see [How to: Reduce Graphics Flicker with Double Buffering for Forms and Controls](how-to-reduce-graphics-flicker-with-double-buffering-for-forms-and-controls.md).
## Manually Managing Buffered Graphics
For more advanced double buffering scenarios, such as animation or advanced memory management, you can use the .NET Framework classes to implement your own double-buffering logic. The class responsible for allocating and managing individual graphics buffers is the <xref:System.Drawing.BufferedGraphicsContext> class. Every application domain has its own default <xref:System.Drawing.BufferedGraphicsContext> instance that manages all of the default double buffering for that application. In most cases there will be only one application domain per application, so there is generally one default <xref:System.Drawing.BufferedGraphicsContext> per application. Default <xref:System.Drawing.BufferedGraphicsContext> instances are managed by the <xref:System.Drawing.BufferedGraphicsManager> class. You can retrieve a reference to the default <xref:System.Drawing.BufferedGraphicsContext> instance by calling the <xref:System.Drawing.BufferedGraphicsManager.Current%2A>. You can also create a dedicated <xref:System.Drawing.BufferedGraphicsContext> instance, which can improve performance for graphically intensive applications. For information on how to create a <xref:System.Drawing.BufferedGraphicsContext> instance, see [How to: Manually Manage Buffered Graphics](how-to-manually-manage-buffered-graphics.md).
## Manually Displaying Buffered Graphics
You can use an instance of the <xref:System.Drawing.BufferedGraphicsContext> class to create graphics buffers by calling the <xref:System.Drawing.BufferedGraphicsContext.Allocate%2A?displayProperty=nameWithType>, which returns an instance of the <xref:System.Drawing.BufferedGraphics> class. A <xref:System.Drawing.BufferedGraphics> object manages a memory buffer that is associated with a rendering surface, such as a form or control.
After it is instantiated, the <xref:System.Drawing.BufferedGraphics> class manages rendering to an in-memory graphics buffer. You can render graphics to the memory buffer through the <xref:System.Drawing.BufferedGraphics.Graphics%2A>, which exposes a <xref:System.Drawing.Graphics> object that directly represents the memory buffer. You can paint to this <xref:System.Drawing.Graphics> object just as you would to a <xref:System.Drawing.Graphics> object that represents a drawing surface. After all the graphics have been drawn to the buffer, you can use the <xref:System.Drawing.BufferedGraphics.Render%2A?displayProperty=nameWithType> to copy the contents of the buffer to the drawing surface on the screen.
For more information on using the <xref:System.Drawing.BufferedGraphics> class, see [Manually Rendering Buffered Graphics](how-to-manually-render-buffered-graphics.md). For more information on rendering graphics, see [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
## See also
- <xref:System.Drawing.BufferedGraphics>
- <xref:System.Drawing.BufferedGraphicsContext>
- <xref:System.Drawing.BufferedGraphicsManager>
- [How to: Manually Render Buffered Graphics](how-to-manually-render-buffered-graphics.md)
- [How to: Reduce Graphics Flicker with Double Buffering for Forms and Controls](how-to-reduce-graphics-flicker-with-double-buffering-for-forms-and-controls.md)
- [How to: Manually Manage Buffered Graphics](how-to-manually-manage-buffered-graphics.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
@@ -0,0 +1,39 @@
---
title: "Drag-and-Drop Operations and Clipboard Support"
ms.date: "03/30/2017"
helpviewer_keywords:
- "drag and drop [Windows Forms]"
- "drag and drop [Windows Forms], Windows Forms"
- "Clipboard [Windows Forms], Windows Forms"
ms.assetid: 7cce79b6-5835-46fd-b690-73f12ad368b2
---
# Drag-and-Drop Operations and Clipboard Support
You can enable user drag-and-drop operations within a Windows-based application by handling a series of events, most notably the <xref:System.Windows.Forms.Control.DragEnter>, <xref:System.Windows.Forms.Control.DragLeave>, and <xref:System.Windows.Forms.Control.DragDrop> events.
You can also implement user cut/copy/paste support and user data transfer to the Clipboard within your Windows-based applications by using simple method calls.
## In This Section
[Walkthrough: Performing a Drag-and-Drop Operation in Windows Forms](walkthrough-performing-a-drag-and-drop-operation-in-windows-forms.md)
Explains how to start a drag-and-drop operation.
[How to: Perform Drag-and-Drop Operations Between Applications](how-to-perform-drag-and-drop-operations-between-applications.md)
Illustrates how to accomplish drag-and-drop operations across applications.
[How to: Add Data to the Clipboard](how-to-add-data-to-the-clipboard.md)
Describes a way to programmatically insert information on the Clipboard.
[How to: Retrieve Data from the Clipboard](how-to-retrieve-data-from-the-clipboard.md)
Describes how to access the data stored on the Clipboard.
## Related Sections
[Drag-and-Drop Functionality in Windows Forms](../drag-and-drop-functionality-in-windows-forms.md)
Describes the methods, events, and classes used to implement drag-and-drop behavior.
<xref:System.Windows.Forms.Control.QueryContinueDrag>
Describes the intricacies of the event that asks permission to continue the drag operation.
<xref:System.Windows.Forms.Control.DoDragDrop%2A>
Describes the intricacies of the method that is central to beginning a drag operation.
<xref:System.Windows.Forms.Clipboard>
Also see [How to: Send Data to the Active MDI Child](how-to-send-data-to-the-active-mdi-child.md).
@@ -0,0 +1,52 @@
---
title: "Drawing, Positioning, and Cloning Images in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "raster images [Windows Forms]"
- "images [Windows Forms], positioning"
- "drawing [Windows Forms], images"
- "drawing [Windows Forms], raster images"
- "images [Windows Forms], cloning"
- "images [Windows Forms], drawing"
- "GDI+, drawing images"
- "GDI+, cloning images"
- "GDI+, positioning images"
ms.assetid: 09f0c07a-19c0-43b4-90a2-862a10545ce8
---
# Drawing, Positioning, and Cloning Images in GDI+
You can use the <xref:System.Drawing.Bitmap> class to load and display raster images, and you can use the <xref:System.Drawing.Imaging.Metafile> class to load and display vector images. The <xref:System.Drawing.Bitmap> and <xref:System.Drawing.Imaging.Metafile> classes inherit from the <xref:System.Drawing.Image> class. To display a vector image, you need an instance of the <xref:System.Drawing.Graphics> class and a <xref:System.Drawing.Imaging.Metafile>. To display a raster image, you need an instance of the <xref:System.Drawing.Graphics> class and a <xref:System.Drawing.Bitmap>. The instance of the <xref:System.Drawing.Graphics> class provides the <xref:System.Drawing.Graphics.DrawImage%2A> method, which receives the <xref:System.Drawing.Imaging.Metafile> or <xref:System.Drawing.Bitmap> as an argument.
## File Types and Cloning
The following code example shows how to construct a <xref:System.Drawing.Bitmap> from the file Climber.jpg and displays the bitmap. The destination point for the upper-left corner of the image, (10, 10), is specified in the second and third parameters.
[!code-csharp[System.Drawing.ImagesBitmapsMetafiles#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/CS/Class1.cs#11)]
[!code-vb[System.Drawing.ImagesBitmapsMetafiles#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/VB/Class1.vb#11)]
The following illustration shows the image.
![Image Sample](./media/aboutgdip03-art04.gif "AboutGdip03_Art04")
You can construct <xref:System.Drawing.Bitmap> objects from a variety of graphics file formats: BMP, GIF, JPEG, EXIF, PNG, TIFF, and ICON.
The following code example shows how to construct <xref:System.Drawing.Bitmap> objects from a variety of file types and then displays the bitmaps.
[!code-csharp[System.Drawing.ImagesBitmapsMetafiles#12](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/CS/Class1.cs#12)]
[!code-vb[System.Drawing.ImagesBitmapsMetafiles#12](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/VB/Class1.vb#12)]
The <xref:System.Drawing.Bitmap> class provides a <xref:System.Drawing.Bitmap.Clone%2A> method that you can use to make a copy of an existing <xref:System.Drawing.Bitmap>. The <xref:System.Drawing.Bitmap.Clone%2A> method has a source rectangle parameter that you can use to specify the portion of the original bitmap that you want to copy. The following code example shows how to create a <xref:System.Drawing.Bitmap> by cloning the top half of an existing <xref:System.Drawing.Bitmap>. Then both images are drawn.
[!code-csharp[System.Drawing.ImagesBitmapsMetafiles#13](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/CS/Class1.cs#13)]
[!code-vb[System.Drawing.ImagesBitmapsMetafiles#13](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ImagesBitmapsMetafiles/VB/Class1.vb#13)]
The following illustration shows the two images.
![Cropping](./media/aboutgdip03-art05.gif "AboutGdip03_Art05")
## See also
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
- [How to: Create Graphics Objects for Drawing](how-to-create-graphics-objects-for-drawing.md)
- [Working with Images, Bitmaps, Icons, and Metafiles](working-with-images-bitmaps-icons-and-metafiles.md)
@@ -0,0 +1,24 @@
---
title: "Effects of Modifying a Base Form's Appearance"
ms.date: "03/30/2017"
helpviewer_keywords:
- "parent forms [Windows Forms]"
- "inherited forms [Windows Forms], modifications to base form"
- "Windows Forms, base form appearance"
- "base forms"
- "inheritance [Windows Forms], forms"
ms.assetid: 1c3f2b29-a05c-4c6f-aa1a-4e66b94f343a
---
# Effects of modifying a base form's appearance
During application development, you may often need to change the appearance of the base form from which other forms in the project (or in other projects) are inheriting.
At design time, changes to the base form's appearance (be it the setting of properties or the addition and subtraction of controls) are reflected on inherited forms when the project containing the base form is built. It is not sufficient for you to simply save the changes to the base form. To build a project, choose **Build** from the **Build** menu.
Modifications made to the base form at run time have no affect on inherited forms that are already instantiated.
## See also
- [base](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/base)
- [How to: Inherit Windows Forms](how-to-inherit-windows-forms.md)
- [Windows Forms Visual Inheritance](windows-forms-visual-inheritance.md)
@@ -0,0 +1,51 @@
---
title: "Ellipses and Arcs in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "arcs"
- "GDI+, arcs"
- "drawing [Windows Forms], ellipses"
- "GDI+, ellipses"
- "ellipses"
- "drawing [Windows Forms], arcs"
ms.assetid: 34f35133-a835-4ca4-81f6-0dfedee8b683
---
# Ellipses and Arcs in GDI+
You can easily draw ellipses and arcs using the <xref:System.Drawing.Graphics.DrawEllipse%2A> and <xref:System.Drawing.Graphics.DrawArc%2A> methods of the <xref:System.Drawing.Graphics> class.
## Drawing an Ellipse
To draw an ellipse, you need a <xref:System.Drawing.Graphics> object and a <xref:System.Drawing.Pen> object. The <xref:System.Drawing.Graphics> object provides the <xref:System.Drawing.Graphics.DrawEllipse%2A> method, and the <xref:System.Drawing.Pen> object stores attributes, such as width and color, of the line used to render the ellipse. The <xref:System.Drawing.Pen> object is passed as one of the arguments to the <xref:System.Drawing.Graphics.DrawEllipse%2A> method. The remaining arguments passed to the <xref:System.Drawing.Graphics.DrawEllipse%2A> method specify the bounding rectangle for the ellipse. The following illustration shows an ellipse along with its bounding rectangle.
![Ellipses and arcs](./media/aboutgdip02-art05.gif "Aboutgdip02_art05")
The following example draws an ellipse; the bounding rectangle has a width of 80, a height of 40, and an upper-left corner of (100, 50):
[!code-csharp[LinesCurvesAndShapes#51](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#51)]
[!code-vb[LinesCurvesAndShapes#51](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#51)]
<xref:System.Drawing.Graphics.DrawEllipse%2A> is an overloaded method of the <xref:System.Drawing.Graphics> class, so there are several ways you can supply it with arguments. For example, you can construct a <xref:System.Drawing.Rectangle> and pass the <xref:System.Drawing.Rectangle> to the <xref:System.Drawing.Graphics.DrawEllipse%2A> method as an argument:
[!code-csharp[LinesCurvesAndShapes#52](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#52)]
[!code-vb[LinesCurvesAndShapes#52](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#52)]
## Drawing an Arc
An arc is a portion of an ellipse. To draw an arc, you call the <xref:System.Drawing.Graphics.DrawArc%2A> method of the <xref:System.Drawing.Graphics> class. The parameters of the <xref:System.Drawing.Graphics.DrawArc%2A> method are the same as the parameters of the <xref:System.Drawing.Graphics.DrawEllipse%2A> method, except that <xref:System.Drawing.Graphics.DrawArc%2A> requires a starting angle and sweep angle. The following example draws an arc with a starting angle of 30 degrees and a sweep angle of 180 degrees:
[!code-csharp[LinesCurvesAndShapes#53](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#53)]
[!code-vb[LinesCurvesAndShapes#53](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#53)]
The following illustration shows the arc, the ellipse, and the bounding rectangle.
![Ellipses and arcs](./media/aboutgdip02-art06.gif "Aboutgdip02_art06")
## See also
- <xref:System.Drawing.Graphics?displayProperty=nameWithType>
- <xref:System.Drawing.Pen?displayProperty=nameWithType>
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [How to: Create Graphics Objects for Drawing](how-to-create-graphics-objects-for-drawing.md)
- [How to: Create a Pen](how-to-create-a-pen.md)
- [How to: Draw an Outlined Shape](how-to-draw-an-outlined-shape.md)
@@ -0,0 +1,60 @@
---
title: "Getting Started with Graphics Programming"
description: Get started using GDI+ in a Windows Forms application. Learn how to complete several GDI+ tasks, such as drawing and filling shapes and text.
ms.date: "03/30/2017"
helpviewer_keywords:
- "graphics [Windows Forms], getting started"
- "getting started [Windows Forms], graphics programming"
- "examples [Windows Forms], graphics"
- "drawing"
ms.assetid: eb0f6d6f-9e52-4167-9592-ff4b82fb5869
---
# Getting Started with Graphics Programming
This section shows how to get started using GDI+ in a Windows Forms application. The following topics show how to complete several GDI+ tasks such as drawing and filling shapes and text.
## In This Section
[How to: Create Graphics Objects for Drawing](how-to-create-graphics-objects-for-drawing.md)
Shows how to create a <xref:System.Drawing.Graphics> object for drawing.
[How to: Create a Pen](how-to-create-a-pen.md)
Shows how to create a pen.
[How to: Set the Color of a Pen](how-to-set-the-color-of-a-pen.md)
Demonstrates how to set the color of a pen.
[How to: Create a Solid Brush](how-to-create-a-solid-brush.md)
Describes how to create a solid brush.
[How to: Draw a Line on a Windows Form](how-to-draw-a-line-on-a-windows-form.md)
Demonstrates how to draw a line.
[How to: Draw an Outlined Shape](how-to-draw-an-outlined-shape.md)
Describes how to draw a shape.
[How to: Draw a Filled Rectangle on a Windows Form](how-to-draw-a-filled-rectangle-on-a-windows-form.md)
Explains how to draw a rectangle.
[How to: Draw a Filled Ellipse on a Windows Form](how-to-draw-a-filled-ellipse-on-a-windows-form.md)
Shows how to draw a filled ellipse.
[How to: Draw Text on a Windows Form](how-to-draw-text-on-a-windows-form.md)
Describes how to draw text.
[How to: Draw Vertical Text on a Windows Form](how-to-draw-vertical-text-on-a-windows-form.md)
Shows how to draw vertical text.
[How to: Render Images with GDI+](how-to-render-images-with-gdi.md)
Demonstrates how to draw images.
[How to: Create a Shaped Windows Form](how-to-create-a-shaped-windows-form.md)
Explains how to change the shape of a form.
[How to: Copy Pixels for Reducing Flicker in Windows Forms](how-to-copy-pixels-for-reducing-flicker-in-windows-forms.md)
Explains how to copy pixels from one area to another.
## Reference
<xref:System.Drawing>
Describes this namespace and has links to all its members.
<xref:System.Windows.Forms>
Describes this namespace and has links to all of its members.
@@ -0,0 +1,60 @@
---
title: "Global and Local Transformations"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "matrices [Windows Forms], using transformations"
- "transformations [Windows Forms], global"
- "transformations [Windows Forms], local"
ms.assetid: b601d66d-d572-4f11-9d2e-92f0dc8893f3
---
# Global and Local Transformations
A global transformation is a transformation that applies to every item drawn by a given <xref:System.Drawing.Graphics> object. In contrast, a local transformation is a transformation that applies to a specific item to be drawn.
## Global Transformations
To create a global transformation, construct a <xref:System.Drawing.Graphics> object, and then manipulate its <xref:System.Drawing.Graphics.Transform%2A> property. The <xref:System.Drawing.Graphics.Transform%2A> property is a <xref:System.Drawing.Drawing2D.Matrix> object, so it can hold any sequence of affine transformations. The transformation stored in the <xref:System.Drawing.Graphics.Transform%2A> property is called the world transformation. The <xref:System.Drawing.Graphics> class provides several methods for building up a composite world transformation: <xref:System.Drawing.Graphics.MultiplyTransform%2A>, <xref:System.Drawing.Graphics.RotateTransform%2A>, <xref:System.Drawing.Graphics.ScaleTransform%2A>, and <xref:System.Drawing.Graphics.TranslateTransform%2A>. The following example draws an ellipse twice: once before creating a world transformation and once after. The transformation first scales by a factor of 0.5 in the y direction, then translates 50 units in the x direction, and then rotates 30 degrees.
[!code-csharp[System.Drawing.CoordinateSystems#21](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/CS/Class1.cs#21)]
[!code-vb[System.Drawing.CoordinateSystems#21](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/VB/Class1.vb#21)]
The following illustration shows the matrices involved in the transformation.
![Transformations](./media/aboutgdip05-art14.gif "AboutGdip05_art14")
> [!NOTE]
> In the preceding example, the ellipse is rotated about the origin of the coordinate system, which is at the upper-left corner of the client area. This produces a different result than rotating the ellipse about its own center.
## Local Transformations
A local transformation applies to a specific item to be drawn. For example, a <xref:System.Drawing.Drawing2D.GraphicsPath> object has a <xref:System.Drawing.Drawing2D.GraphicsPath.Transform%2A> method that allows you to transform the data points of that path. The following example draws a rectangle with no transformation and a path with a rotation transformation. (Assume that there is no world transformation.)
[!code-csharp[System.Drawing.CoordinateSystems#22](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/CS/Class1.cs#22)]
[!code-vb[System.Drawing.CoordinateSystems#22](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/VB/Class1.vb#22)]
You can combine the world transformation with local transformations to achieve a variety of results. For example, you can use the world transformation to revise the coordinate system and use local transformations to rotate and scale objects drawn on the new coordinate system.
Suppose you want a coordinate system that has its origin 200 pixels from the left edge of the client area and 150 pixels from the top of the client area. Furthermore, assume that you want the unit of measure to be the pixel, with the x-axis pointing to the right and the y-axis pointing up. The default coordinate system has the y-axis pointing down, so you need to perform a reflection across the horizontal axis. The following illustration shows the matrix of such a reflection.
![Transformations](./media/aboutgdip05-art15.gif "AboutGdip05_art15")
Next, assume you need to perform a translation 200 units to the right and 150 units down.
The following example establishes the coordinate system just described by setting the world transformation of a <xref:System.Drawing.Graphics> object.
[!code-csharp[System.Drawing.CoordinateSystems#23](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/CS/Class1.cs#23)]
[!code-vb[System.Drawing.CoordinateSystems#23](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/VB/Class1.vb#23)]
The following code (placed at the end of the preceding example) creates a path that consists of a single rectangle with its lower-left corner at the origin of the new coordinate system. The rectangle is filled once with no local transformation and once with a local transformation. The local transformation consists of a horizontal scaling by a factor of 2 followed by a 30-degree rotation.
[!code-csharp[System.Drawing.CoordinateSystems#24](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/CS/Class1.cs#24)]
[!code-vb[System.Drawing.CoordinateSystems#24](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.CoordinateSystems/VB/Class1.vb#24)]
The following illustration shows the new coordinate system and the two rectangles.
![Transformations](./media/aboutgdip05-art16.gif "AboutGdip05_art16")
## See also
- [Coordinate Systems and Transformations](coordinate-systems-and-transformations.md)
- [Using Transformations in Managed GDI+](using-transformations-in-managed-gdi.md)
@@ -0,0 +1,46 @@
---
title: Globalize apps
description: This topic presents a list of topics related to globalizing Windows Forms applications.
ms.date: "06/08/2018"
helpviewer_keywords:
- "Windows Forms, globalization"
- "localization [Windows Forms]"
- "globalization [Windows Forms]"
- "world-ready applications"
- "global applications [Windows Forms], creating Windows Forms"
ms.assetid: 72f6cd92-83be-45ec-aa37-9cb8e3ebc3c5
---
# Globalizing Windows Forms applications
*Globalization* is the process of designing and developing a software product that functions for multiple cultures.
## In this section
[International Fonts in Windows Forms and Controls](international-fonts-in-windows-forms-and-controls.md)
Explains when and how to select fonts for display of international characters on Windows Forms.
[Bi-Directional Support for Windows Forms Applications](bi-directional-support-for-windows-forms-applications.md)
Explains how to create Windows-based applications that support bi-directional (right-to-left) languages.
[Display of Asian Characters with the ImeMode Property](display-of-asian-characters-with-the-imemode-property.md)
Introduces the `ImeMode` property, which is used to control the type of input a Windows Form or control accepts.
## Related sections
- [Globalizing and localizing .NET applications](https://docs.microsoft.com/dotnet/standard/globalization-localization/index)
- [Walkthrough: Downloading Satellite Assemblies on Demand with the ClickOnce Deployment API Using the Designer](/visualstudio/deployment/walkthrough-downloading-satellite-assemblies-on-demand-with-the-clickonce-deployment-api-using-the-designer)
- [Localizing ClickOnce Applications](/visualstudio/deployment/localizing-clickonce-applications)
- [Walkthrough: Downloading Satellite Assemblies on Demand with the ClickOnce Deployment API](/visualstudio/deployment/walkthrough-downloading-satellite-assemblies-on-demand-with-the-clickonce-deployment-api)
- [How to: Set the Culture and UI Culture for Windows Forms Globalization](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/b28bx3bh(v=vs.100))
- [How to: Create Mirrored Windows Forms and Controls](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/xwbz5ws0(v=vs.100))
- [How to: Support Localization on Windows Forms Using AutoSize and the TableLayoutPanel Control](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/1zkt8b33(v=vs.100))
- [Walkthrough: Localizing Windows Forms](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/y99d1cd3(v=vs.100))
- [Walkthrough: Creating a Layout That Adjusts Proportion for Localization](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/7k9fa71y(v=vs.100))
@@ -0,0 +1,47 @@
---
title: "Graphics and Drawing"
description: Learn about the Graphics, Pen, Brush, and Color objects, and how to perform such tasks as drawing shapes, drawing text, or displaying images in Windows Forms.
ms.date: "03/30/2017"
helpviewer_keywords:
- "graphics [Windows Forms]"
- "graphics [Windows Forms], using in Windows Forms"
- "GDI+, using in managed code"
- "drawing [Windows Forms]"
ms.assetid: 362532c5-1a06-4257-bdc8-723461009ede
---
# Graphics and Drawing in Windows Forms
The common language runtime uses an advanced implementation of the Windows Graphics Device Interface (GDI) called GDI+. With GDI+ you can create graphics, draw text, and manipulate graphical images as objects. GDI+ is designed to offer performance and ease of use. You can use GDI+ to render graphical images on Windows Forms and controls. Although you cannot use GDI+ directly on Web Forms, you can display graphical images through the Image Web Server control.
In this section, you will find topics that introduce the fundamentals of GDI+ programming. Although not intended to be a comprehensive reference, this section includes information about the <xref:System.Drawing.Graphics>, <xref:System.Drawing.Pen>, <xref:System.Drawing.Brush>, and <xref:System.Drawing.Color> objects, and explains how to perform such tasks as drawing shapes, drawing text, or displaying images. For more information, see [GDI+ Reference](/windows/desktop/gdiplus/-gdiplus-class-gdi-reference).
If you'd like to jump in and get started right away, see [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md). It has topics on how to use code to draw lines, shapes, text, and more on Windows forms.
## In This Section
[Graphics Overview](graphics-overview-windows-forms.md)
Provides an introduction to the graphics-related managed classes.
[About GDI+ Managed Code](about-gdi-managed-code.md)
Provides information about the managed GDI+ classes.
[Using Managed Graphics Classes](using-managed-graphics-classes.md)
Demonstrates how to complete a variety of tasks using the GDI+ managed classes.
## Reference
<xref:System.Drawing>
Provides access to GDI+ basic graphics functionality.
<xref:System.Drawing.Drawing2D>
Provides advanced two-dimensional and vector graphics functionality.
<xref:System.Drawing.Imaging>
Provides advanced GDI+ imaging functionality.
<xref:System.Drawing.Text>
Provides advanced GDI+ typography functionality. The classes in this namespace can be used to create and use collections of fonts.
<xref:System.Drawing.Printing>
Provides printing functionality.
## Related Sections
[Custom Control Painting and Rendering](../controls/custom-control-painting-and-rendering.md)
Details how to provide code for painting controls.
@@ -0,0 +1,42 @@
---
title: "Graphics Overview"
ms.date: "03/30/2017"
helpviewer_keywords:
- "Windows Forms, graphics"
- "Windows Forms, GDI+"
- "graphics [Windows Forms], Windows Forms"
- "GDI+, Windows Forms"
- "graphics"
ms.assetid: 94084509-2c6f-477d-923b-d25d37a36b1c
---
# Graphics Overview (Windows Forms)
GDI+ is a Graphics Device Interface that enables programmers to write device-independent applications. The services of GDI+ are exposed through a set of managed classes.
## In This Section
[Overview of Graphics](overview-of-graphics.md)
Provides a general introduction to GDI+.
[Three Categories of Graphics Services](three-categories-of-graphics-services.md)
Describes the three categories that make up programming with GDI+.
[Structure of the Graphics Interface](structure-of-the-graphics-interface.md)
Describes the managed class interface of GDI+.
## Reference
<xref:System.Drawing>
Provides access to GDI+ basic graphics functionality.
<xref:System.Drawing.Drawing2D>
Provides advanced two-dimensional and vector graphics functionality.
<xref:System.Drawing.Imaging>
Provides advanced GDI+ imaging functionality.
<xref:System.Drawing.Text>
Provides advanced GDI+ typography functionality.
<xref:System.Drawing.Printing>
Provides print-related services.
<xref:System.Windows.Forms.TextRenderer>
Provides GDI text drawing and measuring functionality.
@@ -0,0 +1,69 @@
---
title: "Graphics Paths in GDI+"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "graphics [Windows Forms], paths"
- "GDI+, drawing paths"
- "paths [Windows Forms], drawing"
- "drawing [Windows Forms], paths"
ms.assetid: a5500dec-666c-41fd-9da3-2169dd89c5eb
---
# Graphics Paths in GDI+
Paths are formed by combining lines, rectangles, and simple curves. Recall from the [Vector Graphics Overview](vector-graphics-overview.md) that the following basic building blocks have proven to be the most useful for drawing pictures:
- Lines
- Rectangles
- Ellipses
- Arcs
- Polygons
- Cardinal splines
- Bézier splines
In GDI+, the <xref:System.Drawing.Drawing2D.GraphicsPath> object allows you to collect a sequence of these building blocks into a single unit. The entire sequence of lines, rectangles, polygons, and curves can then be drawn with one call to the <xref:System.Drawing.Graphics.DrawPath%2A> method of the <xref:System.Drawing.Graphics> class. The following illustration shows a path created by combining a line, an arc, a Bézier spline, and a cardinal spline.
![Path](./media/aboutgdip02-art14.gif "Aboutgdip02_art14")
## Using a Path
The <xref:System.Drawing.Drawing2D.GraphicsPath> class provides the following methods for creating a sequence of items to be drawn: <xref:System.Drawing.Drawing2D.GraphicsPath.AddLine%2A>, <xref:System.Drawing.Drawing2D.GraphicsPath.AddRectangle%2A>, <xref:System.Drawing.Drawing2D.GraphicsPath.AddEllipse%2A>, <xref:System.Drawing.Drawing2D.GraphicsPath.AddArc%2A>, <xref:System.Drawing.Drawing2D.GraphicsPath.AddPolygon%2A>, <xref:System.Drawing.Drawing2D.GraphicsPath.AddCurve%2A> (for cardinal splines), and <xref:System.Drawing.Drawing2D.GraphicsPath.AddBezier%2A>. Each of these methods is overloaded; that is, each method supports several different parameter lists. For example, one variation of the <xref:System.Drawing.Drawing2D.GraphicsPath.AddLine%2A> method receives four integers, and another variation of the <xref:System.Drawing.Drawing2D.GraphicsPath.AddLine%2A> method receives two <xref:System.Drawing.Point> objects.
The methods for adding lines, rectangles, and Bézier splines to a path have plural companion methods that add several items to the path in a single call: <xref:System.Drawing.Drawing2D.GraphicsPath.AddLines%2A>, <xref:System.Drawing.Drawing2D.GraphicsPath.AddRectangles%2A>, and <xref:System.Drawing.Drawing2D.GraphicsPath.AddBeziers%2A>. Also, the <xref:System.Drawing.Drawing2D.GraphicsPath.AddCurve%2A> and <xref:System.Drawing.Drawing2D.GraphicsPath.AddArc%2A> methods have companion methods, <xref:System.Drawing.Drawing2D.GraphicsPath.AddClosedCurve%2A> and <xref:System.Drawing.Drawing2D.GraphicsPath.AddPie%2A>, that add a closed curve or pie to the path.
To draw a path, you need a <xref:System.Drawing.Graphics> object, a <xref:System.Drawing.Pen> object, and a <xref:System.Drawing.Drawing2D.GraphicsPath> object. The <xref:System.Drawing.Graphics> object provides the <xref:System.Drawing.Graphics.DrawPath%2A> method, and the <xref:System.Drawing.Pen> object stores attributes, such as width and color, of the line used to render the path. The <xref:System.Drawing.Drawing2D.GraphicsPath> object stores the sequence of lines and curves that make up the path. The <xref:System.Drawing.Pen> object and the <xref:System.Drawing.Drawing2D.GraphicsPath> object are passed as arguments to the <xref:System.Drawing.Graphics.DrawPath%2A> method. The following example draws a path that consists of a line, an ellipse, and a Bézier spline:
[!code-csharp[LinesCurvesAndShapes#101](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#101)]
[!code-vb[LinesCurvesAndShapes#101](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#101)]
The following illustration shows the path.
![Path](./media/aboutgdip02-art15.gif "Aboutgdip02_art15")
In addition to adding lines, rectangles, and curves to a path, you can add paths to a path. This allows you to combine existing paths to form large, complex paths.
[!code-csharp[LinesCurvesAndShapes#102](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#102)]
[!code-vb[LinesCurvesAndShapes#102](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#102)]
There are two other items you can add to a path: strings and pies. A pie is a portion of the interior of an ellipse. The following example creates a path from an arc, a cardinal spline, a string, and a pie:
[!code-csharp[LinesCurvesAndShapes#103](~/samples/snippets/csharp/VS_Snippets_Winforms/LinesCurvesAndShapes/CS/Class1.cs#103)]
[!code-vb[LinesCurvesAndShapes#103](~/samples/snippets/visualbasic/VS_Snippets_Winforms/LinesCurvesAndShapes/VB/Class1.vb#103)]
The following illustration shows the path. Note that a path does not have to be connected; the arc, cardinal spline, string, and pie are separated.
![Paths](./media/aboutgdip02-art16.gif "Aboutgdip02_Art16")
## See also
- <xref:System.Drawing.Drawing2D.GraphicsPath?displayProperty=nameWithType>
- <xref:System.Drawing.Point?displayProperty=nameWithType>
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [How to: Create Graphics Objects for Drawing](how-to-create-graphics-objects-for-drawing.md)
- [Constructing and Drawing Paths](constructing-and-drawing-paths.md)
@@ -0,0 +1,26 @@
---
title: Help Systems
ms.date: "03/30/2017"
helpviewer_keywords:
- "Help [Windows Forms], adding to Windows applications"
- "Windows applications [Windows Forms], providing Help systems"
- "What's This? Help"
- "Help [Windows Forms], Windows Forms"
- "HelpProvider component [Windows Forms], providing Help in Windows applications"
ms.assetid: 2a96a278-432c-41fc-9e3c-5bfedf5e1267
---
# Help Systems in Windows Forms Applications
One of the most important courtesies you, as a developer of applications, can furnish your users with is a competent Help system. This is where they will turn when they become confused or disoriented. Providing a Help system in a Windows-based application is easily done by using the [HelpProvider Component](../controls/helpprovider-component-windows-forms.md).
## Different Types of Help
The Windows Forms <xref:System.Windows.Forms.HelpProvider> component is used to associate an HTML Help 1.x Help file (either a .chm file, produced with the HTML Help Workshop, or an .htm file) with your Windows-based application. The <xref:System.Windows.Forms.HelpProvider> component can be used to provide context-sensitive Help for controls on Windows Forms or specific controls. Additionally, the <xref:System.Windows.Forms.HelpProvider> component can open a Help file to specific areas, such as the main page of a table of contents, an index, or a search function. For general information about the <xref:System.Windows.Forms.HelpProvider> component, see [HelpProvider Component Overview](../controls/helpprovider-component-overview-windows-forms.md). For information on how to use the <xref:System.Windows.Forms.HelpProvider> component to show pop-up Help on Windows Forms, see [How to: Display Pop-up Help](how-to-display-pop-up-help.md). For information on using the <xref:System.Windows.Forms.ToolTip> component to show control-specific Help, see [Control Help Using ToolTips](control-help-using-tooltips.md).
You can generate HTML Help 1.x files with the HTML Help Workshop. For more information on HTML Help, see the "HTML Help Workshop" or the other "HTML Help" topics in MSDN.
## See also
- [Integrating User Help in Windows Forms](integrating-user-help-in-windows-forms.md)
- [HelpProvider Component](../controls/helpprovider-component-windows-forms.md)
- [ToolTip Component](../controls/tooltip-component-windows-forms.md)
- [Windows Forms Overview](../windows-forms-overview.md)
- [Windows Forms](../index.yml)
@@ -0,0 +1,61 @@
---
title: "How to: Add Data to the Clipboard"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "Clipboard [Windows Forms], copying data to"
- "data [Windows Forms], copying to Clipboard"
ms.assetid: 25152454-0e78-40a9-8a9e-a2a5a274e517
---
# How to: Add Data to the Clipboard
The <xref:System.Windows.Forms.Clipboard> class provides methods that you can use to interact with the Windows operating system Clipboard feature. Many applications use the Clipboard as a temporary repository for data. For example, word processors use the Clipboard during cut-and-paste operations. The Clipboard is also useful for transferring data from one application to another.
When you add data to the Clipboard, you can indicate the data format so that other applications can recognize the data if they can use that format. You can also add data to the Clipboard in multiple different formats to increase the number of other applications that can potentially use the data.
A Clipboard format is a string that identifies the format so that an application that uses that format can retrieve the associated data. The <xref:System.Windows.Forms.DataFormats> class provides predefined format names for your use. You can also use your own format names or use the type of an object as its format.
To add data to the Clipboard in one or multiple formats, use the <xref:System.Windows.Forms.Clipboard.SetDataObject%2A> method. You can pass any object to this method, but to add data in multiple formats, you must first add the data to a separate object designed to work with multiple formats. Typically, you will add your data to a <xref:System.Windows.Forms.DataObject>, but you can use any type that implements the <xref:System.Windows.Forms.IDataObject> interface.
In .NET Framework 2.0, you can add data directly to the Clipboard by using new methods designed to make basic Clipboard tasks easier. Use these methods when you work with data in a single, common format such as text.
> [!NOTE]
> All Windows-based applications share the Clipboard. Therefore, the contents are subject to change when you switch to another application.
>
> The <xref:System.Windows.Forms.Clipboard> class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your `Main` method is marked with the <xref:System.STAThreadAttribute> attribute.
>
> An object must be serializable for it to be put on the Clipboard. To make a type serializable, mark it with the <xref:System.SerializableAttribute> attribute. If you pass a non-serializable object to a Clipboard method, the method will fail without throwing an exception. For more information about serialization, see <xref:System.Runtime.Serialization>.
### To add data to the Clipboard in a single, common format
1. Use the <xref:System.Windows.Forms.Clipboard.SetAudio%2A>, <xref:System.Windows.Forms.Clipboard.SetFileDropList%2A>, <xref:System.Windows.Forms.Clipboard.SetImage%2A>, or <xref:System.Windows.Forms.Clipboard.SetText%2A> method. These methods are available only in .NET Framework 2.0.
[!code-csharp[System.Windows.Forms.Clipboard#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/CS/form1.cs#2)]
[!code-vb[System.Windows.Forms.Clipboard#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/vb/form1.vb#2)]
### To add data to the Clipboard in a custom format
1. Use the <xref:System.Windows.Forms.Clipboard.SetData%2A> method with a custom format name. This method is available only in .NET Framework 2.0.
You can also use predefined format names with the <xref:System.Windows.Forms.Clipboard.SetData%2A> method. For more information, see <xref:System.Windows.Forms.DataFormats>.
[!code-csharp[System.Windows.Forms.Clipboard#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/CS/form1.cs#3)]
[!code-vb[System.Windows.Forms.Clipboard#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/vb/form1.vb#3)]
[!code-csharp[System.Windows.Forms.Clipboard#100](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/CS/form1.cs#100)]
[!code-vb[System.Windows.Forms.Clipboard#100](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/vb/form1.vb#100)]
### To add data to the Clipboard in multiple formats
1. Use the <xref:System.Windows.Forms.Clipboard.SetDataObject%2A?displayProperty=nameWithType> method and pass in a <xref:System.Windows.Forms.DataObject> that contains your data. You must use this method to add data to the Clipboard on versions earlier than .NET Framework 2.0.
[!code-csharp[System.Windows.Forms.Clipboard#4](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/CS/form1.cs#4)]
[!code-vb[System.Windows.Forms.Clipboard#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/vb/form1.vb#4)]
[!code-csharp[System.Windows.Forms.Clipboard#100](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/CS/form1.cs#100)]
[!code-vb[System.Windows.Forms.Clipboard#100](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.Clipboard/vb/form1.vb#100)]
## See also
- [Drag-and-Drop Operations and Clipboard Support](drag-and-drop-operations-and-clipboard-support.md)
- [How to: Retrieve Data from the Clipboard](how-to-retrieve-data-from-the-clipboard.md)
@@ -0,0 +1,29 @@
---
title: "How To: Add Multiple Sets of Settings To Your Application in C#"
description: Learn how to add multiple sets of Windows Forms settings to your application in C# by using Visual Studio.
ms.date: "03/30/2017"
helpviewer_keywords:
- "application settings [Windows Forms], multiple sets"
- "application settings [Windows Forms], C#"
ms.assetid: 45007ac6-cf07-4be7-bc38-3f0ef962faf9
---
# How To: Add Multiple Sets of Settings To Your Application in C\#
In some cases, you might want to have multiple sets of settings in an application. For example, if you are developing an application where a particular group of settings is expected to change frequently, it might be wise to separate them all into a single file so that the file can be replaced wholesale, leaving other settings unaffected. Visual Studio allows you to add multiple sets of settings to your project. Additional sets of settings can be accessed via the `Properties.Settings` object.
## Add an Additional Set of Settings
1. In Visual Studio, from the **Project** menu, choose **Add New Item**.
The **Add New Item** dialog box opens.
2. In the **Add New Item** dialog box, select **Settings File**, enter a name for the file, and click **Add** to add a new settings file to your solution.
3. In **Solution Explorer**, drag the new Settings file into the **Properties** folder. This allows your new settings to be available in code.
4. Add and use settings in this file as you would any other settings file. You can access this group of settings via the `Properties.Settings` object.
## See also
- [Using Application Settings and User Settings](using-application-settings-and-user-settings.md)
- [Application Settings Overview](application-settings-overview.md)
@@ -0,0 +1,36 @@
---
title: "How to: Align Drawn Text"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "text [Windows Forms], aligning"
- "Windows Forms, aligning drawn text"
ms.assetid: 83c10a81-1a90-4b5c-98aa-2c6c4b280079
---
# How to: Align Drawn Text
When you perform custom drawing, you may often want to center drawn text on a form or control. You can easily align text drawn with the <xref:System.Drawing.Graphics.DrawString%2A> or <xref:System.Windows.Forms.TextRenderer.DrawText%2A> methods by creating the correct formatting object and setting the appropriate format flags.
### To draw centered text with GDI+ (DrawString)
1. Use a <xref:System.Drawing.StringFormat> with the appropriate <xref:System.Drawing.Graphics.DrawString%2A> method to specify centered text.
[!code-csharp[System.Drawing.AlignDrawnText#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/CS/Form1.cs#10)]
[!code-vb[System.Drawing.AlignDrawnText#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/VB/Form1.vb#10)]
### To draw centered text with GDI (DrawText)
1. Use the <xref:System.Windows.Forms.TextFormatFlags> enumeration for wrapping as well as vertically and horizontally centering text with the appropriate <xref:System.Windows.Forms.TextRenderer.DrawText%2A> method.
[!code-csharp[System.Drawing.AlignDrawnText#20](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/CS/Form1.cs#20)]
[!code-vb[System.Drawing.AlignDrawnText#20](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/VB/Form1.vb#20)]
## Compiling the Code
The preceding code examples are designed for use with Windows Forms, and they require <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [How to: Draw Text with GDI](how-to-draw-text-with-gdi.md)
- [Using Fonts and Text](using-fonts-and-text.md)
- [How to: Construct Font Families and Fonts](how-to-construct-font-families-and-fonts.md)
@@ -0,0 +1,32 @@
---
title: "How to: Apply Gamma Correction to a Gradient"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "gradient brushes [Windows Forms], gamma correction"
- "gradients [Windows Forms], gamma correction"
ms.assetid: da4690e7-5fac-4fd2-b3f0-5cb35c165b92
---
# How to: Apply Gamma Correction to a Gradient
You can enable gamma correction for a linear gradient brush by setting the brush's <xref:System.Drawing.Drawing2D.LinearGradientBrush.GammaCorrection%2A> property to `true`. You can disable gamma correction by setting the <xref:System.Drawing.Drawing2D.LinearGradientBrush.GammaCorrection%2A> property to `false`. Gamma correction is disabled by default.
## Example
The following example is a method that is called from a control's <xref:System.Windows.Forms.Control.Paint> event handler. The example creates a linear gradient brush and uses that brush to fill two rectangles. The first rectangle is filled without gamma correction, and the second rectangle is filled with gamma correction.
The following illustration shows the two filled rectangles. The top rectangle, which does not have gamma correction, appears dark in the middle. The bottom rectangle, which has gamma correction, appears to have more uniform intensity.
![Two gradient-filled rectangles, with and without gamma correction.](./media/how-to-apply-gamma-correction-to-a-gradient/two-rectangles-gamma-gradient.png)
[!code-csharp[System.Drawing.UsingaGradientBrush#31](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#31)]
[!code-vb[System.Drawing.UsingaGradientBrush#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#31)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- <xref:System.Drawing.Drawing2D.LinearGradientBrush?displayProperty=nameWithType>
- [Using a Gradient Brush to Fill Shapes](using-a-gradient-brush-to-fill-shapes.md)
@@ -0,0 +1,50 @@
---
title: "How to: Arrange MDI Child Forms"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "child forms [Windows Forms], arranging"
- "MDI [Windows Forms], arranging child forms"
ms.assetid: a0786378-3206-4ccc-898e-7d3b38cc5089
---
# How to: Arrange MDI Child Forms
Often, applications will have menu commands for actions such as Tile, Cascade, and Arrange, which control the layout of the open MDI child forms. You can use the <xref:System.Windows.Forms.Form.LayoutMdi%2A> method with one of the <xref:System.Windows.Forms.MdiLayout> enumeration values to rearrange the child forms in an MDI parent form.
The <xref:System.Windows.Forms.MdiLayout> enumeration values display child forms as cascading, as horizontally or vertically tiled, or as child form icons arranged along the lower portion of the MDI form. These values have the same effect as the Windows commands **Cascade windows**, **Show windows side by side**, **Show windows stacked**, and **Show the desktop**, respectively.
Often, these methods are used as the event handlers called by a menu item's <xref:System.Windows.Forms.Control.Click> event. In this way, a menu item with the text "Cascade Windows" can have the desired effect on the MDI child windows.
### To arrange child forms
1. In a method, use the <xref:System.Windows.Forms.Form.LayoutMdi%2A> method to set the <xref:System.Windows.Forms.MdiLayout> enumeration for the MDI parent form. The following example uses the <xref:System.Windows.Forms.MdiLayout.Cascade?displayProperty=nameWithType> enumeration value for the child windows of the MDI parent form (`Form1`). The enumeration is used in code during the event handler for the <xref:System.Windows.Forms.Control.Click> event of the **Cascade Windows** menu item.
```vb
Protected Sub CascadeWindows_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade)
End Sub
```
```csharp
protected void CascadeWindows_Click(object sender, System.EventArgs e){
this.LayoutMdi(System.Windows.Forms.MdiLayout.Cascade);
}
```
> [!NOTE]
> You can also tile windows and arranging windows as icons by changing the <xref:System.Windows.Forms.MdiLayout> enumeration value used.
2. If youre using Visual C#, place the following code in the form's constructor to register the event handler.
```csharp
this.button1.Click += new System.EventHandler(this.button1_Click);
```
## See also
- [Multiple-Document Interface (MDI) Applications](multiple-document-interface-mdi-applications.md)
- [How to: Create MDI Parent Forms](how-to-create-mdi-parent-forms.md)
- [How to: Create MDI Child Forms](how-to-create-mdi-child-forms.md)
- [How to: Determine the Active MDI Child](how-to-determine-the-active-mdi-child.md)
- [How to: Send Data to the Active MDI Child](how-to-send-data-to-the-active-mdi-child.md)
@@ -0,0 +1,55 @@
---
title: "How to: Capture User Input from a PrintDialog at Run Time"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "print options [Windows Forms], changing at run time"
- "printing [Windows Forms], options"
- "print options"
- "run time [Windows Forms], changing print options"
ms.assetid: 438501d8-9a70-4fb3-aae6-e46579aba0c6
---
# How to: Capture User Input from a PrintDialog at Run Time
While you can set options related to printing at design time, you will sometimes want to change these options at run time, most likely because of choices made by the user. You can capture user input for printing a document using the <xref:System.Windows.Forms.PrintDialog> and the <xref:System.Drawing.Printing.PrintDocument> components.
### To change print options programmatically
1. Add a <xref:System.Windows.Forms.PrintDialog> and a <xref:System.Drawing.Printing.PrintDocument> component to your form.
2. Set the <xref:System.Windows.Forms.PrintDialog.Document%2A> property of the <xref:System.Windows.Forms.PrintDialog> to the <xref:System.Drawing.Printing.PrintDocument> added to the form.
```vb
PrintDialog1.Document = PrintDocument1
```
```csharp
printDialog1.Document = PrintDocument1;
```
```cpp
printDialog1->Document = PrintDocument1;
```
3. Display the <xref:System.Windows.Forms.PrintDialog> component by using the <xref:System.Windows.Forms.CommonDialog.ShowDialog%2A> method.
```vb
PrintDialog1.ShowDialog()
```
```csharp
printDialog1.ShowDialog();
```
```cpp
printDialog1->ShowDialog();
```
4. The user's printing choices from the dialog will be copied to the <xref:System.Drawing.Printing.PrinterSettings> property of the <xref:System.Drawing.Printing.PrintDocument> component.
## See also
- [How to: Print a Multi-Page Text File in Windows Forms](how-to-print-a-multi-page-text-file-in-windows-forms.md)
- [Windows Forms Print Support](windows-forms-print-support.md)
@@ -0,0 +1,29 @@
---
title: "How To: Change the Value of a Setting Between Application Sessions"
ms.date: "03/30/2017"
helpviewer_keywords:
- "application settings [Windows Forms], changing"
- "application settings [Windows Forms], between application sessions"
ms.assetid: 1a85911f-97b2-476c-930b-83379edd890c
---
# How To: Change the Value of a Setting Between Application Sessions
At times, you might want to change the value of a setting between application sessions after the application has been compiled and deployed. For example, you might want to change a connection string to point to the correct database location. Since design-time tools are not available after the application has been compiled and deployed, you must change the setting value manually in the file.
### To Change the Value of a Setting Between Application Sessions
1. Using Microsoft Notepad or some other text or XML editor, open the .config file associated with your application.
2. Locate the entry for the setting you want to change. It should look similar to the example presented below.
```xml
<setting name="Setting1" serializeAs="String" >
<value>My Setting Value</value>
</setting>
```
3. Type a new value for your setting and save the file.
## See also
- [Using Application Settings and User Settings](using-application-settings-and-user-settings.md)
- [Application Settings Overview](application-settings-overview.md)
@@ -0,0 +1,32 @@
---
title: "How To: Change the Value of an Existing Setting at Design Time"
ms.date: "03/30/2017"
helpviewer_keywords:
- "application settings [Windows Forms], design time"
- "application settings [Windows Forms], changing"
ms.assetid: 5da91272-ad7e-49e7-9d1f-eb64439a1e4d
---
# How To: Change the Value of an Existing Setting at Design Time
You can use Visual Studio to edit the values of existing settings in your project.
### To Change the Value of an Existing Setting at Design Time in C\#
1. In **Solution Explorer**, expand the **Properties** node of your project.
2. Double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
3. In the Settings designer, find the setting for which you want to change the value and type the new value in the Value column.
### To Change the Value of an Existing Setting at Design Time in Visual Basic
1. In **Solution Explorer**, right-click your project node and choose **Properties**.
2. In the **Properties** page, select the **Settings** tab.
3. In the Settings designer, find the setting for which you want to change the value and type the new value in the Value column.
## See also
- [Using Application Settings and User Settings](using-application-settings-and-user-settings.md)
- [How To: Create a New Setting at Design Time](how-to-create-a-new-setting-at-design-time.md)
- [Application Settings Overview](application-settings-overview.md)
@@ -0,0 +1,105 @@
---
title: "How to: Choose the Printers Attached to a User's Computer"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "printing [Windows Forms], choosing printers"
- "printers [Windows Forms], choosing"
ms.assetid: 63c1172b-2931-4ac0-953f-37f629494bbf
---
# How to: Choose the Printers Attached to a User's Computer in Windows Forms
Often, users want to choose a printer other than the default printer to print to. You can enable users to choose a printer from among those currently installed by using the <xref:System.Windows.Forms.PrintDialog> component. Through the <xref:System.Windows.Forms.PrintDialog> component, the <xref:System.Windows.Forms.DialogResult> of the <xref:System.Windows.Forms.PrintDialog> component is captured and used to select the printer.
In the following procedure, a text file is selected to be printed to the default printer. The <xref:System.Windows.Forms.PrintDialog> class is then instantiated.
### To choose a printer and then print a file
1. Select the printer to be used using the <xref:System.Windows.Forms.PrintDialog> component.
In the following code example, there are two events being handled. In the first, a <xref:System.Windows.Forms.Button> control's <xref:System.Windows.Forms.Control.Click> event, the <xref:System.Windows.Forms.PrintDialog> class is instantiated and the printer selected by the user is captured in the <xref:System.Windows.Forms.DialogResult> property.
In the second event, the <xref:System.Drawing.Printing.PrintDocument.PrintPage> event of the <xref:System.Drawing.Printing.PrintDocument> component, a sample document is printed to the printer specified.
```vb
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim PrintDialog1 As New PrintDialog()
PrintDialog1.Document = PrintDocument1
Dim result As DialogResult = PrintDialog1.ShowDialog()
If (result = DialogResult.OK) Then
PrintDocument1.Print()
End If
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500))
End Sub
```
```csharp
private void button1_Click(object sender, System.EventArgs e)
{
PrintDialog printDialog1 = new PrintDialog();
printDialog1.Document = printDocument1;
DialogResult result = printDialog1.ShowDialog();
if (result == DialogResult.OK)
{
printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red,
new Rectangle(500, 500, 500, 500));
}
```
```cpp
private:
void button1_Click(System::Object ^ sender,
System::EventArgs ^ e)
{
PrintDialog ^ printDialog1 = gcnew PrintDialog();
printDialog1->Document = printDocument1;
System::Windows::Forms::DialogResult result =
printDialog1->ShowDialog();
if (result == DialogResult::OK)
{
printDocument1->Print();
}
}
private:
void printDocument1_PrintPage(System::Object ^ sender,
System::Drawing::Printing::PrintPageEventArgs ^ e)
{
e->Graphics->FillRectangle(Brushes::Red,
Rectangle(500, 500, 500, 500));
}
```
(Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.
```csharp
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler
(this.printDocument1_PrintPage);
this.button1.Click += new System.EventHandler(this.button1_Click);
```
```cpp
this->printDocument1->PrintPage += gcnew
System::Drawing::Printing::PrintPageEventHandler
(this, &Form1::printDocument1_PrintPage);
this->button1->Click += gcnew
System::EventHandler(this, &Form1::button1_Click);
```
## See also
- [Windows Forms Print Support](windows-forms-print-support.md)
@@ -0,0 +1,80 @@
---
title: Complete Print Jobs
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "print jobs [Windows Forms], completing in Windows Forms"
- "printing [Windows Forms], print jobs"
ms.assetid: 23ec74f7-34c5-4710-82a0-ee2914518548
---
# How to: Complete Windows Forms Print Jobs
Frequently, word processors and other applications that involve printing will provide the option to display a message to users that a print job is complete. You can provide this functionality in your Windows Forms by handling the <xref:System.Drawing.Printing.PrintDocument.EndPrint> event of the <xref:System.Drawing.Printing.PrintDocument> component.
The following procedure requires that you have created a Windows-based application with a <xref:System.Drawing.Printing.PrintDocument> component on it, which is the standard way of enabling printing from a Windows-based application. For more information about printing from Windows Forms using the <xref:System.Drawing.Printing.PrintDocument> component, see [How to: Create Standard Windows Forms Print Jobs](how-to-create-standard-windows-forms-print-jobs.md).
### To complete a print job
1. Set the <xref:System.Drawing.Printing.PrintDocument.DocumentName%2A> property of the <xref:System.Drawing.Printing.PrintDocument> component.
```vb
PrintDocument1.DocumentName = "MyTextFile"
```
```csharp
printDocument1.DocumentName = "MyTextFile";
```
```cpp
printDocument1->DocumentName = "MyTextFile";
```
2. Write code to handle the <xref:System.Drawing.Printing.PrintDocument.EndPrint> event.
In the following code example, a message box is displayed, indicating that the document has finished printing.
```vb
Private Sub PrintDocument1_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PrintDocument1.EndPrint
MessageBox.Show(PrintDocument1.DocumentName + " has finished printing.")
End Sub
```
```csharp
private void printDocument1_EndPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
MessageBox.Show(printDocument1.DocumentName +
" has finished printing.");
}
```
```cpp
private:
void printDocument1_EndPrint(System::Object ^ sender,
System::Drawing::Printing::PrintEventArgs ^ e)
{
MessageBox::Show(String::Concat(printDocument1->DocumentName,
" has finished printing."));
}
```
(Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.
```csharp
this.printDocument1.EndPrint += new
System.Drawing.Printing.PrintEventHandler
(this.printDocument1_EndPrint);
```
```cpp
this->printDocument1->EndPrint += gcnew
System::Drawing::Printing::PrintEventHandler
(this, &Form1::printDocument1_EndPrint);
```
## See also
- <xref:System.Drawing.Printing.PrintDocument>
- [Windows Forms Print Support](windows-forms-print-support.md)
@@ -0,0 +1,49 @@
---
title: "How to: Construct Font Families and Fonts"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "font families [Windows Forms], constructing"
- "fonts [Windows Forms], constructing"
ms.assetid: d3a4a223-9492-4b54-9afd-db1c31c3cefd
---
# How to: Construct Font Families and Fonts
GDI+ groups fonts with the same typeface but different styles into font families. For example, the Arial font family contains the following fonts:
- Arial Regular
- Arial Bold
- Arial Italic
- Arial Bold Italic
GDI+ uses four styles to form families: regular, bold, italic, and bold italic. Adjectives such as *narrow* and *rounded* are not considered styles; rather they are part of the family name. For example, Arial Narrow is a font family with the following members:
- Arial Narrow Regular
- Arial Narrow Bold
- Arial Narrow Italic
- Arial Narrow Bold Italic
Before you can draw text with GDI+, you need to construct a <xref:System.Drawing.FontFamily> object and a <xref:System.Drawing.Font> object. The <xref:System.Drawing.FontFamily> object specifies the typeface (for example, Arial), and the <xref:System.Drawing.Font> object specifies the size, style, and units.
## Example
The following example constructs a regular style Arial font with a size of 16 pixels. In the following code, the first argument passed to the <xref:System.Drawing.Font.%23ctor%2A> constructor is the <xref:System.Drawing.FontFamily> object. The second argument specifies the size of the font measured in units identified by the fourth argument. The third argument identifies the style.
<xref:System.Drawing.GraphicsUnit.Pixel> is a member of the <xref:System.Drawing.GraphicsUnit> enumeration, and <xref:System.Drawing.FontStyle.Regular> is a member of the <xref:System.Drawing.FontStyle> enumeration.
[!code-csharp[System.Drawing.FontsAndText#61](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs#61)]
[!code-vb[System.Drawing.FontsAndText#61](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.FontsAndText/VB/Class1.vb#61)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [Using Fonts and Text](using-fonts-and-text.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
@@ -0,0 +1,32 @@
---
title: "How to: Convert a BMP image to a PNG image"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "BMP images [Windows Forms], converting to PNG"
- "image formats [Windows Forms], converting between"
ms.assetid: 9d4a692d-73ac-4ce3-9e05-9ec321e8fbd6
---
# How to: Convert a BMP image to a PNG image
Oftentimes, you will want to convert from one image file format to another. You can do this conversion easily by calling the <xref:System.Drawing.Image.Save%2A> method of the <xref:System.Drawing.Image> class and specifying the <xref:System.Drawing.Imaging.ImageFormat> for the desired image file format.
## Example
The following example loads a BMP image from a type, and saves the image in the PNG format.
[!code-csharp[UsingImageEncodersDecoders#4](~/samples/snippets/csharp/VS_Snippets_Winforms/UsingImageEncodersDecoders/CS/Form1.cs#4)]
[!code-vb[UsingImageEncodersDecoders#4](~/samples/snippets/visualbasic/VS_Snippets_Winforms/UsingImageEncodersDecoders/VB/Form1.vb#4)]
## Compiling the Code
This example requires:
- A Windows Forms application.
- A reference to the `System.Drawing.Imaging` namespace.
## See also
- [How to: List Installed Encoders](how-to-list-installed-encoders.md)
- [Using Image Encoders and Decoders in Managed GDI+](using-image-encoders-and-decoders-in-managed-gdi.md)
- [Types of Bitmaps](types-of-bitmaps.md)
@@ -0,0 +1,44 @@
---
title: "How to: Copy and Paste an ElementHost Control at Design Time"
ms.date: "03/30/2017"
helpviewer_keywords:
- "Windows Forms, content copying and pasting"
- "interoperability [WPF]"
- "ElementHost control [Windows Forms], copying and pasting at design time"
- "WPF user control [Windows Forms], hosting in Windows Forms"
ms.assetid: e570375d-2a68-44ba-b4f7-c781af2d20e8
author: jillre
ms.author: jillfra
manager: jillfra
---
# How to: Copy and paste an ElementHost control
This procedure shows you how to copy a Windows Presentation Foundation (WPF) control on a Windows Form in Visual Studio.
1. In Visual Studio, add a new WPF <xref:System.Windows.Controls.UserControl> to a Windows Forms project. Use the default name for the control type, `UserControl1.xaml`. For more information, see [Walkthrough: Creating New WPF Content on Windows Forms at Design Time](walkthrough-creating-new-wpf-content-on-windows-forms-at-design-time.md).
2. In the **Properties** window, set the value of the <xref:System.Windows.FrameworkElement.Width%2A> and <xref:System.Windows.FrameworkElement.Height%2A> properties of `UserControl1` to **200**.
3. Set the value of the <xref:System.Windows.Controls.Control.Background%2A> property to **Blue**.
4. Build the project.
5. Open `Form1` in the Windows Forms Designer.
6. From the **Toolbox**, drag an instance of `UserControl1` onto the form.
An instance of `UserControl1` is hosted in a new <xref:System.Windows.Forms.Integration.ElementHost> control named `elementHost1`.
7. With `elementHost1` selected, press **Ctrl**+**C** to copy it to the clipboard.
8. Press **Ctrl**+**V** to paste the copied control onto the form.
A new <xref:System.Windows.Forms.Integration.ElementHost> control named `elementHost2` is created on the form.
## See also
- <xref:System.Windows.Forms.Integration.ElementHost>
- <xref:System.Windows.Forms.Integration.WindowsFormsHost>
- [Migration and Interoperability](https://docs.microsoft.com/dotnet/framework/wpf/advanced/migration-and-interoperability)
- [Using WPF Controls](using-wpf-controls.md)
- [Design XAML in Visual Studio](/visualstudio/xaml-tools/designing-xaml-in-visual-studio)
@@ -0,0 +1,65 @@
---
title: "How to: Copy Pixels for Reducing Flicker"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "bitblt"
- "graphics [Windows Forms], copying"
- "flicker [Windows Forms], reducing in Windows Forms"
- "graphics [Windows Forms], reducing flicker"
- "pixels [Windows Forms], copying"
- "flicker"
- "bit-block transfer"
ms.assetid: 33b76910-13a3-4521-be98-5c097341ae3b
---
# How to: Copy Pixels for Reducing Flicker in Windows Forms
When you animate a simple graphic, users can sometimes encounter flicker or other undesirable visual effects. One way to limit this problem is to use a "bitblt" process on the graphic. Bitblt is the "bit-block transfer" of the color data from an origin rectangle of pixels to a destination rectangle of pixels.
With Windows Forms, bitblt is accomplished using the <xref:System.Drawing.Graphics.CopyFromScreen%2A> method of the <xref:System.Drawing.Graphics> class. In the parameters of the method, you specify the source and destination (as points), the size of the area to be copied, and the graphics object used to draw the new shape.
In the example below, a shape is drawn on the form in its <xref:System.Windows.Forms.Control.Paint> event handler. Then, the <xref:System.Drawing.Graphics.CopyFromScreen%2A> method is used to duplicate the shape.
> [!NOTE]
> Setting the form's <xref:System.Windows.Forms.Control.DoubleBuffered%2A> property to `true` will make graphics-based code in the <xref:System.Windows.Forms.Control.Paint> event be double-buffered. While this will not have any discernible performance gains when using the code below, it is something to keep in mind when working with more complex graphics-manipulation code.
## Example
```vb
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Draw a circle with a bar on top.
e.Graphics.FillEllipse(Brushes.DarkBlue, New Rectangle _
(10, 10, 60, 60))
e.Graphics.FillRectangle(Brushes.Khaki, New Rectangle _
(20, 30, 60, 10))
' Copy the graphic to a new location.
e.Graphics.CopyFromScreen(New Point(10, 10), New Point _
(100, 100), New Size(70, 70))
End Sub
```
```csharp
private void Form1_Paint(System.Object sender,
System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.DarkBlue, new
Rectangle(10,10,60,60));
e.Graphics.FillRectangle(Brushes.Khaki, new
Rectangle(20,30,60,10));
e.Graphics.CopyFromScreen(new Point(10, 10), new Point(100, 100),
new Size(70, 70));
}
```
## Compiling the Code
The code above is run in the form's <xref:System.Windows.Forms.Control.Paint> event handler so that the graphics persist when the form is redrawn. As such, do not call graphics-related methods in the <xref:System.Windows.Forms.Form.Load> event handler, because the drawn content will not be redrawn if the form is resized or obscured by another form.
## See also
- <xref:System.Drawing.CopyPixelOperation>
- <xref:System.Drawing.Graphics.FillRectangle%2A?displayProperty=nameWithType>
- <xref:System.Windows.Forms.Control.OnPaint%2A?displayProperty=nameWithType>
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
@@ -0,0 +1,28 @@
---
title: "How to: Create a Bitmap at Run Time"
description: Learn how to create a bitmap object and display it in an existing Windows Forms PictureBox control.
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "bitmaps [Windows Forms], creating"
- "bitmaps [Windows Forms], examples [Visual Basic]"
ms.assetid: 737bae30-e599-4e1d-bf30-bab8280b32be
---
# How to: Create a Bitmap at Run Time
This example creates and draws in a <xref:System.Drawing.Bitmap> object and displays it in an existing Windows Forms <xref:System.Windows.Forms.PictureBox> control.
## Example
[!code-csharp[System.Drawing.CreateBitmapAtRuntime#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.CreateBitmapAtRuntime/CS/Form1.cs#1)]
[!code-vb[System.Drawing.CreateBitmapAtRuntime#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.CreateBitmapAtRuntime/VB/Form1.vb#1)]
## Compiling the Code
This example requires:
- A Windows Form that imports the System, System.Drawing and System.Windows.Forms assemblies.
## See also
- <xref:System.Drawing.Bitmap>
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
@@ -0,0 +1,80 @@
---
title: "How to: Create a Linear Gradient"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "linear gradients [Windows Forms], creating"
- "gradients [Windows Forms], creating linear"
- "colors [Windows Forms], creating linear gradients"
- "gradients"
ms.assetid: 6c88e1cc-1217-4399-ac12-cb37592b9f01
---
# How to: Create a Linear Gradient
GDI+ provides horizontal, vertical, and diagonal linear gradients. By default, the color in a linear gradient changes uniformly. However, you can customize a linear gradient so that the color changes in a non-uniform fashion.
> [!NOTE]
> The examples in this article are methods that are called from a control's <xref:System.Windows.Forms.Control.Paint> event handler.
The following example fills a line, an ellipse, and a rectangle with a horizontal linear gradient brush.
The <xref:System.Drawing.Drawing2D.LinearGradientBrush.%23ctor%2A> constructor receives four arguments: two points and two colors. The first point (0, 10) is associated with the first color (red), and the second point (200, 10) is associated with the second color (blue). As you would expect, the line drawn from (0, 10) to (200, 10) changes gradually from red to blue.
The 10s in the points (0, 10) and (200, 10) are not important. What is important is that the two points have the same second coordinate — the line connecting them is horizontal. The ellipse and the rectangle also change gradually from red to blue as the horizontal coordinate goes from 0 to 200.
The following illustration shows the line, the ellipse, and the rectangle. Note that the color gradient repeats itself as the horizontal coordinate increases beyond 200.
![A line, an ellipse, and a rectangle filled with a color gradient.](./media/how-to-create-a-linear-gradient/gradient-line-ellipse-rectangle.png)
## To use horizontal linear gradients
- Pass in the opaque red and opaque blue as the third and fourth argument, respectively.
[!code-csharp[System.Drawing.UsingaGradientBrush#21](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#21)]
[!code-vb[System.Drawing.UsingaGradientBrush#21](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#21)]
In the preceding example, the color components change linearly as you move from a horizontal coordinate of 0 to a horizontal coordinate of 200. For example, a point whose first coordinate is halfway between 0 and 200 will have a blue component that is halfway between 0 and 255.
GDI+ allows you to adjust the way a color varies from one edge of a gradient to the other. Suppose you want to create a gradient brush that changes from black to red according to the following table.
|Horizontal coordinate|RGB components|
|---------------------------|--------------------|
|0|(0, 0, 0)|
|40|(128, 0, 0)|
|200|(255, 0, 0)|
Note that the red component is at half intensity when the horizontal coordinate is only 20 percent of the way from 0 to 200.
The following example sets the <xref:System.Drawing.Drawing2D.LinearGradientBrush.Blend%2A?displayProperty=nameWithType> property to associate three relative intensities with three relative positions. As in the preceding table, a relative intensity of 0.5 is associated with a relative position of 0.2. The code fills an ellipse and a rectangle with the gradient brush.
The following illustration shows the resulting ellipse and rectangle.
![An ellipse and a rectangle filled with a horizontal color gradient.](./media/how-to-create-a-linear-gradient/gradient-ellipse-rectangle.png)
## To customize linear gradients
- Pass in the opaque black and opaque red as the third and fourth argument, respectively.
[!code-csharp[System.Drawing.UsingaGradientBrush#22](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#22)]
[!code-vb[System.Drawing.UsingaGradientBrush#22](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#22)]
The gradients in the preceding examples have been horizontal; that is, the color changes gradually as you move along any horizontal line. You can also define vertical gradients and diagonal gradients.
The following example passes the points (0, 0) and (200, 100) to a <xref:System.Drawing.Drawing2D.LinearGradientBrush.%23ctor%2A> constructor. The color blue is associated with (0, 0), and the color green is associated with (200, 100). A line (with pen width 10) and an ellipse are filled with the linear gradient brush.
The following illustration shows the line and the ellipse. Note that the color in the ellipse changes gradually as you move along any line that is parallel to the line passing through (0, 0) and (200, 100).
![A line and an ellipse filled with a diagonal color gradient.](./media/how-to-create-a-linear-gradient/gradient-line-ellipse.png)
## To create diagonal linear gradients
- Pass in the opaque blue and opaque green as the third and fourth argument, respectively.
[!code-csharp[System.Drawing.UsingaGradientBrush#23](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#23)]
[!code-vb[System.Drawing.UsingaGradientBrush#23](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#23)]
## See also
- [Using a Gradient Brush to Fill Shapes](using-a-gradient-brush-to-fill-shapes.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
@@ -0,0 +1,38 @@
---
title: "How To: Create a New Setting at Design Time"
description: Learn how to create a new Windows Forms setting at design time by using the Settings designer in Visual Studio.
ms.date: "03/30/2017"
helpviewer_keywords:
- "application settings [Windows Forms], design time"
- "application settings [Windows Forms], creating"
ms.assetid: c5d60a66-6507-462f-a81f-e3bc0a804e16
---
# How To: Create a new setting at design time
You can create a new setting at design time by using the Settings designer in Visual Studio. The Settings designer is a grid-style interface that allows you to create new settings and specify properties for those settings. You must specify Name, Value, Type and Scope for your new settings. Once a setting is created, it is accessible in code.
## Create a new setting at design time in C\#
1. Open Visual Studio.
2. In **Solution Explorer**, expand the **Properties** node of your project.
3. Double-click the .settings file in which you want to add a new setting. The default name for this file is Settings.settings.
4. In the Settings designer, set the **Name**, **Value**, **Type**, and **Scope** for your setting. Each row represents a single setting.
## Create a new setting at design time in Visual Basic
1. Open Visual Studio.
2. In **Solution Explorer**, right-click your project node and choose **Properties**.
3. In the **Properties** page, select the **Settings** tab.
4. In the Settings designer, set the **Name**, **Value**, **Type**, and **Scope** for your setting. Each row represents a single setting.
## See also
- [Using Application Settings and User Settings](using-application-settings-and-user-settings.md)
- [Application Settings Overview](application-settings-overview.md)
- [How To: Change the Value of an Existing Setting at Design Time](how-to-change-the-value-of-an-existing-setting-at-design-time.md)
@@ -0,0 +1,111 @@
---
title: "How to: Create a Path Gradient"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "path gradients [Windows Forms], creating"
- "gradients [Windows Forms], creating path"
- "graphics paths [Windows Forms], creating gradient"
ms.assetid: 1948e834-e104-481c-b71d-d8aa9e4d106e
---
# How to: Create a Path Gradient
The <xref:System.Drawing.Drawing2D.PathGradientBrush> class allows you to customize the way you fill a shape with gradually changing colors. For example, you can specify one color for the center of a path and another color for the boundary of a path. You can also specify separate colors for each of several points along the boundary of a path.
> [!NOTE]
> In GDI+, a path is a sequence of lines and curves maintained by a <xref:System.Drawing.Drawing2D.GraphicsPath> object. For more information about GDI+ paths, see [Graphics Paths in GDI+](graphics-paths-in-gdi.md) and [Constructing and Drawing Paths](constructing-and-drawing-paths.md).
The examples in this article are methods that are called from a control's <xref:System.Windows.Forms.Control.Paint> event handler.
### To fill an ellipse with a path gradient
- The following example fills an ellipse with a path gradient brush. The center color is set to blue and the boundary color is set to aqua. The following illustration shows the filled ellipse.
![Gradient Path fills an ellipse.](./media/how-to-create-a-path-gradient/gradient-path-filled-ellipse.png)
By default, a path gradient brush does not extend outside the boundary of the path. If you use the path gradient brush to fill a figure that extends beyond the boundary of the path, the area of the screen outside the path will not be filled.
The following illustration shows what happens if you change the <xref:System.Drawing.Graphics.FillEllipse%2A?displayProperty=nameWithType> call in the following code to `e.Graphics.FillRectangle(pthGrBrush, 0, 10, 200, 40)`:
![Gradient Path extended beyond boundary of the path.](./media/how-to-create-a-path-gradient/gradient-path-extended-beyond-boundary.png)
[!code-csharp[System.Drawing.UsingaGradientBrush#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#11)]
[!code-vb[System.Drawing.UsingaGradientBrush#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#11)]
The preceding code example is designed for use with Windows Forms, and it requires the <xref:System.Windows.Forms.PaintEventArgs> e, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
### To specify points on the boundary
- The following example constructs a path gradient brush from a star-shaped path. The code sets the <xref:System.Drawing.Drawing2D.PathGradientBrush.CenterColor%2A> property, which sets the color at the centroid of the star to red. Then the code sets the <xref:System.Drawing.Drawing2D.PathGradientBrush.SurroundColors%2A> property to specify various colors (stored in the `colors` array) at the individual points in the `points` array. The final code statement fills the star-shaped path with the path gradient brush.
[!code-csharp[System.Drawing.UsingaGradientBrush#12](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#12)]
[!code-vb[System.Drawing.UsingaGradientBrush#12](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#12)]
- The following example draws a path gradient without a <xref:System.Drawing.Drawing2D.GraphicsPath> object in the code. The particular <xref:System.Drawing.Drawing2D.PathGradientBrush.%23ctor%2A> constructor in the example receives an array of points but does not require a <xref:System.Drawing.Drawing2D.GraphicsPath> object. Also, note that the <xref:System.Drawing.Drawing2D.PathGradientBrush> is used to fill a rectangle, not a path. The rectangle is larger than the closed path used to define the brush, so some of the rectangle is not painted by the brush. The following illustration shows the rectangle (dotted line) and the portion of the rectangle painted by the path gradient brush:
![Gradient portion painted by the path gradient brush.](./media/how-to-create-a-path-gradient/gradient-painted-path-gradient-brush.png)
[!code-csharp[System.Drawing.UsingaGradientBrush#13](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#13)]
[!code-vb[System.Drawing.UsingaGradientBrush#13](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#13)]
### To customize a path gradient
- One way to customize a path gradient brush is to set its <xref:System.Drawing.Drawing2D.PathGradientBrush.FocusScales%2A> property. The focus scales specify an inner path that lies inside the main path. The center color is displayed everywhere inside that inner path rather than only at the center point.
The following example creates a path gradient brush based on an elliptical path. The code sets the boundary color to blue, sets the center color to aqua, and then uses the path gradient brush to fill the elliptical path.
Next, the code sets the focus scales of the path gradient brush. The x focus scale is set to 0.3, and the y focus scale is set to 0.8. The code calls the <xref:System.Drawing.Graphics.TranslateTransform%2A> method of a <xref:System.Drawing.Graphics> object so that the subsequent call to <xref:System.Drawing.Graphics.FillPath%2A> fills an ellipse that sits to the right of the first ellipse.
To see the effect of the focus scales, imagine a small ellipse that shares its center with the main ellipse. The small (inner) ellipse is the main ellipse scaled (about its center) horizontally by a factor of 0.3 and vertically by a factor of 0.8. As you move from the boundary of the outer ellipse to the boundary of the inner ellipse, the color changes gradually from blue to aqua. As you move from the boundary of the inner ellipse to the shared center, the color remains aqua.
The following illustration shows the output of the following code. The ellipse on the left is aqua only at the center point. The ellipse on the right is aqua everywhere inside the inner path.
![Gradient effect of focus scales](./media/how-to-create-a-path-gradient/focus-scales-aqua-inner-outer-ellipse.png)
[!code-csharp[System.Drawing.UsingaGradientBrush#14](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#14)]
[!code-vb[System.Drawing.UsingaGradientBrush#14](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#14)]
### To customize with interpolation
- Another way to customize a path gradient brush is to specify an array of interpolation colors and an array of interpolation positions.
The following example creates a path gradient brush based on a triangle. The code sets the <xref:System.Drawing.Drawing2D.PathGradientBrush.InterpolationColors%2A> property of the path gradient brush to specify an array of interpolation colors (dark green, aqua, blue) and an array of interpolation positions (0, 0.25, 1). As you move from the boundary of the triangle to the center point, the color changes gradually from dark green to aqua and then from aqua to blue. The change from dark green to aqua happens in 25 percent of the distance from dark green to blue.
The following illustration shows the triangle filled with the custom path gradient brush.
![Triangle filled with custom path gradient brush.](./media/how-to-create-a-path-gradient/gradient-brush-filled-triangle.png)
[!code-csharp[System.Drawing.UsingaGradientBrush#15](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#15)]
[!code-vb[System.Drawing.UsingaGradientBrush#15](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#15)]
### To set the center point
- By default, the center point of a path gradient brush is at the centroid of the path used to construct the brush. You can change the location of the center point by setting the <xref:System.Drawing.Drawing2D.PathGradientBrush.CenterPoint%2A> property of the <xref:System.Drawing.Drawing2D.PathGradientBrush> class.
The following example creates a path gradient brush based on an ellipse. The center of the ellipse is at (70, 35), but the center point of the path gradient brush is set to (120, 40).
[!code-csharp[System.Drawing.UsingaGradientBrush#16](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#16)]
[!code-vb[System.Drawing.UsingaGradientBrush#16](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#16)]
The following illustration shows the filled ellipse and the center point of the path gradient brush:
![Gradient Path with filled ellipse and center point.](./media/how-to-create-a-path-gradient/gradient-path-filled-ellipse-center-point.png)
- You can set the center point of a path gradient brush to a location outside the path that was used to construct the brush. The following example replaces the call to set the <xref:System.Drawing.Drawing2D.PathGradientBrush.CenterPoint%2A> property in the preceding code.
[!code-csharp[System.Drawing.UsingaGradientBrush#17](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/CS/Class1.cs#17)]
[!code-vb[System.Drawing.UsingaGradientBrush#17](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingaGradientBrush/VB/Class1.vb#17)]
The following illustration shows the output with this change:
![Gradient Path with center point outside the path.](./media/how-to-create-a-path-gradient/gradient-path-center-point-outside.png)
In the preceding illustration, the points at the far right of the ellipse are not pure blue (although they are very close). The colors in the gradient are positioned as if the fill reached the point (145, 35) where the color would be pure blue (0, 0, 255). But the fill never reaches (145, 35) because a path gradient brush paints only inside its path.
## Compiling the Code
The preceding examples are designed for use with Windows Forms, and they require <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Using a Gradient Brush to Fill Shapes](using-a-gradient-brush-to-fill-shapes.md)
@@ -0,0 +1,29 @@
---
title: "How to: Create a Pen"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "graphics [Windows Forms], creating pens"
- "pens [Windows Forms], creating"
- "Pen object"
ms.assetid: 7fbea8b7-7ac1-4413-9c17-733a850381e3
---
# How to: Create a Pen
This example creates a <xref:System.Drawing.Pen> object.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#3](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#3)]
[!code-csharp[System.Drawing.ConceptualHowTos#3](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#3)]
[!code-vb[System.Drawing.ConceptualHowTos#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#3)]
## Robust Programming
After you have finished using objects that consume system resources, such as <xref:System.Drawing.Pen> objects, you should call <xref:System.Drawing.Pen.Dispose%2A> on them.
## See also
- <xref:System.Drawing.Pen>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Pens, Lines, and Rectangles in GDI+](pens-lines-and-rectangles-in-gdi.md)
@@ -0,0 +1,51 @@
---
title: "How to: Create a Private Font Collection"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "private font collections [Windows Forms], creating"
- "fonts [Windows Forms], creating private collections"
ms.assetid: 6533d5e5-a8dc-4b76-9fc4-3bf75c8b9212
---
# How to: Create a Private Font Collection
The <xref:System.Drawing.Text.PrivateFontCollection> class inherits from the <xref:System.Drawing.Text.FontCollection> abstract base class. You can use a <xref:System.Drawing.Text.PrivateFontCollection> object to maintain a set of fonts specifically for your application. A private font collection can include installed system fonts as well as fonts that have not been installed on the computer. To add a font file to a private font collection, call the <xref:System.Drawing.Text.PrivateFontCollection.AddFontFile%2A> method of a <xref:System.Drawing.Text.PrivateFontCollection> object.
The <xref:System.Drawing.Text.FontCollection.Families%2A> property of a <xref:System.Drawing.Text.PrivateFontCollection> object contains an array of <xref:System.Drawing.FontFamily> objects.
The number of font families in a private font collection is not necessarily the same as the number of font files that have been added to the collection. For example, suppose you add the files ArialBd.tff, Times.tff, and TimesBd.tff to a collection. There will be three files but only two families in the collection because Times.tff and TimesBd.tff belong to the same family.
## Example
The following example adds the following three font files to a <xref:System.Drawing.Text.PrivateFontCollection> object:
- C:\\*systemroot*\Fonts\Arial.tff (Arial, regular)
- C:\\*systemroot*\Fonts\CourBI.tff (Courier New, bold italic)
- C:\\*systemroot*\Fonts\TimesBd.tff (Times New Roman, bold)
The code retrieves an array of <xref:System.Drawing.FontFamily> objects from the <xref:System.Drawing.Text.FontCollection.Families%2A> property of the <xref:System.Drawing.Text.PrivateFontCollection> object.
For each <xref:System.Drawing.FontFamily> object in the collection, the code calls the <xref:System.Drawing.FontFamily.IsStyleAvailable%2A> method to determine whether various styles (regular, bold, italic, bold italic, underline, and strikeout) are available. The arguments passed to the <xref:System.Drawing.FontFamily.IsStyleAvailable%2A> method are members of the <xref:System.Drawing.FontStyle> enumeration.
If a given family/style combination is available, a <xref:System.Drawing.Font> object is constructed using that family and style. The first argument passed to the <xref:System.Drawing.Font.%23ctor%2A> constructor is the font family name (not a <xref:System.Drawing.FontFamily> object as is the case for other variations of the <xref:System.Drawing.Font.%23ctor%2A> constructor). After the <xref:System.Drawing.Font> object is constructed, it is passed to the <xref:System.Drawing.Graphics.DrawString%2A> method of the <xref:System.Drawing.Graphics> class to display the family name along with the name of the style.
The output of the following code is similar to the output shown in the following illustration:
![Screenshot that shows text in various fonts.](./media/how-to-create-a-private-font-collection/various-fonts-text-output.png)
Arial.tff (which was added to the private font collection in the following code example) is the font file for the Arial regular style. Note, however, that the program output shows several available styles other than regular for the Arial font family. That is because GDI+ can simulate the bold, italic, and bold italic styles from the regular style. GDI+ can also produce underlines and strikeouts from the regular style.
Similarly, GDI+ can simulate the bold italic style from either the bold style or the italic style. The program output shows that the bold italic style is available for the Times family even though TimesBd.tff (Times New Roman, bold) is the only Times file in the collection.
[!code-csharp[System.Drawing.FontsAndText#51](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs#51)]
[!code-vb[System.Drawing.FontsAndText#51](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.FontsAndText/VB/Class1.vb#51)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- <xref:System.Drawing.Text.PrivateFontCollection>
- [Using Fonts and Text](using-fonts-and-text.md)
@@ -0,0 +1,44 @@
---
title: "How to: Create a Shaped Windows Form"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "forms [Windows Forms], rounded"
- "Windows Forms, custom shapes"
- "Windows Forms, shaped"
- "shaped forms"
- "forms [Windows Forms], changing the shape of"
- "forms [Windows Forms], circular"
- "forms [Windows Forms], nonrectangular"
- "Windows Forms, nonrectangular shape"
- "Windows Forms, rounded"
- "Windows Forms, circular"
- "forms [Windows Forms], custom shapes"
ms.assetid: 6e6041e0-8e67-4487-b1e9-e410dbd1ef6c
---
# How to: Create a Shaped Windows Form
This example gives a form an elliptical shape that resizes with the form.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#10](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#10)]
[!code-csharp[System.Drawing.ConceptualHowTos#10](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#10)]
[!code-vb[System.Drawing.ConceptualHowTos#10](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#10)]
## Compiling the Code
This example requires:
- References to the <xref:System.Windows.Forms> and <xref:System.Drawing> namespaces.
This example overrides the <xref:System.Windows.Forms.Control.OnPaint%2A> method to change the shape of the form. To use this code, copy the method declaration as well as the drawing code inside the method.
## See also
- <xref:System.Windows.Forms.Control.OnPaint%2A>
- <xref:System.Drawing.Region>
- <xref:System.Drawing>
- <xref:System.Drawing.Drawing2D.GraphicsPath.AddEllipse%2A>
- <xref:System.Windows.Forms.Control.Region%2A>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
@@ -0,0 +1,31 @@
---
title: "How to: Create a Solid Brush"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "solid color brushes"
- "brushes [Windows Forms], examples"
- "brushes [Windows Forms], creating solid"
ms.assetid: 85c3fe7d-fb1d-4591-8a9f-d75b556b90af
---
# How to: Create a Solid Brush
This example creates a <xref:System.Drawing.SolidBrush> object that can be used by a <xref:System.Drawing.Graphics> object for filling shapes.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#1](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#1)]
[!code-csharp[System.Drawing.ConceptualHowTos#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#1)]
[!code-vb[System.Drawing.ConceptualHowTos#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#1)]
## Robust Programming
After you have finished using them, you should call <xref:System.IDisposable.Dispose%2A> on objects that consume system resources, such as brush objects.
## See also
- <xref:System.Drawing.SolidBrush>
- <xref:System.Drawing.Brush>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Brushes and Filled Shapes in GDI+](brushes-and-filled-shapes-in-gdi.md)
- [Using a Brush to Fill Shapes](using-a-brush-to-fill-shapes.md)
@@ -0,0 +1,48 @@
---
title: "How to: Create Application Settings"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "application settings [Windows Forms], Windows Forms"
- "application settings [Windows Forms], creating"
ms.assetid: 1e7aa347-af75-41e5-89ca-f53cab704f72
---
# How to: Create Application Settings
Using managed code, you can create new application settings and bind them to properties on your form or your form's controls, so that these settings are loaded and saved automatically at run time.
In the following procedure, you manually create a wrapper class that derives from <xref:System.Configuration.ApplicationSettingsBase>. To this class you add a publicly accessible property for each application setting that you want to expose.
You can also perform this procedure using minimal code in the Visual Studio designer. Also see [How to: Create Application Settings Using the Designer](https://docs.microsoft.com/previous-versions/visualstudio/visual-studio-2010/wabtadw6(v=vs.100)).
### To create new Application Settings programmatically
1. Add a new class to your project, and rename it. For this procedure, we will call this class `MyUserSettings`. Change the class definition so that the class derives from <xref:System.Configuration.ApplicationSettingsBase>.
2. Define a property on this wrapper class for each application setting you require, and apply that property with either the <xref:System.Configuration.ApplicationScopedSettingAttribute> or <xref:System.Configuration.UserScopedSettingAttribute>, depending on the scope of the setting. For more information about settings scope, see [Application Settings Overview](application-settings-overview.md). By now, your code should look like this:
[!code-csharp[ApplicationSettings.Create#1](~/samples/snippets/csharp/VS_Snippets_Winforms/ApplicationSettings.Create/CS/MyAppSettings.cs#1)]
[!code-vb[ApplicationSettings.Create#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ApplicationSettings.Create/VB/MyAppSettings.vb#1)]
3. Create an instance of this wrapper class in your application. It will commonly be a private member of the main form. Now that you have defined your class, you need to bind it to a property; in this case, the <xref:System.Windows.Forms.Form.BackColor%2A> property of your form. You can accomplish this in your form's `Load` event handler.
[!code-csharp[ApplicationSettings.Create#2](~/samples/snippets/csharp/VS_Snippets_Winforms/ApplicationSettings.Create/CS/Form1.cs#2)]
[!code-vb[ApplicationSettings.Create#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ApplicationSettings.Create/VB/Form1.vb#2)]
4. If you provide a way to change settings at run time, you will need to save the user's current settings to disk when your form closes, or else these changes will be lost.
[!code-csharp[ApplicationSettings.Create#3](~/samples/snippets/csharp/VS_Snippets_Winforms/ApplicationSettings.Create/CS/Form1.cs#3)]
[!code-vb[ApplicationSettings.Create#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/ApplicationSettings.Create/VB/Form1.vb#3)]
You have now successfully created a new application setting and bound it to the specified property.
## .NET Framework Security
The default settings provider, <xref:System.Configuration.LocalFileSettingsProvider>, persists information to configuration files as plain text. This limits security to the file access security provided by the operating system for the current user. Because of this, care must be taken with the information stored in configuration files. For example, one common use for application settings is to store connection strings that point to the application's data store. However, because of security concerns, such strings should not include passwords. For more information about connection strings, see <xref:System.Configuration.SpecialSetting>.
## See also
- <xref:System.Configuration.SpecialSettingAttribute>
- <xref:System.Configuration.LocalFileSettingsProvider>
- [Application Settings Overview](application-settings-overview.md)
- [How to: Validate Application Settings](how-to-validate-application-settings.md)
@@ -0,0 +1,35 @@
---
title: "How to: Create Figures from Lines, Curves, and Shapes"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "figures [Windows Forms], creating from shapes"
- "figures [Windows Forms], creating from lines"
ms.assetid: 82fd56c7-b443-4765-9b7c-62ce030656ec
---
# How to: Create Figures from Lines, Curves, and Shapes
To create a figure, construct a <xref:System.Drawing.Drawing2D.GraphicsPath>, and then call methods, such as <xref:System.Drawing.Drawing2D.GraphicsPath.AddLine%2A> and <xref:System.Drawing.Drawing2D.GraphicsPath.AddCurve%2A>, to add primitives to the path.
## Example
The following code examples create paths that have figures:
- The first example creates a path that has a single figure. The figure consists of a single arc. The arc has a sweep angle of 180 degrees, which is counterclockwise in the default coordinate system.
- The second example creates a path that has two figures. The first figure is an arc followed by a line. The second figure is a line followed by a curve followed by a line. The first figure is left open, and the second figure is closed.
[!code-csharp[System.Drawing.ConstructingDrawingPaths#21](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingPaths/CS/Class1.cs#21)]
[!code-vb[System.Drawing.ConstructingDrawingPaths#21](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingPaths/VB/Class1.vb#21)]
[!code-csharp[System.Drawing.ConstructingDrawingPaths#22](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingPaths/CS/Class1.cs#22)]
[!code-vb[System.Drawing.ConstructingDrawingPaths#22](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingPaths/VB/Class1.vb#22)]
## Compiling the Code
The previous examples are designed for use with Windows Forms, and they require <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- <xref:System.Drawing.Drawing2D.GraphicsPath>
- [Constructing and Drawing Paths](constructing-and-drawing-paths.md)
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
@@ -0,0 +1,173 @@
---
title: "How to: Create Graphics Objects for Drawing"
description: Learn now to create a Graphic object that you need to draw lines and shapes, render text, or display and manipulate images with GDI+.
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "graphics [Windows Forms], creating"
- "images [Windows Forms], creating"
- "GDI+, creating images"
ms.assetid: 162861f9-f050-445e-8abb-b2c43a918b8b
---
# How to: Create Graphics Objects for Drawing
Before you can draw lines and shapes, render text, or display and manipulate images with GDI+, you need to create a <xref:System.Drawing.Graphics> object. The <xref:System.Drawing.Graphics> object represents a GDI+ drawing surface, and is the object that is used to create graphical images.
There are two steps in working with graphics:
1. Creating a <xref:System.Drawing.Graphics> object.
2. Using the <xref:System.Drawing.Graphics> object to draw lines and shapes, render text, or display and manipulate images.
## Creating a Graphics Object
A graphics object can be created in a variety of ways.
#### To create a graphics object
- Receive a reference to a graphics object as part of the <xref:System.Windows.Forms.PaintEventArgs> in the <xref:System.Windows.Forms.Control.Paint> event of a form or control. This is usually how you obtain a reference to a graphics object when creating painting code for a control. Similarly, you can also obtain a graphics object as a property of the <xref:System.Drawing.Printing.PrintPageEventArgs> when handling the <xref:System.Drawing.Printing.PrintDocument.PrintPage> event for a <xref:System.Drawing.Printing.PrintDocument>.
-or-
- Call the <xref:System.Windows.Forms.Control.CreateGraphics%2A> method of a control or form to obtain a reference to a <xref:System.Drawing.Graphics> object that represents the drawing surface of that control or form. Use this method if you want to draw on a form or control that already exists.
-or-
- Create a <xref:System.Drawing.Graphics> object from any object that inherits from <xref:System.Drawing.Image>. This approach is useful when you want to alter an already existing image.
The following sections give details about each of these processes.
## PaintEventArgs in the Paint Event Handler
When programming the <xref:System.Windows.Forms.PaintEventHandler> for controls or the <xref:System.Drawing.Printing.PrintDocument.PrintPage> for a <xref:System.Drawing.Printing.PrintDocument>, a graphics object is provided as one of the properties of <xref:System.Windows.Forms.PaintEventArgs> or <xref:System.Drawing.Printing.PrintPageEventArgs>.
#### To obtain a reference to a Graphics object from the PaintEventArgs in the Paint event
1. Declare the <xref:System.Drawing.Graphics> object.
2. Assign the variable to refer to the <xref:System.Drawing.Graphics> object passed as part of the <xref:System.Windows.Forms.PaintEventArgs>.
3. Insert code to paint the form or control.
The following example shows how to reference a <xref:System.Drawing.Graphics> object from the <xref:System.Windows.Forms.PaintEventArgs> in the <xref:System.Windows.Forms.Control.Paint> event:
```vb
Private Sub Form1_Paint(sender As Object, pe As PaintEventArgs) Handles _
MyBase.Paint
' Declares the Graphics object and sets it to the Graphics object
' supplied in the PaintEventArgs.
Dim g As Graphics = pe.Graphics
' Insert code to paint the form here.
End Sub
```
```csharp
private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs pe)
{
// Declares the Graphics object and sets it to the Graphics object
// supplied in the PaintEventArgs.
Graphics g = pe.Graphics;
// Insert code to paint the form here.
}
```
```cpp
private:
void Form1_Paint(System::Object ^ sender,
System::Windows::Forms::PaintEventArgs ^ pe)
{
// Declares the Graphics object and sets it to the Graphics object
// supplied in the PaintEventArgs.
Graphics ^ g = pe->Graphics;
// Insert code to paint the form here.
}
```
## CreateGraphics Method
You can also use the <xref:System.Windows.Forms.Control.CreateGraphics%2A> method of a control or form to obtain a reference to a <xref:System.Drawing.Graphics> object that represents the drawing surface of that control or form.
#### To create a Graphics object with the CreateGraphics method
- Call the <xref:System.Windows.Forms.Control.CreateGraphics%2A> method of the form or control upon which you want to render graphics.
```vb
Dim g as Graphics
' Sets g to a Graphics object representing the drawing surface of the
' control or form g is a member of.
g = Me.CreateGraphics
```
```csharp
Graphics g;
// Sets g to a graphics object representing the drawing surface of the
// control or form g is a member of.
g = this.CreateGraphics();
```
```cpp
Graphics ^ g;
// Sets g to a graphics object representing the drawing surface of the
// control or form g is a member of.
g = this->CreateGraphics();
```
## Create from an Image Object
Additionally, you can create a graphics object from any object that derives from the <xref:System.Drawing.Image> class.
#### To create a Graphics object from an Image
- Call the <xref:System.Drawing.Graphics.FromImage%2A?displayProperty=nameWithType> method, supplying the name of the Image variable from which you want to create a <xref:System.Drawing.Graphics> object.
The following example shows how to use a <xref:System.Drawing.Bitmap> object:
```vb
Dim myBitmap as New Bitmap("C:\Documents and Settings\Joe\Pics\myPic.bmp")
Dim g as Graphics = Graphics.FromImage(myBitmap)
```
```csharp
Bitmap myBitmap = new Bitmap(@"C:\Documents and
Settings\Joe\Pics\myPic.bmp");
Graphics g = Graphics.FromImage(myBitmap);
```
```cpp
Bitmap ^ myBitmap = gcnew
Bitmap("D:\\Documents and Settings\\Joe\\Pics\\myPic.bmp");
Graphics ^ g = Graphics::FromImage(myBitmap);
```
> [!NOTE]
> You can only create <xref:System.Drawing.Graphics> objects from nonindexed .bmp files, such as 16-bit, 24-bit, and 32-bit .bmp files. Each pixel of nonindexed .bmp files holds a color, in contrast to pixels of indexed .bmp files, which hold an index to a color table.
## Drawing and Manipulating Shapes and Images
After it is created, a <xref:System.Drawing.Graphics> object may be used to draw lines and shapes, render text, or display and manipulate images. The principal objects that are used with the <xref:System.Drawing.Graphics> object are:
- The <xref:System.Drawing.Pen> class—Used for drawing lines, outlining shapes, or rendering other geometric representations.
- The <xref:System.Drawing.Brush> class—Used for filling areas of graphics, such as filled shapes, images, or text.
- The <xref:System.Drawing.Font> class—Provides a description of what shapes to use when rendering text.
- The <xref:System.Drawing.Color> structure—Represents the different colors to display.
#### To use the Graphics object you have created
- Work with the appropriate object listed above to draw what you need.
For more information, see the following topics:
|To render|See|
|---------------|---------|
|Lines|[How to: Draw a Line on a Windows Form](how-to-draw-a-line-on-a-windows-form.md)|
|Shapes|[How to: Draw an Outlined Shape](how-to-draw-an-outlined-shape.md)|
|Text|[How to: Draw Text on a Windows Form](how-to-draw-text-on-a-windows-form.md)|
|Images|[How to: Render Images with GDI+](how-to-render-images-with-gdi.md)|
## See also
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [How to: Render Images with GDI+](how-to-render-images-with-gdi.md)
@@ -0,0 +1,108 @@
---
title: "How to: Create MDI Child Forms"
description: Learn how to use Visual Studio to create a Multiple-Document Interface (MDI) child form that displays a RichTextBox control.
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "MDI [Windows Forms], creating forms"
- "child forms"
ms.assetid: 164b69bb-2eca-4339-ada3-0679eb2c6dda
---
# How to: Create MDI child forms
MDI child forms are an essential element of [Multiple-Document Interface (MDI) applications](multiple-document-interface-mdi-applications.md), as these forms are the center of user interaction.
In the following procedure, you'll use Visual Studio to create an MDI child form that displays a <xref:System.Windows.Forms.RichTextBox> control, similar to most word-processing applications. By substituting the <xref:System.Windows.Forms> control with other controls, such as the <xref:System.Windows.Forms.DataGridView> control, or a mixture of controls, you can create MDI child windows (and, by extension, MDI applications) with diverse possibilities.
## Create MDI child forms
1. Create a new Windows Forms application project in Visual Studio. In the **Properties** window for the form, set its <xref:System.Windows.Forms.Form.IsMdiContainer%2A> property to `true` and its `WindowsState` property to `Maximized`.
This designates the form as an MDI container for child windows.
2. From the `Toolbox`, drag a <xref:System.Windows.Forms.MenuStrip> control to the form. Set its `Text` property to **File**.
3. Click the ellipsis (…) next to the **Items** property, and click **Add** to add two child tool strip menu items. Set the `Text` property for these items to **New** and **Window**.
4. In **Solution Explorer**, right-click the project, and then select **Add** > **New Item**.
5. In the **Add New Item** dialog box, select **Windows Form** (in Visual Basic or in Visual C#) or **Windows Forms Application (.NET)** (in Visual C++) from the **Templates** pane. In the **Name** box, name the form **Form2**. Select **Open** to add the form to the project.
> [!NOTE]
> The MDI child form you created in this step is a standard Windows Form. As such, it has an <xref:System.Windows.Forms.Form.Opacity%2A> property, which enables you to control the transparency of the form. However, the <xref:System.Windows.Forms.Form.Opacity%2A> property was designed for top-level windows. Do not use it with MDI child forms, as painting problems can occur.
This form will be the template for your MDI child forms.
The **Windows Forms Designer** opens, displaying **Form2**.
6. From the **Toolbox**, drag a **RichTextBox** control to the form.
7. In the **Properties** window, set the `Anchor` property to **Top, Left** and the `Dock` property to **Fill**.
This causes the <xref:System.Windows.Forms.RichTextBox> control to completely fill the area of the MDI child form, even when the form is resized.
8. Double click the **New** menu item to create a <xref:System.Windows.Forms.Control.Click> event handler for it.
9. Insert code similar to the following to create a new MDI child form when the user clicks the **New** menu item.
> [!NOTE]
> In the following example, the event handler handles the <xref:System.Windows.Forms.Control.Click> event for `MenuItem2`. Be aware that, depending on the specifics of your application architecture, your **New** menu item may not be `MenuItem2`.
```vb
Protected Sub MDIChildNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click
Dim NewMDIChild As New Form2()
'Set the Parent Form of the Child window.
NewMDIChild.MdiParent = Me
'Display the new form.
NewMDIChild.Show()
End Sub
```
```csharp
protected void MDIChildNew_Click(object sender, System.EventArgs e){
Form2 newMDIChild = new Form2();
// Set the Parent Form of the Child window.
newMDIChild.MdiParent = this;
// Display the new form.
newMDIChild.Show();
}
```
```cpp
private:
void menuItem2_Click(System::Object ^ sender,
System::EventArgs ^ e)
{
Form2^ newMDIChild = gcnew Form2();
// Set the Parent Form of the Child window.
newMDIChild->MdiParent = this;
// Display the new form.
newMDIChild->Show();
}
```
In C++, add the following `#include` directive at the top of Form1.h:
```cpp
#include "Form2.h"
```
10. In the drop-down list at the top of the **Properties** window, select the menu strip that corresponds to the **File** menu strip and set the <xref:System.Windows.Forms.MenuStrip.MdiWindowListItem%2A> property to the Window <xref:System.Windows.Forms.ToolStripMenuItem>.
This enables the **Window** menu to maintain a list of open MDI child windows with a check mark next to the active child window.
11. Press **F5** to run the application. By selecting **New** from the **File** menu, you can create new MDI child forms, which are kept track of in the **Window** menu item.
> [!NOTE]
> When an MDI child form has a <xref:System.Windows.Forms.MainMenu> component (with, usually, a menu structure of menu items) and it is opened within an MDI parent form that has a <xref:System.Windows.Forms.MainMenu> component (with, usually, a menu structure of menu items), the menu items will merge automatically if you have set the <xref:System.Windows.Forms.MenuItem.MergeType%2A> property (and optionally, the <xref:System.Windows.Forms.MenuItem.MergeOrder%2A> property). Set the <xref:System.Windows.Forms.MenuItem.MergeType%2A> property of both <xref:System.Windows.Forms.MainMenu> components and all of the menu items of the child form to <xref:System.Windows.Forms.MenuMerge.MergeItems>. Additionally, set the <xref:System.Windows.Forms.MenuItem.MergeOrder%2A> property so that the menu items from both menus appear in the desired order. Moreover, keep in mind that when you close an MDI parent form, each of the MDI child forms raises a <xref:System.Windows.Forms.Form.Closing> event before the <xref:System.Windows.Forms.Form.Closing> event for the MDI parent is raised. Canceling an MDI child's <xref:System.Windows.Forms.Form.Closing> event will not prevent the MDI parent's <xref:System.Windows.Forms.Form.Closing> event from being raised; however, the <xref:System.ComponentModel.CancelEventArgs> argument for the MDI parent's <xref:System.Windows.Forms.Form.Closing> event will now be set to `true`. You can force the MDI parent and all MDI child forms to close by setting the <xref:System.ComponentModel.CancelEventArgs> argument to `false`.
## See also
- [Multiple-Document Interface (MDI) Applications](multiple-document-interface-mdi-applications.md)
- [How to: Create MDI Parent Forms](how-to-create-mdi-parent-forms.md)
- [How to: Determine the Active MDI Child](how-to-determine-the-active-mdi-child.md)
- [How to: Send Data to the Active MDI Child](how-to-send-data-to-the-active-mdi-child.md)
- [How to: Arrange MDI Child Forms](how-to-arrange-mdi-child-forms.md)
@@ -0,0 +1,40 @@
---
title: "How to: Create MDI Parent Forms"
description: Learn how to create an MDI parent form programmatically and by using the Windows Forms Designer.
ms.date: "03/30/2017"
helpviewer_keywords:
- "parent forms"
- "MDI [Windows Forms], creating forms"
ms.assetid: 12c71221-2377-4bb6-b10b-7b4b300fd462
---
# How to: Create MDI Parent Forms
> [!IMPORTANT]
> This topic uses the <xref:System.Windows.Forms.MainMenu> control, which has been replaced by the <xref:System.Windows.Forms.MenuStrip> control. The <xref:System.Windows.Forms.MainMenu> control is retained for both backward compatibility and future use, if you choose. For information about creating a MDI parent Form by using a <xref:System.Windows.Forms.MenuStrip>, see [How to: Create an MDI Window List with MenuStrip](../controls/how-to-create-an-mdi-window-list-with-menustrip-windows-forms.md).
The foundation of a Multiple-Document Interface (MDI) application is the MDI parent form. This is the form that contains the MDI child windows, which are the sub-windows wherein the user interacts with the MDI application. Creating an MDI parent form is easy, both in the Windows Forms Designer and programmatically.
## Create an MDI parent form at design time
1. Create a Windows Application project in Visual Studio.
2. In the **Properties** window, set the <xref:System.Windows.Forms.Form.IsMdiContainer%2A> property to **true**.
This designates the form as an MDI container for child windows.
> [!NOTE]
> While setting properties in the **Properties** window, you can also set the `WindowState` property to **Maximized**, if you like, as it is easiest to manipulate MDI child windows when the parent form is maximized. Additionally, be aware that the edge of the MDI parent form will pick up the system color (set in the Windows System Control Panel), rather than the back color you set using the <xref:System.Windows.Forms.Control.BackColor%2A?displayProperty=nameWithType> property.
3. From the **Toolbox**, drag a **MenuStrip** control to the form. Create a top-level menu item with the **Text** property set to **&File** with submenu items called **&New** and **&Close**. Also create a top-level menu item called **&Window**.
The first menu will create and hide menu items at run time, and the second menu will keep track of the open MDI child windows. At this point, you have created an MDI parent window.
4. Press **F5** to run the application. For information about creating MDI child windows that operate within the MDI parent form, see [How to: Create MDI Child Forms](how-to-create-mdi-child-forms.md).
## See also
- [Multiple-Document Interface (MDI) Applications](multiple-document-interface-mdi-applications.md)
- [How to: Create MDI Child Forms](how-to-create-mdi-child-forms.md)
- [How to: Determine the Active MDI Child](how-to-determine-the-active-mdi-child.md)
- [How to: Send Data to the Active MDI Child](how-to-send-data-to-the-active-mdi-child.md)
- [How to: Arrange MDI Child Forms](how-to-arrange-mdi-child-forms.md)
@@ -0,0 +1,76 @@
---
title: Create Standard Print Jobs
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "printing [Windows Forms]"
- "printing [Windows Forms], creating print jobs"
- "printing [Visual Basic], in Windows applications"
ms.assetid: 03342b90-9cfe-40b2-838b-b479a13c5dea
---
# How to: Create Standard Windows Forms Print Jobs
The foundation of printing in Windows Forms is the <xref:System.Drawing.Printing.PrintDocument> component—more specifically, the <xref:System.Drawing.Printing.PrintDocument.PrintPage> event. By writing code to handle the <xref:System.Drawing.Printing.PrintDocument.PrintPage> event, you can specify what to print and how to print it.
### To create a print job
1. Add a <xref:System.Drawing.Printing.PrintDocument> component to your form.
2. Write code to handle the <xref:System.Drawing.Printing.PrintDocument.PrintPage> event.
You will have to code your own printing logic. Additionally, you will have to specify the material to be printed.
In the following code example, a sample graphic in the shape of a red rectangle is created in the <xref:System.Drawing.Printing.PrintDocument.PrintPage> event handler to act as material to be printed.
```vb
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
e.Graphics.FillRectangle(Brushes.Red, New Rectangle(500, 500, 500, 500))
End Sub
```
```csharp
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.FillRectangle(Brushes.Red,
new Rectangle(500, 500, 500, 500));
}
```
```cpp
private:
void printDocument1_PrintPage(System::Object ^ sender,
System::Drawing::Printing::PrintPageEventArgs ^ e)
{
e->Graphics->FillRectangle(Brushes::Red,
Rectangle(500, 500, 500, 500));
}
```
(Visual C# and Visual C++) Place the following code in the form's constructor to register the event handler.
```csharp
this.printDocument1.PrintPage += new
System.Drawing.Printing.PrintPageEventHandler
(this.printDocument1_PrintPage);
```
```cpp
printDocument1->PrintPage += gcnew
System::Drawing::Printing::PrintPageEventHandler
(this, &Form1::printDocument1_PrintPage);
```
You may also want to write code for the <xref:System.Drawing.Printing.PrintDocument.BeginPrint> and <xref:System.Drawing.Printing.PrintDocument.EndPrint> events, perhaps including an integer representing the total number of pages to print that is decremented as each page prints.
> [!NOTE]
> You can add a <xref:System.Windows.Forms.PrintDialog> component to your form to provide a clean and efficient user interface (UI) to your users. Setting the <xref:System.Windows.Forms.PrintDialog.Document%2A> property of the <xref:System.Windows.Forms.PrintDialog> component enables you to set properties related to the print document you are working with on your form. For more information about the <xref:System.Windows.Forms.PrintDialog> component, see [PrintDialog Component](../controls/printdialog-component-windows-forms.md).
For more information about the specifics of Windows Forms print jobs, including how to create a print job programmatically, see <xref:System.Drawing.Printing.PrintPageEventArgs>.
## See also
- <xref:System.Drawing.Printing.PrintDocument>
- [Windows Forms Print Support](windows-forms-print-support.md)
@@ -0,0 +1,50 @@
---
title: "How to: Create Thumbnail Images"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "thumbnail images [Windows Forms], creating"
- "images [Windows Forms], creating thumbnails"
ms.assetid: e956242a-1e5b-4217-a3cf-5f3fb45d00ba
---
# How to: Create Thumbnail Images
A thumbnail image is a small version of an image. You can create a thumbnail image by calling the <xref:System.Drawing.Image.GetThumbnailImage%2A> method of an <xref:System.Drawing.Image> object.
## Example
The following example constructs an <xref:System.Drawing.Image> object from a JPG file. The original image has a width of 640 pixels and a height of 479 pixels. The code creates a thumbnail image that has a width of 100 pixels and a height of 100 pixels.
The following illustration shows the thumbnail image:
![Screenshot that shows the output thumbnail.](./media/how-to-create-thumbnail-images/construct-thumbnail-image.png)
> [!NOTE]
> In this example, a callback method is declared, but never used. This supports all versions of GDI+.
[!code-csharp[System.Drawing.WorkingWithImages#71](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/CS/Class1.cs#71)]
[!code-vb[System.Drawing.WorkingWithImages#71](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/VB/Class1.vb#71)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler. To run the example, follow these steps:
1. Create a new Windows Forms application.
2. Add the example code to the form.
3. Create a handler for the form's <xref:System.Windows.Forms.Control.Paint> event
4. In the <xref:System.Windows.Forms.Control.Paint> handler, call the `GetThumbnail` method and pass `e` for <xref:System.Windows.Forms.PaintEventArgs>.
5. Find an image file that you want to make a thumbnail of.
6. In the `GetThumbnail` method, specify the path and file name to your image.
7. Press F5 to run the example.
A 100 by 100 thumbnail image appears on the form.
## See also
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
- [Working with Images, Bitmaps, Icons, and Metafiles](working-with-images-bitmaps-icons-and-metafiles.md)
@@ -0,0 +1,33 @@
---
title: "How to: Create Vertical Text"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "text [Windows Forms], drawing vertical"
- "Windows Forms, drawing vertical text"
- "strings [Windows Forms], drawing vertical"
- "vertical text [Windows Forms], drawing"
ms.assetid: 50c69046-4188-47d9-b949-cc2610ffd337
---
# How to: Create Vertical Text
You can use a <xref:System.Drawing.StringFormat> object to specify that text be drawn vertically rather than horizontally.
## Example
The following example assigns the value <xref:System.Drawing.StringFormatFlags.DirectionVertical> to the <xref:System.Drawing.StringFormat.FormatFlags%2A> property of a <xref:System.Drawing.StringFormat> object. That <xref:System.Drawing.StringFormat> object is passed to the <xref:System.Drawing.Graphics.DrawString%2A> method of the <xref:System.Drawing.Graphics> class. The value <xref:System.Drawing.StringFormatFlags.DirectionVertical> is a member of the <xref:System.Drawing.StringFormatFlags> enumeration.
The following illustration shows the vertical text:
![Graphic that shows vertical font text.](./media/how-to-create-vertical-text/vertical-font-text-graphic.png)
[!code-csharp[System.Drawing.FontsAndText#31](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs#31)]
[!code-vb[System.Drawing.FontsAndText#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.FontsAndText/VB/Class1.vb#31)]
## Compiling the Code
- The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e` , which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [How to: Draw Text with GDI](how-to-draw-text-with-gdi.md)
@@ -0,0 +1,35 @@
---
title: "How to: Crop and Scale Images"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "images [Windows Forms], cropping"
- "images [Windows Forms], scaling"
ms.assetid: 053e3360-bca0-4b25-9afa-0e77a6f17b03
---
# How to: Crop and Scale Images
The <xref:System.Drawing.Graphics> class provides several <xref:System.Drawing.Graphics.DrawImage%2A> methods, some of which have source and destination rectangle parameters that you can use to crop and scale images.
## Example
The following example constructs an <xref:System.Drawing.Image> object from the disk file Apple.gif. The code draws the entire apple image in its original size. The code then calls the <xref:System.Drawing.Graphics.DrawImage%2A> method of a <xref:System.Drawing.Graphics> object to draw a portion of the apple image in a destination rectangle that is larger than the original apple image.
The <xref:System.Drawing.Graphics.DrawImage%2A> method determines which portion of the apple to draw by looking at the source rectangle, which is specified by the third, fourth, fifth, and sixth arguments. In this case, the apple is cropped to 75 percent of its width and 75 percent of its height.
The <xref:System.Drawing.Graphics.DrawImage%2A> method determines where to draw the cropped apple and how big to make the cropped apple by looking at the destination rectangle, which is specified by the second argument. In this case, the destination rectangle is 30 percent wider and 30 percent taller than the original image.
The following illustration shows the original apple and the scaled, cropped apple.
![Screenshot of an original image and the same image cropped.](./media/how-to-crop-and-scale-images/original-image-cropped-image.png)
[!code-csharp[System.Drawing.WorkingWithImages#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/CS/Class1.cs#11)]
[!code-vb[System.Drawing.WorkingWithImages#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/VB/Class1.vb#11)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler. Make sure to replace `Apple.gif` with an image file name and path that are valid on your system.
## See also
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
- [Working with Images, Bitmaps, Icons, and Metafiles](working-with-images-bitmaps-icons-and-metafiles.md)
@@ -0,0 +1,86 @@
---
title: "How to: Determine the Active MDI Child"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "Clipboard [Windows Forms], copying data to"
- "MDI [Windows Forms], child windows"
- "child forms"
- "MDI [Windows Forms], activating forms"
- "MDI [Windows Forms], locating focus"
ms.assetid: 33880ec3-0207-4c2b-a616-ff140443cc0f
---
# How to: Determine the Active MDI Child
On occasion, you will want to provide a command that operates on the control that has focus on the currently active child form. For example, suppose you want to copy selected text from the child form's text box to the Clipboard. You would create a procedure that copies selected text to the Clipboard using the <xref:System.Windows.Forms.Control.Click> event of the Copy menu item on the standard Edit menu.
Because an MDI application can have many instances of the same child form, the procedure needs to know which form to use. To specify the correct form, use the <xref:System.Windows.Forms.Form.ActiveMdiChild%2A> property, which returns the child form that has the focus or that was most recently active.
When you have several controls on a form, you also need to specify which control is active. Like the <xref:System.Windows.Forms.Form.ActiveMdiChild%2A> property, the <xref:System.Windows.Forms.ContainerControl.ActiveControl%2A> property returns the control with the focus on the active child form. The procedure below illustrates a copy procedure that can be called from a child form menu, a menu on the MDI form, or a toolbar button.
### To determine the active MDI child (to copy its text to the Clipboard)
1. Within a method, copy the text of the active control of the active child form to the Clipboard.
> [!NOTE]
> This example assumes there is an MDI parent form (`Form1`) that has one or more MDI child windows containing a <xref:System.Windows.Forms.RichTextBox> control. For more information, see [Creating MDI Parent Forms](how-to-create-mdi-parent-forms.md).
```vb
Public Sub mniCopy_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles mniCopy.Click
' Determine the active child form.
Dim activeChild As Form = Me.ActiveMDIChild
' If there is an active child form, find the active control, which
' in this example should be a RichTextBox.
If (Not activeChild Is Nothing) Then
Dim theBox As RichTextBox = _
TryCast(activeChild.ActiveControl, RichTextBox)
If (Not theBox Is Nothing) Then
'Put selected text on Clipboard.
Clipboard.SetDataObject(theBox.SelectedText)
Else
MessageBox.Show("You need to select a RichTextBox.")
End If
End If
End Sub
```
```csharp
protected void mniCopy_Click (object sender, System.EventArgs e)
{
// Determine the active child form.
Form activeChild = this.ActiveMdiChild;
// If there is an active child form, find the active control, which
// in this example should be a RichTextBox.
if (activeChild != null)
{
try
{
RichTextBox theBox = (RichTextBox)activeChild.ActiveControl;
if (theBox != null)
{
// Put the selected text on the Clipboard.
Clipboard.SetDataObject(theBox.SelectedText);
}
}
catch
{
MessageBox.Show("You need to select a RichTextBox.");
}
}
}
```
## See also
- [Multiple-Document Interface (MDI) Applications](multiple-document-interface-mdi-applications.md)
- [How to: Create MDI Parent Forms](how-to-create-mdi-parent-forms.md)
- [How to: Create MDI Child Forms](how-to-create-mdi-child-forms.md)
- [How to: Send Data to the Active MDI Child](how-to-send-data-to-the-active-mdi-child.md)
- [How to: Arrange MDI Child Forms](how-to-arrange-mdi-child-forms.md)
@@ -0,0 +1,31 @@
---
title: "How to: Determine the Parameters Supported by an Encoder"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "encoder parameters [Windows Forms], determining supported"
ms.assetid: f47ae459-e3ce-4d41-a140-2f6c6aea3f44
---
# How to: Determine the Parameters Supported by an Encoder
You can adjust image parameters, such as quality and compression level, but you must know which parameters are supported by a given image encoder. The <xref:System.Drawing.Image> class provides the <xref:System.Drawing.Image.GetEncoderParameterList%2A> method so that you can determine which image parameters are supported for a particular encoder. You specify the encoder with a GUID. The <xref:System.Drawing.Image.GetEncoderParameterList%2A> method returns an array of <xref:System.Drawing.Imaging.EncoderParameter> objects.
## Example
The following example code outputs the supported parameters for the JPEG encoder. Use the list of parameter categories and associated GUIDs in the <xref:System.Drawing.Imaging.Encoder> class overview to determine the category for each parameter.
[!code-csharp[UsingImageEncodersDecoders#3](~/samples/snippets/csharp/VS_Snippets_Winforms/UsingImageEncodersDecoders/CS/Form1.cs#3)]
[!code-vb[UsingImageEncodersDecoders#3](~/samples/snippets/visualbasic/VS_Snippets_Winforms/UsingImageEncodersDecoders/VB/Form1.vb#3)]
## Compiling the Code
This example requires:
- A Windows Forms application.
- A <xref:System.Windows.Forms.PaintEventArgs>, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [How to: List Installed Encoders](how-to-list-installed-encoders.md)
- [Types of Bitmaps](types-of-bitmaps.md)
- [Using Image Encoders and Decoders in Managed GDI+](using-image-encoders-and-decoders-in-managed-gdi.md)
@@ -0,0 +1,41 @@
---
title: "How to: Display Pop-up Help"
ms.date: "03/30/2017"
helpviewer_keywords:
- "pop-up Help"
- "Help [Windows Forms], pop-up Help"
- "Windows Forms, displaying Help"
- "forms [Windows Forms], displaying Help"
- "modal dialog boxes [Windows Forms], pop-up Help"
- "F1 Help [Windows Forms], in dialog boxes"
- "HelpProvider component [Windows Forms]"
- "Help [Windows Forms], adding to dialog boxes"
ms.assetid: 218aa81e-e87e-4d67-af05-11627bbdce3b
---
# How to: Display pop-up Help
One way to display Help on Windows Forms is through the **Help** button, located on the right side of the title bar, accessible through the <xref:System.Windows.Forms.Form.HelpButton%2A> property. This type of Help display is well-suited for use with dialog boxes. Dialog boxes shown modally (with the <xref:System.Windows.Forms.Form.ShowDialog%2A> method) have trouble bringing up external Help systems, because modal dialog boxes need to be closed before focus can shift to another window. Additionally, using the **Help** button requires that there is no **Minimize** button or **Maximize** button shown in the title bar. This is a standard dialog-box convention, whereas forms usually have **Minimize** and **Maximize** buttons.
You can also use the <xref:System.Windows.Forms.HelpProvider> component to link controls to files in a Help system, even if you have implemented pop-up Help. For more information, see [Providing Help in a Windows Application](how-to-provide-help-in-a-windows-application.md).
## Display pop-up Help
1. In Visual Studio, drag a [HelpProvider](../controls/helpprovider-component-windows-forms.md) component from the Toolbox to your form.
It will sit in the tray at the bottom of the Windows Forms Designer.
2. In the Properties window, set the <xref:System.Windows.Forms.Form.HelpButton%2A> property to `true`. This will display a button with a question mark in it on the right side of the title bar of the form.
3. In order for the <xref:System.Windows.Forms.Form.HelpButton%2A> to display, the form's <xref:System.Windows.Forms.Form.MinimizeBox%2A> and <xref:System.Windows.Forms.Form.MaximizeBox%2A> properties must be set to `false`, the <xref:System.Windows.Forms.Form.ControlBox%2A> property set to `true`, and the <xref:System.Windows.Forms.Form.FormBorderStyle%2A> property to one of the following values: <xref:System.Windows.Forms.FormBorderStyle.FixedSingle>, <xref:System.Windows.Forms.FormBorderStyle.Fixed3D>, <xref:System.Windows.Forms.FormBorderStyle.FixedDialog> or <xref:System.Windows.Forms.FormBorderStyle.Sizable>.
4. Select the control for which you want to show help on your form and set the Help string in the Properties window. This is the string of text that will be displayed in a window similar to a [ToolTip](../controls/tooltip-component-windows-forms.md).
5. Press **F5**.
6. Press the **Help** button on the title bar and click the control on which you set the Help string.
## See also
- [Control Help Using ToolTips](control-help-using-tooltips.md)
- [Integrating User Help in Windows Forms](integrating-user-help-in-windows-forms.md)
- [Windows Forms](../index.yml)
@@ -0,0 +1,31 @@
---
title: "How to: Draw a Custom Dashed Line"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "lines [Windows Forms], custom"
- "lines [Windows Forms], drawing"
- "lines [Windows Forms], dashed"
ms.assetid: cd0ed96a-cce4-47b9-b58a-3bae2e3d1bee
---
# How to: Draw a Custom Dashed Line
GDI+ provides several dash styles that are listed in the <xref:System.Drawing.Drawing2D.DashStyle> enumeration. If those standard dash styles do not suit your needs, you can create a custom dash pattern.
## Example
To draw a custom dashed line, put the lengths of the dashes and spaces in an array and assign the array as the value of the <xref:System.Drawing.Pen.DashPattern%2A> property of a <xref:System.Drawing.Pen> object. The following example draws a custom dashed line based on the array `{5, 2, 15, 4}`. If you multiply the elements of the array by the pen width of 5, you get `{25, 10, 75, 20}`. The displayed dashes alternate in length between 25 and 75, and the spaces alternate in length between 10 and 20.
The following illustration shows the resulting dashed line. Note that the final dash has to be shorter than 25 units so that the line can end at (405, 5).
![Illustration that shows a dashed line.](./media/how-to-draw-a-custom-dashed-line/dashed-line-illustration.gif "pens6")
[!code-csharp[System.Drawing.UsingAPen#51](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingAPen/CS/Class1.cs#51)]
[!code-vb[System.Drawing.UsingAPen#51](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingAPen/VB/Class1.vb#51)]
## Compiling the Code
Create a Windows Form and handle the form's <xref:System.Windows.Forms.Control.Paint> event. Paste the preceding code into the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
@@ -0,0 +1,38 @@
---
title: "How to: Draw a Filled Ellipse on a Windows Form"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
f1_keywords:
- "Graphics.FillEllipse"
helpviewer_keywords:
- "ellipses [Windows Forms], drawing"
- "circles [Windows Forms], drawing"
- "circular shapes"
- "drawing [Windows Forms], ellipses"
- "shapes [Windows Forms], drawing"
- "forms [Windows Forms], drawing ellipses"
ms.assetid: 781db806-950d-4c5b-b022-493f7fd0c4a8
---
# How to: Draw a Filled Ellipse on a Windows Form
This example draws a filled ellipse on a form.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#1](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#1)]
[!code-csharp[System.Drawing.ConceptualHowTos#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#1)]
[!code-vb[System.Drawing.ConceptualHowTos#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#1)]
## Compiling the Code
You cannot call this method in the <xref:System.Windows.Forms.Form.Load> event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the <xref:System.Windows.Forms.Control.OnPaint%2A> method.
## Robust Programming
You should always call <xref:System.IDisposable.Dispose%2A> on any objects that consume system resources, such as <xref:System.Drawing.Brush> and <xref:System.Drawing.Graphics> objects.
## See also
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Alpha Blending Lines and Fills](alpha-blending-lines-and-fills.md)
- [Using a Brush to Fill Shapes](using-a-brush-to-fill-shapes.md)
@@ -0,0 +1,38 @@
---
title: "How to: Draw a Filled Rectangle on a Windows Form"
description: Learn how to programmatically draw a filled rectangle on a Windows Form. Also learn about compiling your code.
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
f1_keywords:
- "Graphics.FillRectangle"
helpviewer_keywords:
- "drawing [Windows Forms], rectangles"
- "rectangles [Windows Forms], drawing"
- "drawing rectangles"
ms.assetid: d656a93c-987d-4809-aafd-493fe17450f0
---
# How to: Draw a Filled Rectangle on a Windows Form
This example draws a filled rectangle on a form.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#2](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#2)]
[!code-csharp[System.Drawing.ConceptualHowTos#2](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#2)]
[!code-vb[System.Drawing.ConceptualHowTos#2](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#2)]
## Compiling the Code
You cannot call this method in the <xref:System.Windows.Forms.Form.Load> event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the <xref:System.Windows.Forms.Control.OnPaint%2A> method.
## Robust Programming
You should always call <xref:System.IDisposable.Dispose%2A> on any objects that consume system resources, such as <xref:System.Drawing.Brush> and <xref:System.Drawing.Graphics> objects.
## See also
- <xref:System.Drawing.Graphics.FillRectangle%2A>
- <xref:System.Windows.Forms.Control.OnPaint%2A>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
- [Brushes and Filled Shapes in GDI+](brushes-and-filled-shapes-in-gdi.md)
@@ -0,0 +1,32 @@
---
title: "How to: Draw a Line Filled with a Texture"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "drawing [Windows Forms], lines"
- "lines [Windows Forms], texture"
- "drawing lines [Windows Forms], texture"
ms.assetid: dc9118cc-f3c2-42e5-8173-f46d41d18fd5
---
# How to: Draw a Line Filled with a Texture
Instead of drawing a line with a solid color, you can draw a line with a texture. To draw lines and curves with a texture, create a <xref:System.Drawing.TextureBrush> object, and pass that <xref:System.Drawing.TextureBrush> object to a <xref:System.Drawing.Pen.%23ctor%2A> constructor. The bitmap associated with the texture brush is used to tile the plane (invisibly), and when the pen draws a line or curve, the stroke of the pen uncovers certain pixels of the tiled texture.
## Example
The following example creates a <xref:System.Drawing.Bitmap> object from the file `Texture1.jpg`. That bitmap is used to construct a <xref:System.Drawing.TextureBrush> object, and the <xref:System.Drawing.TextureBrush> object is used to construct a <xref:System.Drawing.Pen> object. The call to <xref:System.Drawing.Graphics.DrawImage%2A> draws the bitmap with its upper-left corner at (0, 0). The call to <xref:System.Drawing.Graphics.DrawEllipse%2A> uses the <xref:System.Drawing.Pen> object to draw a textured ellipse.
The following illustration shows the bitmap and the textured ellipse:
![Screenshot that shows the bitmap and the textured ellipse.](./media/how-to-draw-a-line-filled-with-a-texture/bitmap-textured-ellipse.png)
[!code-csharp[System.Drawing.UsingAPen#61](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingAPen/CS/Class1.cs#61)]
[!code-vb[System.Drawing.UsingAPen#61](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingAPen/VB/Class1.vb#61)]
## Compiling the Code
Create a Windows Form and handle the form's <xref:System.Windows.Forms.Control.Paint> event. Paste the preceding code into the <xref:System.Windows.Forms.Control.Paint> event handler. Replace `Texture.jpg` with an image valid on your system.
## See also
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
@@ -0,0 +1,36 @@
---
title: "How to: Draw a Line on a Windows Form"
description: Learn how to draws a line on a form by handling the Paint event, and then perform the drawing using the Graphics property of the PaintEventArgs.
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
f1_keywords:
- "Graphics.DrawLine"
helpviewer_keywords:
- "examples [Windows Forms], drawing lines on forms"
- "drawing [Windows Forms], lines"
- "lines [Windows Forms], drawing"
- "drawing lines"
ms.assetid: 55c1dbeb-75d0-430c-9814-a24b8971ad8c
---
# How to: Draw a Line on a Windows Form
This example draws a line on a form. Typically, when you draw on a form, you handle the forms <xref:System.Windows.Forms.Control.Paint> event and perform the drawing using the <xref:System.Windows.Forms.PaintEventArgs.Graphics%2A> property of the <xref:System.Windows.Forms.PaintEventArgs>, as shown in this example
## Example
[!code-csharp[System.Drawing.UsingAPen#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingAPen/CS/Class1.cs#11)]
[!code-vb[System.Drawing.UsingAPen#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingAPen/VB/Class1.vb#11)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs>`e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## Robust Programming
You should always call <xref:System.IDisposable.Dispose%2A> on any objects that consume system resources, such as <xref:System.Drawing.Pen> objects.
## See also
- <xref:System.Drawing.Graphics.DrawLine%2A>
- <xref:System.Windows.Forms.Control.OnPaint%2A>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
@@ -0,0 +1,36 @@
---
title: "How to: Draw a Line with Line Caps"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "drawing [Windows Forms], lines"
- "lines [Windows Forms], drawing"
- "pens [Windows Forms], drawing lines"
- "drawing lines [Windows Forms], line caps"
ms.assetid: eb68c3e1-c400-4886-8a04-76978a429cb6
---
# How to: Draw a Line with Line Caps
You can draw the start or end of a line in one of several shapes called line caps. GDI+ supports several line caps, such as round, square, diamond, and arrowhead.
## Example
You can specify line caps for the start of a line (start cap), the end of a line (end cap), or the dashes of a dashed line (dash cap).
The following example draws a line with an arrowhead at one end and a round cap at the other end. The illustration shows the resulting line:
![Illustration that shows a line with a round cap.](./media/how-to-draw-a-line-with-line-caps/line-cap-arrowhead-example.gif)
[!code-csharp[System.Drawing.UsingAPen#71](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingAPen/CS/Class1.cs#71)]
[!code-vb[System.Drawing.UsingAPen#71](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingAPen/VB/Class1.vb#71)]
## Compiling the Code
- Create a Windows Form and handle the form's <xref:System.Windows.Forms.Control.Paint> event. Paste the example code into the <xref:System.Windows.Forms.Control.Paint> event handler passing `e` as <xref:System.Windows.Forms.PaintEventArgs>.
## See also
- <xref:System.Drawing.Pen?displayProperty=nameWithType>
- <xref:System.Drawing.Drawing2D.LineCap?displayProperty=nameWithType>
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
@@ -0,0 +1,32 @@
---
title: "How to: Draw a Sequence of B&#233;zier Splines"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "splines [Windows Forms], drawing Bezier"
- "Bezier splines [Windows Forms], drawing sequence of"
ms.assetid: 37a0bedb-20c2-4cf0-91fa-a5509e826b30
---
# How to: Draw a Sequence of B&#233;zier Splines
You can use the <xref:System.Drawing.Graphics.DrawBeziers%2A> method of the <xref:System.Drawing.Graphics> class to draw a sequence of connected Bézier splines.
## Example
The following example draws a curve that consists of two connected Bézier splines. The endpoint of the first Bézier spline is the start point of the second Bézier spline.
The following illustration shows the connected splines along with the seven points:
![Graphic that shows the connected splines along with seven points.](./media/how-to-draw-a-sequence-of-bezier-splines/bezier-spline-seven-points.png)
[!code-csharp[System.Drawing.ConstructingDrawingCurves#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/CS/Class1.cs#11)]
[!code-vb[System.Drawing.ConstructingDrawingCurves#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/VB/Class1.vb#11)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Bézier Splines in GDI+](bezier-splines-in-gdi.md)
- [Constructing and Drawing Curves](constructing-and-drawing-curves.md)
@@ -0,0 +1,32 @@
---
title: "How to: Draw a Single B&#233;zier Spline"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "Bezier splines [Windows Forms], drawing"
- "drawing [Windows Forms], Bezier splines"
ms.assetid: f4f3fe30-f0a6-4743-ac91-11310cebea9f
---
# How to: Draw a Single B&#233;zier Spline
A Bézier spline is defined by four points: a start point, two control points, and an endpoint.
## Example
The following example draws a Bézier spline with start point (10, 100) and endpoint (200, 100). The control points are (100, 10) and (150, 150).
The following illustration shows the resulting Bézier spline along with its start point, control points, and endpoint. The illustration also shows the spline's convex hull, which is a polygon formed by connecting the four points with straight lines.
![Illustration of a Bezier Spline.](./media/how-to-draw-a-single-bezier-spline/bezier-spline-illustration.png)
[!code-csharp[System.Drawing.ConstructingDrawingCurves#31](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/CS/Class1.cs#31)]
[!code-vb[System.Drawing.ConstructingDrawingCurves#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/VB/Class1.vb#31)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- <xref:System.Drawing.Graphics.DrawBezier%2A>
- [Bézier Splines in GDI+](bezier-splines-in-gdi.md)
- [How to: Draw a Sequence of Bézier Splines](how-to-draw-a-sequence-of-bezier-splines.md)
@@ -0,0 +1,32 @@
---
title: "How to: Draw an Existing Bitmap to the Screen"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "bitmaps [Windows Forms], displaying in Windows Forms"
- "bitmaps [Windows Forms], loading in Windows Forms applications"
- "images [Windows Forms], displaying on Windows Forms"
ms.assetid: 5bc558d7-b326-4050-a834-b8600da0de95
---
# How to: Draw an Existing Bitmap to the Screen
You can easily draw an existing image on the screen. First you need to create a <xref:System.Drawing.Bitmap> object by using the bitmap constructor that takes a file name, <xref:System.Drawing.Bitmap.%23ctor%28System.String%29>. This constructor accepts images with several different file formats, including BMP, GIF, JPEG, PNG, and TIFF. After you have created the <xref:System.Drawing.Bitmap> object, pass that <xref:System.Drawing.Bitmap> object to the <xref:System.Drawing.Graphics.DrawImage%2A> method of a <xref:System.Drawing.Graphics> object.
## Example
This example creates a <xref:System.Drawing.Bitmap> object from a JPEG file and then draws the bitmap with its upper-left corner at (60, 10).
The following illustration shows the bitmap drawn at the specified location:
![Screenshot that shows an image at a specified position.](./media/how-to-draw-an-existing-bitmap-to-the-screen/bitmap-specified-position.png)
[!code-csharp[System.Drawing.WorkingWithImages#21](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/CS/Class1.cs#21)]
[!code-vb[System.Drawing.WorkingWithImages#21](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/VB/Class1.vb#21)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Working with Images, Bitmaps, Icons, and Metafiles](working-with-images-bitmaps-icons-and-metafiles.md)
@@ -0,0 +1,44 @@
---
title: "How to: Draw an Outlined Shape"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
f1_keywords:
- "Graphics.DrawEllipse"
helpviewer_keywords:
- "ellipses [Windows Forms], drawing"
- "circles [Windows Forms], drawing"
- "drawing [Windows Forms], shapes"
- "circular shapes"
- "forms [Windows Forms], drawing circular shapes"
- "circles"
- "outlined shapes [Windows Forms], examples"
- "outlined shapes [Windows Forms], drawing"
- "drawing [Windows Forms], circular shapes"
- "shapes [Windows Forms], drawing"
ms.assetid: f4f9214c-607e-407d-8cdd-6549f0278451
---
# How to: Draw an Outlined Shape
This example draws outlined ellipses and rectangles on a form.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#6](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#6)]
[!code-csharp[System.Drawing.ConceptualHowTos#6](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#6)]
[!code-vb[System.Drawing.ConceptualHowTos#6](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#6)]
## Compiling the Code
You cannot call this method in the <xref:System.Windows.Forms.Form.Load> event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the <xref:System.Windows.Forms.Control.OnPaint%2A> method.
## Robust Programming
You should always call <xref:System.IDisposable.Dispose%2A> on any objects that consume system resources, such as <xref:System.Drawing.Pen> and <xref:System.Drawing.Graphics> objects.
## See also
- <xref:System.Drawing.Graphics.DrawEllipse%2A>
- <xref:System.Windows.Forms.Control.OnPaint%2A>
- <xref:System.Drawing.Graphics.DrawRectangle%2A>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Using a Pen to Draw Lines and Shapes](using-a-pen-to-draw-lines-and-shapes.md)
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
@@ -0,0 +1,49 @@
---
title: "How to: Draw Cardinal Splines"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "cardinal splines [Windows Forms], drawing"
- "drawing [Windows Forms], cardinal splines"
- "graphics [Windows Forms], cardinal splines"
ms.assetid: a4a41e80-4461-4b47-b6bd-2c5e68881994
---
# How to: Draw Cardinal Splines
A cardinal spline is a curve that passes smoothly through a given set of points. To draw a cardinal spline, create a <xref:System.Drawing.Graphics> object and pass the address of an array of points to the <xref:System.Drawing.Graphics.DrawCurve%2A> method.
### Drawing a Bell-Shaped Cardinal Spline
- The following example draws a bell-shaped cardinal spline that passes through five designated points. The following illustration shows the curve and five points.
![Diagram that shows a bell-shaped cardinal spline.](./media/how-to-draw-cardinal-splines/bell-shaped-cardinal-spline.png)
[!code-csharp[System.Drawing.ConstructingDrawingCurves#21](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/CS/Class1.cs#21)]
[!code-vb[System.Drawing.ConstructingDrawingCurves#21](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/VB/Class1.vb#21)]
### Drawing a Closed Cardinal Spline
- Use the <xref:System.Drawing.Graphics.DrawClosedCurve%2A> method of the <xref:System.Drawing.Graphics> class to draw a closed cardinal spline. In a closed cardinal spline, the curve continues through the last point in the array and connects with the first point in the array. The following example draws a closed cardinal spline that passes through six designated points. The following illustration shows the closed spline along with the six points:
![Diagram that shows a closed cardinal spline.](./media/how-to-draw-cardinal-splines/closed-cardinal-spine.png)
[!code-csharp[System.Drawing.ConstructingDrawingCurves#22](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/CS/Class1.cs#22)]
[!code-vb[System.Drawing.ConstructingDrawingCurves#22](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/VB/Class1.vb#22)]
### Changing the Bend of a Cardinal Spline
- Change the way a cardinal spline bends by passing a tension argument to the <xref:System.Drawing.Graphics.DrawCurve%2A> method. The following example draws three cardinal splines that pass through the same set of points. The following illustration shows the three splines along with their tension values. Note that when the tension is 0, the points are connected by straight lines.
![Diagram that shows three cardinal splines.](./media/how-to-draw-cardinal-splines/three-cardinal-splines.png)
[!code-csharp[System.Drawing.ConstructingDrawingCurves#23](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/CS/Class1.cs#23)]
[!code-vb[System.Drawing.ConstructingDrawingCurves#23](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingCurves/VB/Class1.vb#23)]
## Compiling the Code
The preceding examples are designed for use with Windows Forms, and they require <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [Constructing and Drawing Curves](constructing-and-drawing-curves.md)
@@ -0,0 +1,36 @@
---
title: "How to: Draw Opaque and Semitransparent Lines"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "drawing [Windows Forms], lines"
- "transparency [Windows Forms], lines"
- "lines [Windows Forms], drawing alpha blended"
- "alpha blending [Windows Forms], drawing lines"
ms.assetid: 8f2508af-f495-4223-b5cc-646cbbb520eb
---
# How to: Draw Opaque and Semitransparent Lines
When you draw a line, you must pass a <xref:System.Drawing.Pen> object to the <xref:System.Drawing.Graphics.DrawLine%2A> method of the <xref:System.Drawing.Graphics> class. One of the parameters of the <xref:System.Drawing.Pen.%23ctor%2A> constructor is a <xref:System.Drawing.Color> object. To draw an opaque line, set the alpha component of the color to 255. To draw a semitransparent line, set the alpha component to any value from 1 through 254.
When you draw a semitransparent line over a background, the color of the line is blended with the colors of the background. The alpha component specifies how the line and background colors are mixed; alpha values near 0 place more weight on the background colors, and alpha values near 255 place more weight on the line color.
## Example
The following example draws a bitmap and then draws three lines that use the bitmap as a background. The first line uses an alpha component of 255, so it is opaque. The second and third lines use an alpha component of 128, so they are semitransparent; you can see the background image through the lines. The statement that sets the <xref:System.Drawing.Graphics.CompositingQuality%2A> property causes the blending for the third line to be done in conjunction with gamma correction.
[!code-csharp[System.Drawing.AlphaBlending#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlphaBlending/CS/Class1.cs#11)]
[!code-vb[System.Drawing.AlphaBlending#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlphaBlending/VB/Class1.vb#11)]
The following illustration shows the output of the following code:
![Illustration that shows opaque and semitransparent output](./media/how-to-draw-opaque-and-semitransparent-lines/opaque-semitransparent-lines.png)
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Alpha Blending Lines and Fills](alpha-blending-lines-and-fills.md)
- [How to: Give Your Control a Transparent Background](../controls/how-to-give-your-control-a-transparent-background.md)
- [How to: Draw with Opaque and Semitransparent Brushes](how-to-draw-with-opaque-and-semitransparent-brushes.md)
@@ -0,0 +1,47 @@
---
title: "How to: Draw Text at a Specified Location"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "text [Windows Forms], drawing at specified locations [Windows Forms]"
- "drawing text"
- "drawing text [Windows Forms], specified locations [Windows Forms]"
- "Windows Forms, drawing text at a specified location"
ms.assetid: 60816423-1c38-465e-980d-2c2b64d74086
---
# How to: Draw Text at a Specified Location
When you perform custom drawing, you can draw text in a single horizontal line starting at a specified point. You can draw text in this manner by using the <xref:System.Drawing.Graphics.DrawString%2A> overloaded method of the <xref:System.Drawing.Graphics> class that takes a <xref:System.Drawing.Point> or <xref:System.Drawing.PointF> parameter. The <xref:System.Drawing.Graphics.DrawString%2A> method also requires a <xref:System.Drawing.Brush> and <xref:System.Drawing.Font>
You can also use the <xref:System.Windows.Forms.TextRenderer.DrawText%2A> overloaded method of the <xref:System.Windows.Forms.TextRenderer> that takes a <xref:System.Drawing.Point>. <xref:System.Windows.Forms.TextRenderer.DrawText%2A> also requires a <xref:System.Drawing.Color> and a <xref:System.Drawing.Font>.
The following illustration shows the output of text drawn at a specified point when you use the <xref:System.Drawing.Graphics.DrawString%2A> overloaded method.
![Screenshot that shows the output of text at a specified point.](./media/how-to-draw-text-at-a-specified-location/font-text-specified-point.png)
### To draw a line of text with GDI+
1. Use the <xref:System.Drawing.Graphics.DrawString%2A> method, passing the text you want, <xref:System.Drawing.Point> or <xref:System.Drawing.PointF>, <xref:System.Drawing.Font>, and <xref:System.Drawing.Brush>.
[!code-csharp[System.Drawing.AlignDrawnText#30](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/CS/Form1.cs#30)]
[!code-vb[System.Drawing.AlignDrawnText#30](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/VB/Form1.vb#30)]
### To draw a line of text with GDI
1. Use the <xref:System.Windows.Forms.TextRenderer.DrawText%2A> method, passing the text you want, <xref:System.Drawing.Point>, <xref:System.Drawing.Font>, and <xref:System.Drawing.Color>.
[!code-csharp[System.Drawing.AlignDrawnText#40](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/CS/Form1.cs#40)]
[!code-vb[System.Drawing.AlignDrawnText#40](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/VB/Form1.vb#40)]
## Compiling the Code
The previous examples require:
- <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [How to: Draw Text with GDI](how-to-draw-text-with-gdi.md)
- [Using Fonts and Text](using-fonts-and-text.md)
- [How to: Construct Font Families and Fonts](how-to-construct-font-families-and-fonts.md)
- [How to: Draw Wrapped Text in a Rectangle](how-to-draw-wrapped-text-in-a-rectangle.md)
@@ -0,0 +1,38 @@
---
title: "How to: Draw Text on a Windows Form"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "forms [Windows Forms], drawing text"
- "text [Windows Forms], drawing"
ms.assetid: 5d2447a9-21a1-4adc-b954-5516f2bb9b2c
---
# How to: Draw Text on a Windows Form
The following code example shows how to use the <xref:System.Drawing.Graphics.DrawString%2A> method of the <xref:System.Drawing.Graphics> to draw text on a form. Alternatively, you can use <xref:System.Windows.Forms.TextRenderer> for drawing text on a form. For more information, see [How to: Draw Text with GDI](how-to-draw-text-with-gdi.md).
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#7](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#7)]
[!code-csharp[System.Drawing.ConceptualHowTos#7](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#7)]
[!code-vb[System.Drawing.ConceptualHowTos#7](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#7)]
## Compiling the Code
You cannot call the <xref:System.Drawing.Graphics.DrawString%2A> method in the <xref:System.Windows.Forms.Form.Load> event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the <xref:System.Windows.Forms.Control.OnPaint%2A> method.
## Robust Programming
The following conditions may cause an exception:
- The Arial font is not installed.
## See also
- <xref:System.Drawing.Graphics.DrawString%2A>
- <xref:System.Windows.Forms.TextRenderer.DrawText%2A>
- <xref:System.Drawing.StringFormat.FormatFlags%2A>
- <xref:System.Drawing.StringFormatFlags>
- <xref:System.Windows.Forms.TextFormatFlags>
- <xref:System.Windows.Forms.Control.OnPaint%2A>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [How to: Draw Text with GDI](how-to-draw-text-with-gdi.md)
@@ -0,0 +1,38 @@
---
title: "How to: Draw Text with GDI"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "GDI [Windows Forms], drawing text [Windows Forms]"
- "text [Windows Forms], drawing with TextRenderer"
- "drawing [Windows Forms], text"
- "Windows Forms, drawing text with GDI"
ms.assetid: 2a19fe5d-2ace-451c-94db-01cb1118ef7b
---
# How to: Draw Text with GDI
With the <xref:System.Windows.Forms.TextRenderer.DrawText%2A> method in the <xref:System.Windows.Forms.TextRenderer> class, you can access GDI functionality for drawing text on a form or control. GDI text rendering typically offers better performance and more accurate text measuring than GDI+.
> [!NOTE]
> The <xref:System.Windows.Forms.TextRenderer.DrawText%2A> methods of the <xref:System.Windows.Forms.TextRenderer> class are not supported for printing. When printing, always use the <xref:System.Drawing.Graphics.DrawString%2A> methods of the <xref:System.Drawing.Graphics> class.
## Example
The following code example demonstrates how to draw text on multiple lines within a rectangle using the <xref:System.Windows.Forms.TextRenderer.DrawText%2A> method.
[!code-csharp[System.Windows.Forms.TextRendererExamples#7](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Windows.Forms.TextRendererExamples/CS/Form1.cs#7)]
[!code-vb[System.Windows.Forms.TextRendererExamples#7](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Windows.Forms.TextRendererExamples/VB/Form1.vb#7)]
To render text with the <xref:System.Windows.Forms.TextRenderer> class, you need an <xref:System.Drawing.IDeviceContext>, such as a <xref:System.Drawing.Graphics> and a <xref:System.Drawing.Font>, a location to draw the text, and the color in which it should be drawn. Optionally, you can specify the text formatting by using the <xref:System.Windows.Forms.TextFormatFlags> enumeration.
For more information about obtaining a <xref:System.Drawing.Graphics>, see [How to: Create Graphics Objects for Drawing](how-to-create-graphics-objects-for-drawing.md). For more information about constructing a <xref:System.Drawing.Font>, see [How to: Construct Font Families and Fonts](how-to-construct-font-families-and-fonts.md).
## Compiling the Code
The preceding code example is designed for use with Windows Forms, and it requires the <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- <xref:System.Windows.Forms.TextRenderer>
- <xref:System.Drawing.Font>
- <xref:System.Drawing.Color>
- [Using Fonts and Text](using-fonts-and-text.md)
@@ -0,0 +1,41 @@
---
title: "How to: Draw Vertical Text on a Windows Form"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
- "cpp"
f1_keywords:
- "StringFormat.FormatFlags"
- "Graphics.DrawString"
helpviewer_keywords:
- "text [Windows Forms], drawing vertical"
- "strings [Windows Forms], drawing vertical"
- "text [Windows Forms], drawing"
- "text [Windows Forms], vertical text"
ms.assetid: 717a6131-00f6-4373-b574-9894e8317799
---
# How to: Draw Vertical Text on a Windows Form
The following code example shows how to draw vertical text on a form by using the <xref:System.Drawing.Graphics.DrawString%2A> method of <xref:System.Drawing.Graphics>.
## Example
[!code-cpp[System.Drawing.ConceptualHowTos#8](~/samples/snippets/cpp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/cpp/form1.cpp#8)]
[!code-csharp[System.Drawing.ConceptualHowTos#8](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/CS/form1.cs#8)]
[!code-vb[System.Drawing.ConceptualHowTos#8](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConceptualHowTos/VB/form1.vb#8)]
## Compiling the Code
You cannot call this method in the <xref:System.Windows.Forms.Form.Load> event handler. The drawn content will not be redrawn if the form is resized or obscured by another form. To make your content automatically repaint, you should override the <xref:System.Windows.Forms.Control.OnPaint%2A> method.
## Robust Programming
The following conditions may cause an exception:
- The Arial font is not installed.
## See also
- <xref:System.Drawing.Graphics.DrawString%2A>
- <xref:System.Drawing.StringFormat.FormatFlags%2A>
- <xref:System.Drawing.StringFormatFlags>
- <xref:System.Windows.Forms.Control.OnPaint%2A>
- [Getting Started with Graphics Programming](getting-started-with-graphics-programming.md)
- [Using Fonts and Text](using-fonts-and-text.md)
@@ -0,0 +1,37 @@
---
title: "How to: Draw with Opaque and Semitransparent Brushes"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "semi-transparent shapes [Windows Forms], drawing"
- "transparency [Windows Forms], semi-transparent shapes"
- "alpha blending [Windows Forms], brush"
- "brushes [Windows Forms], using semi-transparent"
ms.assetid: a4f6f6b8-3bc8-440a-84af-d62ef0f8ff40
---
# How to: Draw with Opaque and Semitransparent Brushes
When you fill a shape, you must pass a <xref:System.Drawing.Brush> object to one of the fill methods of the <xref:System.Drawing.Graphics> class. The one parameter of the <xref:System.Drawing.SolidBrush.%23ctor%2A> constructor is a <xref:System.Drawing.Color> object. To fill an opaque shape, set the alpha component of the color to 255. To fill a semitransparent shape, set the alpha component to any value from 1 through 254.
When you fill a semitransparent shape, the color of the shape is blended with the colors of the background. The alpha component specifies how the shape and background colors are mixed; alpha values near 0 place more weight on the background colors, and alpha values near 255 place more weight on the shape color.
## Example
The following example draws a bitmap and then fills three ellipses that overlap the bitmap. The first ellipse uses an alpha component of 255, so it is opaque. The second and third ellipses use an alpha component of 128, so they are semitransparent; you can see the background image through the ellipses. The call that sets the <xref:System.Drawing.Graphics.CompositingQuality%2A> property causes the blending for the third ellipse to be done in conjunction with gamma correction.
[!code-csharp[System.Drawing.AlphaBlending#31](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlphaBlending/CS/Class1.cs#31)]
[!code-vb[System.Drawing.AlphaBlending#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlphaBlending/VB/Class1.vb#31)]
The following illustration shows the output of the following code:
![Illustration that shows opaque and semitransparent output.](./media/how-to-draw-with-opaque-and-semitransparent-brushes/compositingquality-ellipse-semitransparent.png)
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [Graphics and Drawing in Windows Forms](graphics-and-drawing-in-windows-forms.md)
- [Alpha Blending Lines and Fills](alpha-blending-lines-and-fills.md)
- [How to: Give Your Control a Transparent Background](../controls/how-to-give-your-control-a-transparent-background.md)
- [How to: Draw Opaque and Semitransparent Lines](how-to-draw-opaque-and-semitransparent-lines.md)
@@ -0,0 +1,46 @@
---
title: "How to: Draw Wrapped Text in a Rectangle"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "Windows Forms, drawing text in a rectangle"
- "text [Windows Forms], drawing in a rectangle"
- "strings [Windows Forms], drawing in a rectangle"
ms.assetid: e1fb432a-dc90-48b5-9b6b-acc14507133d
---
# How to: Draw Wrapped Text in a Rectangle
You can draw wrapped text in a rectangle by using the <xref:System.Drawing.Graphics.DrawString%2A> overloaded method of the <xref:System.Drawing.Graphics> class that takes a <xref:System.Drawing.Rectangle> or <xref:System.Drawing.RectangleF> parameter. You will also use a <xref:System.Drawing.Brush> and a <xref:System.Drawing.Font>.
You can also draw wrapped text in a rectangle by using the <xref:System.Windows.Forms.TextRenderer.DrawText%2A> overloaded method of the <xref:System.Windows.Forms.TextRenderer> that takes a <xref:System.Drawing.Rectangle> and a <xref:System.Windows.Forms.TextFormatFlags> parameter. You will also use a <xref:System.Drawing.Color> and a <xref:System.Drawing.Font>.
The following illustration shows the output of text drawn in the rectangle when you use the <xref:System.Drawing.Graphics.DrawString%2A> method:
![Screenshot that shows the output when using DrawString method.](./media/how-to-draw-wrapped-text-in-a-rectangle/drawstring-method-font-text.png)
### To draw wrapped text in a rectangle with GDI+
1. Use the <xref:System.Drawing.Graphics.DrawString%2A> overloaded method, passing the text you want, <xref:System.Drawing.Rectangle> or <xref:System.Drawing.RectangleF>, <xref:System.Drawing.Font> and <xref:System.Drawing.Brush>.
[!code-csharp[System.Drawing.AlignDrawnText#50](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/CS/Form1.cs#50)]
[!code-vb[System.Drawing.AlignDrawnText#50](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/VB/Form1.vb#50)]
### To draw wrapped text in a rectangle with GDI
1. Use the <xref:System.Windows.Forms.TextFormatFlags> enumeration value to specify the text should be wrapped with the <xref:System.Windows.Forms.TextRenderer.DrawText%2A> overloaded method, passing the text you want, <xref:System.Drawing.Rectangle>, <xref:System.Drawing.Font> and <xref:System.Drawing.Color>.
[!code-csharp[System.Drawing.AlignDrawnText#60](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/CS/Form1.cs#60)]
[!code-vb[System.Drawing.AlignDrawnText#60](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.AlignDrawnText/VB/Form1.vb#60)]
## Compiling the Code
The previous examples require:
- <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>.
## See also
- [How to: Draw Text with GDI](how-to-draw-text-with-gdi.md)
- [Using Fonts and Text](using-fonts-and-text.md)
- [How to: Construct Font Families and Fonts](how-to-construct-font-families-and-fonts.md)
- [How to: Draw Text at a Specified Location](how-to-draw-text-at-a-specified-location.md)
@@ -0,0 +1,30 @@
---
title: "How to: Enumerate Installed Fonts"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "fonts [Windows Forms], enumerating installed"
- "examples [Windows Forms], fonts"
ms.assetid: 26d74ef5-0f39-4eeb-8d20-00e66e014abe
---
# How to: Enumerate Installed Fonts
The <xref:System.Drawing.Text.InstalledFontCollection> class inherits from the <xref:System.Drawing.Text.FontCollection> abstract base class. You can use an <xref:System.Drawing.Text.InstalledFontCollection> object to enumerate the fonts installed on the computer. The <xref:System.Drawing.Text.FontCollection.Families%2A> property of an <xref:System.Drawing.Text.InstalledFontCollection> object is an array of <xref:System.Drawing.FontFamily> objects.
## Example
The following example lists the names of all the font families installed on the computer. The code retrieves the <xref:System.Drawing.FontFamily.Name%2A> property of each <xref:System.Drawing.FontFamily> object in the array returned by the <xref:System.Drawing.Text.FontCollection.Families%2A> property. As the family names are retrieved, they are concatenated to form a comma-separated list. Then the <xref:System.Drawing.Graphics.DrawString%2A> method of the <xref:System.Drawing.Graphics> class draws the comma-separated list in a rectangle.
If you run the example code, the output will be similar to that shown in the following illustration:
![Screenshot that shows the installed font families.](./media/how-to-enumerate-installed-fonts/list-installed-font-families.png)
[!code-csharp[System.Drawing.FontsAndText#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.FontsAndText/CS/Class1.cs#11)]
[!code-vb[System.Drawing.FontsAndText#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.FontsAndText/VB/Class1.vb#11)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of <xref:System.Windows.Forms.PaintEventHandler>. In addition, you should import the <xref:System.Drawing.Text> namespace.
## See also
- [Using Fonts and Text](using-fonts-and-text.md)
@@ -0,0 +1,32 @@
---
title: "How to: Extract the Icon Associated with a File"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "displaying a file name and its file type icon in a ListView control [Windows Forms]"
- "file name extension icons [Windows Forms], displaying in a ListView"
- "extracting icons associated with a file type [Windows Forms]"
ms.assetid: 88e2ad8b-c34f-415a-84f2-dad756b5c928
---
# How to: Extract the Icon Associated with a File in Windows Forms
Many files have embedded icons that provide a visual representation of the associated file type. For example, Microsoft Word documents contain an icon that identifies them as Word documents. When displaying files in a list control or table control, you may want to display the icon representing the file type next to each file name. You can do this easily by using the <xref:System.Drawing.Icon.ExtractAssociatedIcon%2A> method.
## Example
The following code example demonstrates how to extract the icon associated with a file and display the file name and its associated icon in a <xref:System.Windows.Forms.ListView> control.
[!code-csharp[System.Drawing.Icon.ExtractAssociatedIconEx#1](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.Icon.ExtractAssociatedIconEx/CS/Form1.cs#1)]
[!code-vb[System.Drawing.Icon.ExtractAssociatedIconEx#1](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.Icon.ExtractAssociatedIconEx/VB/Form1.vb#1)]
## Compiling the Code
To compile the example:
- Paste the preceding code into a Windows Form, and call the `ExtractAssociatedIconExample` method from the form's constructor or <xref:System.Windows.Forms.Form.Load> event-handling method.
You will need to make sure that your form imports the <xref:System.IO> namespace.
## See also
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
- [ListView Control](../controls/listview-control-windows-forms.md)
@@ -0,0 +1,43 @@
---
title: "How to: Fill a Shape with a Hatch Pattern"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "patterns [Windows Forms], adding to shapes"
- "shapes [Windows Forms], filling with patterns"
- "brushes [Windows Forms], using hatch brushes"
ms.assetid: 9c8300ff-187b-404f-af1f-ebd499f5b16f
---
# How to: Fill a Shape with a Hatch Pattern
A hatch pattern is made from two colors: one for the background and one for the lines that form the pattern over the background. To fill a closed shape with a hatch pattern, use a <xref:System.Drawing.Drawing2D.HatchBrush> object. The following example demonstrates how to fill an ellipse with a hatch pattern:
## Example
The <xref:System.Drawing.Drawing2D.HatchBrush.%23ctor%2A> constructor takes three arguments: the hatch style, the color of the hatch line, and the color of the background. The hatch style argument can be any value from the <xref:System.Drawing.Drawing2D.HatchStyle> enumeration. There are more than fifty elements in the <xref:System.Drawing.Drawing2D.HatchStyle> enumeration; a few of those elements are shown in the following list:
- <xref:System.Drawing.Drawing2D.HatchStyle.Horizontal>
- <xref:System.Drawing.Drawing2D.HatchStyle.Vertical>
- <xref:System.Drawing.Drawing2D.HatchStyle.ForwardDiagonal>
- <xref:System.Drawing.Drawing2D.HatchStyle.BackwardDiagonal>
- <xref:System.Drawing.Drawing2D.HatchStyle.Cross>
- <xref:System.Drawing.Drawing2D.HatchStyle.DiagonalCross>
The following illustration shows the filled ellipse.
![Screenshot of what an ellipse filled with a hatch pattern looks like.](./media/how-to-fill-a-shape-with-a-hatch-pattern/ellipse-filled-hatch.png "hatch1")
[!code-csharp[System.Drawing.UsingABrush#41](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingABrush/CS/Class1.cs#41)]
[!code-vb[System.Drawing.UsingABrush#41](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingABrush/VB/Class1.vb#41)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs>`e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Using a Brush to Fill Shapes](using-a-brush-to-fill-shapes.md)
@@ -0,0 +1,28 @@
---
title: "How to: Fill a Shape with a Solid Color"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "colors [Windows Forms], adding to shapes"
- "shapes [Windows Forms], filling"
ms.assetid: 06088b31-bac9-4ef3-9ebe-06c2c764d6df
---
# How to: Fill a Shape with a Solid Color
To fill a shape with a solid color, create a <xref:System.Drawing.SolidBrush> object, and then pass that <xref:System.Drawing.SolidBrush> object as an argument to one of the fill methods of the <xref:System.Drawing.Graphics> class. The following example shows how to fill an ellipse with the color red.
## Example
In the following code, the <xref:System.Drawing.SolidBrush.%23ctor%2A> constructor takes a <xref:System.Drawing.Color> object as its only argument. The values used by the <xref:System.Drawing.Color.FromArgb%2A> method represent the alpha, red, green, and blue components of the color. Each of these values must be in the range 0 through 255. The first 255 indicates that the color is fully opaque, and the second 255 indicates that the red component is at full intensity. The two zeros indicate that the green and blue components both have an intensity of 0.
The four numbers (0, 0, 100, 60) passed to the <xref:System.Drawing.Graphics.FillEllipse%2A> method specify the location and size of the bounding rectangle for the ellipse. The rectangle has an upper-left corner of (0, 0), a width of 100, and a height of 60.
[!code-csharp[System.Drawing.UsingABrush#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingABrush/CS/Class1.cs#11)]
[!code-vb[System.Drawing.UsingABrush#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingABrush/VB/Class1.vb#11)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Using a Brush to Fill Shapes](using-a-brush-to-fill-shapes.md)
@@ -0,0 +1,32 @@
---
title: "How to: Fill a Shape with an Image Texture"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "images [Windows Forms], using with brushes"
- "bitmaps [Windows Forms], using texture"
- "shapes [Windows Forms], filling with images"
ms.assetid: 508da5a6-2433-4d2b-9680-eaeae4e96e3b
---
# How to: Fill a Shape with an Image Texture
You can fill a closed shape with a texture by using the <xref:System.Drawing.Image> class and the <xref:System.Drawing.TextureBrush> class.
## Example
The following example fills an ellipse with an image. The code constructs an <xref:System.Drawing.Image> object, and then passes the address of that <xref:System.Drawing.Image> object as an argument to a <xref:System.Drawing.TextureBrush.%23ctor%2A> constructor. The third statement scales the image, and the fourth statement fills the ellipse with repeated copies of the scaled image.
In the following code, the <xref:System.Drawing.TextureBrush.Transform%2A> property contains the transformation that is applied to the image before it is drawn. Assume that the original image has a width of 640 pixels and a height of 480 pixels. The transform shrinks the image to 75×75 by setting the horizontal and vertical scaling values.
> [!NOTE]
> In the following example, the image size is 75×75, and the ellipse size is 150×250. Because the image is smaller than the ellipse it is filling, the ellipse is tiled with the image. Tiling means that the image is repeated horizontally and vertically until the boundary of the shape is reached. For more information about tiling, see [How to: Tile a Shape with an Image](how-to-tile-a-shape-with-an-image.md).
[!code-csharp[System.Drawing.UsingABrush#21](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.UsingABrush/CS/Class1.cs#21)]
[!code-vb[System.Drawing.UsingABrush#21](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.UsingABrush/VB/Class1.vb#21)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- [Using a Brush to Fill Shapes](using-a-brush-to-fill-shapes.md)
@@ -0,0 +1,31 @@
---
title: "How to: Fill Open Figures"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "open figures [Windows Forms], filling"
- "figures [Windows Forms], filling"
ms.assetid: 5a36b0e4-f1f4-46c0-a85a-22ae98491950
---
# How to: Fill Open Figures
You can fill a path by passing a <xref:System.Drawing.Drawing2D.GraphicsPath> object to the <xref:System.Drawing.Graphics.FillPath%2A> method. The <xref:System.Drawing.Graphics.FillPath%2A> method fills the path according to the fill mode (alternate or winding) currently set for the path. If the path has any open figures, the path is filled as if those figures were closed. GDI+ closes a figure by drawing a straight line from its ending point to its starting point.
## Example
The following example creates a path that has one open figure (an arc) and one closed figure (an ellipse). The <xref:System.Drawing.Graphics.FillPath%2A> method fills the path according to the default fill mode, which is <xref:System.Drawing.Drawing2D.FillMode.Alternate>.
The following illustration shows the output of the example code. Note that the path is filled (according to <xref:System.Drawing.Drawing2D.FillMode.Alternate>) as if the open figure were closed by a straight line from its ending point to its starting point.
![Diagram that shows the output of the FillPath method](./media/how-to-fill-open-figures/fill-path-alternate-mode.png)
[!code-csharp[System.Drawing.ConstructingDrawingPaths#11](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingPaths/CS/Class1.cs#11)]
[!code-vb[System.Drawing.ConstructingDrawingPaths#11](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.ConstructingDrawingPaths/VB/Class1.vb#11)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler.
## See also
- <xref:System.Drawing.Drawing2D.GraphicsPath>
- [Graphics Paths in GDI+](graphics-paths-in-gdi.md)
@@ -0,0 +1,25 @@
---
title: "How to: Flatten a Curved Path into a Line"
ms.date: "03/30/2017"
helpviewer_keywords:
- "graphics [Windows Forms], flattening curves into lines"
- "curves [Windows Forms], flattening"
- "GraphicsPath object"
- "paths [Windows Forms], flattening"
- "drawing [Windows Forms], flattening curves"
ms.assetid: e654b8de-25f4-4735-9208-42e4514a589c
---
# How to: Flatten a Curved Path into a Line
A <xref:System.Drawing.Drawing2D.GraphicsPath> object stores a sequence of lines and Bézier splines. You can add several types of curves (ellipses, arcs, cardinal splines) to a path, but each curve is converted to a Bézier spline before it is stored in the path. Flattening a path consists of converting each Bézier spline in the path to a sequence of straight lines. The following illustration shows a path before and after flattening.
![Straight Lines and Curves](./media/aboutgdip02-art32a.gif "AboutGdip02_Art32A")
### To Flatten a Path
- call the <xref:System.Drawing.Drawing2D.GraphicsPath.Flatten%2A> method of a <xref:System.Drawing.Drawing2D.GraphicsPath> object. The <xref:System.Drawing.Drawing2D.GraphicsPath.Flatten%2A> method receives a flatness argument that specifies the maximum distance between the flattened path and the original path.
## See also
- <xref:System.Drawing.Drawing2D.GraphicsPath?displayProperty=nameWithType>
- [Lines, Curves, and Shapes](lines-curves-and-shapes.md)
- [Constructing and Drawing Paths](constructing-and-drawing-paths.md)
@@ -0,0 +1,40 @@
---
title: "How to: Improve Performance by Avoiding Automatic Scaling"
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "automatic scaling"
- "images [Windows Forms], improving performance"
- "images [Windows Forms], using without automatic scaling"
- "performance [Windows Forms], improving image"
ms.assetid: 5fe2c95d-8653-4d55-bf0d-e5afa28f223b
---
# How to: Improve Performance by Avoiding Automatic Scaling
GDI+ may automatically scale an image as you draw it, which would decrease performance. Alternatively, you can control the scaling of the image by passing the dimensions of the destination rectangle to the <xref:System.Drawing.Graphics.DrawImage%2A> method.
For example, the following call to the <xref:System.Drawing.Graphics.DrawImage%2A> method specifies an upper-left corner of (50, 30) but does not specify a destination rectangle.
[!code-csharp[System.Drawing.WorkingWithImages#31](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/CS/Class1.cs#31)]
[!code-vb[System.Drawing.WorkingWithImages#31](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/VB/Class1.vb#31)]
Although this is the easiest version of the <xref:System.Drawing.Graphics.DrawImage%2A> method in terms of the number of required arguments, it is not necessarily the most efficient. If the resolution used by GDI+ (usually 96 dots per inch) is different from the resolution stored in the <xref:System.Drawing.Image> object, then the <xref:System.Drawing.Graphics.DrawImage%2A> method will scale the image. For example, suppose an <xref:System.Drawing.Image> object has a width of 216 pixels and a stored horizontal resolution value of 72 dots per inch. Because 216/72 is 3, <xref:System.Drawing.Graphics.DrawImage%2A> will scale the image so that it has a width of 3 inches at a resolution of 96 dots per inch. That is, <xref:System.Drawing.Graphics.DrawImage%2A> will display an image that has a width of 96x3 = 288 pixels.
Even if your screen resolution is different from 96 dots per inch, GDI+ will probably scale the image as if the screen resolution were 96 dots per inch. That is because a GDI+ <xref:System.Drawing.Graphics> object is associated with a device context, and when GDI+ queries the device context for the screen resolution, the result is usually 96, regardless of the actual screen resolution. You can avoid automatic scaling by specifying the destination rectangle in the <xref:System.Drawing.Graphics.DrawImage%2A> method.
## Example
The following example draws the same image twice. In the first case, the width and height of the destination rectangle are not specified, and the image is automatically scaled. In the second case, the width and height (measured in pixels) of the destination rectangle are specified to be the same as the width and height of the original image. The following illustration shows the image rendered twice:
![Screenshot that shows images with scaled texture.](./media/how-to-improve-performance-by-avoiding-automatic-scaling/two-scaled-texture-images.png)
[!code-csharp[System.Drawing.WorkingWithImages#32](~/samples/snippets/csharp/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/CS/Class1.cs#32)]
[!code-vb[System.Drawing.WorkingWithImages#32](~/samples/snippets/visualbasic/VS_Snippets_Winforms/System.Drawing.WorkingWithImages/VB/Class1.vb#32)]
## Compiling the Code
The preceding example is designed for use with Windows Forms, and it requires <xref:System.Windows.Forms.PaintEventArgs> `e`, which is a parameter of the <xref:System.Windows.Forms.Control.Paint> event handler. Replace Texture.jpg with an image name and path that are valid on your system.
## See also
- [Images, Bitmaps, and Metafiles](images-bitmaps-and-metafiles.md)
- [Working with Images, Bitmaps, Icons, and Metafiles](working-with-images-bitmaps-icons-and-metafiles.md)
@@ -0,0 +1,57 @@
---
title: "How to: Inherit Forms Using the Inheritance Picker Dialog Box"
ms.date: "03/30/2017"
helpviewer_keywords:
- "inheritance [Windows Forms], forms"
- "Inheritance Picker dialog box"
- "inherited forms [Windows Forms], creating"
ms.assetid: 969b4c04-12aa-4297-93a2-0ae747447823
---
# How to: Inherit Forms Using the Inheritance Picker
The easiest way to inherit a form or other object is to use the **Inheritance Picker** dialog box. With it, you can take advantage of code or user interfaces (UI) you have already created in other solutions.
> [!NOTE]
> In order to inherit from a form with the **Inheritance Picker** dialog box, the project containing that form must have been built into an executable file or DLL. To build the project, choose **Build Solution** from the **Build** menu.
## Create a Windows Form by using the Inheritance Picker
1. In Visual Studio, from the **Project** menu, choose **Add Windows Form**.
The **Add New Item** dialog box opens.
2. Search the **Inherited Form** template either from the searchbox or by clicking on the **Windows Forms** category, select it, and name it in the **Name** box. Click the **Add** button to proceed.
The **Inheritance Picker** dialog box opens. If the current project already contains forms, they are displayed in the **Inheritance Picker** dialog box.
3. To inherit from a form in another assembly, click the **Browse** button.
4. Within the **Select a file which contains a component to inherit from** dialog box, navigate to the project containing the form or module you desire.
5. Click the name of the .exe or .dll file to select it and click the **Open** button.
This returns you to the **Inheritance Picker** dialog box, where the component is now listed, along with the project in which it is located.
6. Select the component.
In **Solution Explorer**, the component is added to your project. If it has a UI, controls that are part of the inherited form will be marked with a glyph (![Screenshot of the Visual Basic inheritance symbol.](./media/how-to-inherit-forms-using-the-inheritance-picker-dialog-box/visual-basic-inheritance-glyph.gif)), and, when selected, have a border indicating the level of security that the control has on the superclassed form. The behaviors that correspond to the different security levels are listed in the table below.
|Security level of control|Available interaction through Designer and Code Editor with Inherited Form|
|-------------------------------|--------------------------------------------------------------------------------|
|Public|Standard border with sizing handles: control may be sized and moved. The control can be accessed internally by the class which declares it and externally by other classes.|
|Protected|Standard border with sizing handles: control may be sized and moved. Can be accessed internally by the class that declares it and any class that inherits from the parent class, but cannot be accessed by external classes.|
|Protected Internal (Protected Friend in Visual Basic)|Standard border with sizing handles: control may be sized and moved. Can be accessed internally by the class that declares it, by any class that inherits from the parent class, and by other members of the assembly that contains it.|
|Internal (Friend in Visual Basic)|Standard border with no sizing handles, shown on the form, properties visible in **Properties** window. However, all aspects of the control will be considered read-only. You cannot move or size the control, or change its properties. If the control is a container of other controls, like a group box, new controls cannot be added and existing controls cannot be removed, even if those controls were public. The control can only be accessed by other members of the assembly that contains it.|
|Private|Standard border with no sizing handles, shown on the form, properties visible in **Properties** window. However, all aspects of the control will be considered read-only. You cannot move or size the control, or change its properties. If the control is a container of other controls, like a group box, new controls cannot be added and existing controls cannot be removed, even if those controls were public. The control can only be accessed by the class that declares it.|
For information about how to alter a base form's appearance, see [Effects of Modifying a Base Form's Appearance](effects-of-modifying-base-form-appearance.md).
> [!NOTE]
> When you combine inherited controls and components with standard controls and components on Windows Forms, you might encounter conflicts with the z-ordering. You can correct this by modifying the z-order, which is done by clicking in the **Format** menu, pointing to **Order**, and then clicking **Bring To Front** or **Send To Back**. For more information about the z-order of controls, see [How to: Layer Objects on Windows Forms](../controls/how-to-layer-objects-on-windows-forms.md).
## See also
- [Inherits Statement](https://docs.microsoft.com/dotnet/visual-basic/language-reference/statements/inherits-statement)
- [using](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/using)
- [Effects of Modifying a Base Form's Appearance](effects-of-modifying-base-form-appearance.md)
- [Windows Forms Visual Inheritance](windows-forms-visual-inheritance.md)
@@ -0,0 +1,45 @@
---
title: Form inheritance
ms.date: "03/30/2017"
dev_langs:
- "csharp"
- "vb"
helpviewer_keywords:
- "inherited forms [Windows Forms], creating at run-time"
- "inheritance [Windows Forms], forms"
- "Windows Forms, inheritance"
ms.assetid: cb3e1c0f-3d2a-4cdc-b0d1-c92eae567ffb
---
# How to: Inherit Windows Forms
Creating new Windows Forms by inheriting from base forms is a handy way to duplicate your best efforts without going through the process of entirely recreating a form every time you require it.
For more information about inheriting forms at design time using the **Inheritance Picker** dialog box and how to visually distinguish between security levels of inherited controls, see [How to: Inherit Forms Using the Inheritance Picker Dialog Box](how-to-inherit-forms-using-the-inheritance-picker-dialog-box.md).
> [!NOTE]
> In order to inherit from a form, the file or namespace containing that form must have been built into an executable file or DLL. To build the project, choose **Build** from the **Build** menu. Also, a reference to the namespace must be added to the class inheriting the form.
## Inherit a form programmatically
1. In your class, add a reference to the namespace containing the form you wish to inherit from.
2. In the class definition, add a reference to the form to inherit from. The reference should include the namespace that contains the form, followed by a period, then the name of the base form itself.
```vb
Public Class Form2
Inherits Namespace1.Form1
```
```csharp
public class Form2 : Namespace1.Form1
```
When inheriting forms, keep in mind that issues may arise with regard to event handlers being called twice, because each event is being handled by both the base class and the inherited class. For more information on how to avoid this problem, see [Troubleshooting Inherited Event Handlers in Visual Basic](https://docs.microsoft.com/dotnet/visual-basic/programming-guide/language-features/events/troubleshooting-inherited-event-handlers).
## See also
- [Inherits Statement](https://docs.microsoft.com/dotnet/visual-basic/language-reference/statements/inherits-statement)
- [Imports Statement (.NET Namespace and Type)](https://docs.microsoft.com/dotnet/visual-basic/language-reference/statements/imports-statement-net-namespace-and-type)
- [using](https://docs.microsoft.com/dotnet/csharp/language-reference/keywords/using)
- [Effects of Modifying a Base Form's Appearance](effects-of-modifying-base-form-appearance.md)
- [Windows Forms Visual Inheritance](windows-forms-visual-inheritance.md)

Some files were not shown because too many files have changed in this diff Show More