Skip to content

Commit

Permalink
Merge pull request #1852 from dotnet/main
Browse files Browse the repository at this point in the history
Merge main into live
  • Loading branch information
adegeo authored Jun 14, 2024
2 parents d4a4994 + ec78e78 commit 7b8d713
Show file tree
Hide file tree
Showing 37 changed files with 1,053 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "How to: Give Your Control a Transparent Background"
description: Learn how to give your control a transparent background using the properties window at design time.
ms.date: "03/30/2017"
ms.date: 06/12/2024
helpviewer_keywords:
- "transparent backgrounds [Windows Forms], custom controls"
- "custom controls [Windows Forms], transparent background"
Expand All @@ -10,18 +10,19 @@ ms.assetid: 32433e63-f4e9-4305-9857-6de3edeb944a
---
# How to: Give Your Control a Transparent Background

In earlier versions of the .NET Framework, controls didn't support setting transparent backcolors without first setting the <xref:System.Windows.Forms.Control.SetStyle%2A> method in the forms's constructor. In the current framework version, the backcolor for most controls can be set to <xref:System.Drawing.Color.Transparent%2A> in the **Properties** window at design time, or in code in the form's constructor.

> [!NOTE]
> Windows Forms controls do not support true transparency. The background of a transparent Windows Forms control is painted by its parent.
The background color for most controls can be set to <xref:System.Drawing.Color.Transparent%2A> in the **Properties** window at design time, or in code in the form's constructor.

Windows Forms controls don't support _true transparency_. Controls are drawn to the screen in two parts. First, the background is painted, followed by the control's appearance. While you make a control "transparent" by setting the <xref:System.Windows.Forms.ButtonBase.BackColor%2A> to <xref:System.Drawing.Color.Transparent%2A>, this actually passes the background painting to the parent control. If the parent control supports the `BackgroundImage` property, and the property is set, then this image is drawn as the background of the control. If the property isn't supported or isn't set, the `BackColor` of the parent is used to draw the background of the control.

A better way to think of control "transparency" is to think of it as inheriting the **background** paint operation of the parent. You can't see other controls under a "transparent" control.

> [!NOTE]
> The <xref:System.Windows.Controls.Button> control doesn't support a transparent backcolor even when the <xref:System.Windows.Forms.ButtonBase.BackColor%2A> property is set to <xref:System.Drawing.Color.Transparent%2A>.
### To give your control a transparent backcolor
- In the Properties window, choose the <xref:System.Windows.Forms.ButtonBase.BackColor%2A> property and set it to <xref:System.Drawing.Color.Transparent%2A>
> The <xref:System.Windows.Controls.Button> control allows you to set the `BackColor` to `Transparent`, but it has no effect on the control.
## To give your control a transparent background

- In the Properties window, choose the <xref:System.Windows.Forms.ButtonBase.BackColor%2A> property and set it to <xref:System.Drawing.Color.Transparent%2A>

## See also

- <xref:System.Drawing.Color.FromArgb%2A>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "PictureBox Control Overview"
description: This article provides an overview of the PictureBox control in Windows Forms, which is used to display graphics in bitmap, GIF, JPEG, metafile, or icon format.
ms.date: "03/30/2017"
ms.date: 06/12/2024
f1_keywords:
- "PictureBox"
helpviewer_keywords:
Expand All @@ -17,7 +17,19 @@ The Windows Forms <xref:System.Windows.Forms.PictureBox> control is used to disp
## Key Properties and Methods

The picture that is displayed is determined by the <xref:System.Windows.Forms.PictureBox.Image%2A> property, which can be set at run time or at design time. You can alternatively specify the image by setting the <xref:System.Windows.Forms.PictureBox.ImageLocation%2A> property and then load the image synchronously using the <xref:System.Windows.Forms.PictureBox.Load%2A> method or asynchronously using the <xref:System.Windows.Forms.PictureBox.LoadAsync%2A> method. The <xref:System.Windows.Forms.PictureBox.SizeMode%2A> property controls how the image and control fit with each other. For more information, see [How to: Modify the Size or Placement of a Picture at Run Time](how-to-modify-the-size-or-placement-of-a-picture-at-run-time-windows-forms.md).


## Transparent images

The following image formats are supported for transparency:

- 32-bit PNG
- 8-bit PNG
- 32-bit BMP
- 32-bit TIFF
- GIF

Image transparency doesn't show other controls behind the `PictureBox` control. It only shows the background of the control.

## See also

- <xref:System.Windows.Forms.PictureBox>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ description: Learn how the TableLayoutPanel control arranges its content in a gr

