自定义WPF窗体形状的实战记录

2020-01-05 09:31:07于丽
  • 选择要用作窗体形状的图像文件。
  • 在大多数图像编辑工具中,使图像背景自由是一个非常简单的过程,但是,我将为Adobe Photoshop解释它(因为我喜欢该产品)。
  • 在Adobe Photoshop中打开图像文件。
  • 选择除背景之外的整个图像。
  • 复制选择。
  • 单击菜单文件>>新建。 这将显示一个对话框。 在对话框的“内容”部分中,选择“透明”,然后单击“确定”。
  • 将打开透明背景画布,将图像粘贴到那里并将其另存为PNG文件。

    WPF,窗体形状

    现在我们的图像文件准备就绪,让我们现在塑造我们的WPF表格。

    设置WPF应用

    打开Visual Studio 2008并创建新项目。

    在Visual C#项目模板下,选择Windows >> WPF应用。

    使用下面的XAML:

    
    <window allowstransparency="True" opacitymask="White" windowstyle="None" 
    
     background="Transparent" width="620" height="267" 
    
     xmlns:x=http://www.easck.com/winfx/2006/xaml 
    
     xmlns=http://www.easck.com/winfx/2006/xaml/presentation 
    
     x:class="ShapedWPFForm.ShapedWindow">
     <grid>
      <img name="imgBackground" source="Shape.png" stretch="Fill" />
     </grid>
    </window>