The <xref:System.Windows.Forms.TableLayoutPanel> control arranges its contents in a grid. Because the layout is performed both at design time and run time, it can change dynamically as the application environment changes. This gives the controls in the panel the ability to proportionally resize, so they can respond to changes such as the parent control resizing or text length changing due to localization.

Any Windows Forms control can be a child of the <xref:System.Windows.Forms.TableLayoutPanel> control, including other instances of <xref:System.Windows.Forms.TableLayoutPanel>. This allows you to construct sophisticated layouts that adapt to changes at run time.
Any Windows Forms control can be a child of the <xref:System.Windows.Forms.TableLayoutPanel> control, including other instances of <xref:System.Windows.Forms.TableLayoutPanel>. This allows you to construct sophisticated layouts that adapt to changes at run time. However, only a single control can exist in a cell. If you want more than one control in a cell, you must group the controls in a `UserControl` or another layout control such as `Panel` or`TableLayoutPanel`.

The <xref:System.Windows.Forms.TableLayoutPanel> control can expand to accommodate new controls when they are added, depending on the value of the <xref:System.Windows.Forms.TableLayoutPanel.RowCount%2A>, <xref:System.Windows.Forms.TableLayoutPanel.ColumnCount%2A>, and <xref:System.Windows.Forms.TableLayoutPanel.GrowStyle%2A> properties. Setting either the <xref:System.Windows.Forms.TableLayoutPanel.RowCount%2A> or <xref:System.Windows.Forms.TableLayoutPanel.ColumnCount%2A> property to a value of 0 specifies that the <xref:System.Windows.Forms.TableLayoutPanel> will be unbound in the corresponding direction.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ If multiple presentation technologies are used in an application, such as WPF, W
- WPF supports hardware accelerated layered windows.

- WPF does not support transparency color keys, because WPF cannot guarantee to render the exact color you requested, particularly when rendering is hardware-accelerated.


For more information regarding the limitations of interop regions, see [HWNDs inside WPF](wpf-and-win32-interoperation.md#hwnds-inside-wpf).

## See also

- [WPF and Win32 Interoperation](wpf-and-win32-interoperation.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "How to: Persist and Restore Application-Scope Properties Across Application Sessions"
description: Learn how to persist and restore application-scope properties across application sessions via included code examples in XAML, C#, and Visual Basic.
ms.date: "03/30/2017"
ms.date: 06/13/2024
dev_langs:
- "csharp"
- "vb"
Expand All @@ -18,11 +18,17 @@ ms.assetid: 55d5904a-f444-4eb5-abd3-6bc74dd14226

This example shows how to persist application-scope properties when an application shuts down, and how to restore application-scope properties when an application is next launched.

## Example
## Example

The application persists application-scope properties to, and restores them from, isolated storage. Isolated storage is a protected storage area that can safely be used by applications without file access permission. The *App.xaml* file defines the `App_Startup` method as the handler for the <xref:System.Windows.Application.Startup?displayProperty=nameWithType> event, and the `App_Exit` method as the handler for the <xref:System.Windows.Application.Exit?displayProperty=nameWithType> event, as shown in the highlighted lines of the first example. The second example shows a portion of the *App.xaml.cs* and *App.xaml.vb* files that highlights the code for the `App_Startup` method, which restores application-scope properties, and the `App_Exit` method, which saves application-scope properties.
The application persists application-scope properties to, and restores them from, isolated storage. Isolated storage is a protected storage area that can safely be used by applications without file access permission. The *App.xaml* file defines the `App_Startup` method as the handler for the <xref:System.Windows.Application.Startup?displayProperty=nameWithType> event, and the `App_Exit` method as the handler for the <xref:System.Windows.Application.Exit?displayProperty=nameWithType> event, as shown in the highlighted lines of the following XAML:

[!code-xaml[HOWTOApplicationModelSnippets#PersistRestoreAppScopePropertiesXAML1](~/samples/snippets/csharp/VS_Snippets_Wpf/HOWTOApplicationModelSnippets/CSharp/App.xaml?highlight=1-7)]

[!code-csharp[HOWTOApplicationModelSnippets#PersistRestoreAppScopePropertiesCODEBEHIND1](~/samples/snippets/csharp/VS_Snippets_Wpf/HOWTOApplicationModelSnippets/CSharp/App.xaml.cs?highlight=17-55)]
[!code-vb[HOWTOApplicationModelSnippets#PersistRestoreAppScopePropertiesCODEBEHIND1](~/samples/snippets/visualbasic/VS_Snippets_Wpf/HOWTOApplicationModelSnippets/visualbasic/application.xaml.vb?highlight=14-45)]
> [!NOTE]
> The following XAML is written for CSharp. The Visual Basic version omits the class declaration.
:::code language="xaml" source="./snippets/persist-and-restore-application-scope-properties/csharp/App.xaml" highlight="5,6":::

This next example shows the Application code-behind, which contains the event handlers for the XAML. The `App_Startup` method restores application-scope properties, and the `App_Exit` method saves application-scope properties.

:::code language="csharp" source="./snippets/persist-and-restore-application-scope-properties/csharp/App.xaml.cs":::

:::code language="vb" source="./snippets/persist-and-restore-application-scope-properties/vb/Application.xaml.vb":::
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application x:Class="SDKSamples.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="App_Startup"
Exit="App_Exit">
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows;

namespace SDKSamples
{
public partial class App : Application
{
string _filename = "App.data";

public App()
{
// Initialize application-scope property
Properties["NumberOfAppSessions"] = "0";
}

private void App_Startup(object sender, StartupEventArgs e)
{
// Restore application-scope property from isolated storage
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
try
{
if (storage.FileExists(_filename))
{
using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Open, FileAccess.Read))
using (StreamReader reader = new StreamReader(stream))
{
// Restore each application-scope property individually
while (!reader.EndOfStream)
{
string[] keyValue = reader.ReadLine().Split(new char[] { ',' });
Properties[keyValue[0]] = keyValue[1];
}
}
}
}
catch (DirectoryNotFoundException ex)
{
// Path the file didn't exist
}
catch (IsolatedStorageException ex)
{
// Storage was removed or doesn't exist
// -or-
// If using .NET 6+ the inner exception contains the real cause
}
}

private void App_Exit(object sender, ExitEventArgs e)
{
// Increase the amount of times the app was opened
Properties["NumberOfAppSessions"] = int.Parse((string)Properties["NumberOfAppSessions"]) + 1;

// Persist application-scope property to isolated storage
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForDomain();
using (IsolatedStorageFileStream stream = storage.OpenFile(_filename, FileMode.Create, FileAccess.Write))
using (StreamWriter writer = new StreamWriter(stream))
{
// Persist each application-scope property individually
foreach (string key in Properties.Keys)
writer.WriteLine("{0},{1}", key, Properties[key]);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Windows;

[assembly:ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Window x:Class="SDKSamples.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SDKSamples"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
<Grid>
<Label Content="Times app was opened: " HorizontalAlignment="Left" Margin="31,80,0,0" VerticalAlignment="Top"/>
<Label Content="X" HorizontalAlignment="Left" Margin="168,80,0,0" VerticalAlignment="Top" x:Name="lblTimes"/>

</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SDKSamples
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void Window_Loaded(object sender, RoutedEventArgs e)
{
lblTimes.Content = App.Current.Properties["NumberOfAppSessions"];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net481</TargetFramework>
<UseWPF>true</UseWPF>
<RootNamespace>SDKSamples</RootNamespace>
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
<security>
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
<!-- UAC Manifest Options
If you want to change the Windows User Account Control level replace the
requestedExecutionLevel node with one of the following.
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
Specifying requestedExecutionLevel element will disable file and registry virtualization.
Remove this element if your application requires this virtualization for backwards
compatibility.
-->
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>

<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- A list of the Windows versions that this application has been tested on
and is designed to work with. Uncomment the appropriate elements
and Windows will automatically select the most compatible environment. -->

<!-- Windows Vista -->
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->

<!-- Windows 7 -->
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->

<!-- Windows 8 -->
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->

<!-- Windows 8.1 -->
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->

<!-- Windows 10 -->
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->

</application>
</compatibility>

<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config.
Makes the application long-path aware. See https://docs.microsoft.com/windows/win32/fileio/maximum-file-path-limitation -->
<!--
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
<longPathAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true</longPathAware>
</windowsSettings>
</application>
-->

<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
<dependency>
<dependentAssembly>
<assemblyIdentity
type="win32"
name="Microsoft.Windows.Common-Controls"
version="6.0.0.0"
processorArchitecture="*"
publicKeyToken="6595b64144ccf1df"
language="*"
/>
</dependentAssembly>
</dependency>

</assembly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8.1" />
</startup>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml"
Startup="App_Startup"
Exit="App_Exit">
</Application>
Loading

0 comments on commit 7b8d713

Please sign in to comment.