WPF中用xaml怎么给Gird或canvas插入背景图片?

WPF中用xaml怎么给Gird或canvas插入背景图片?,第1张

下面我就大概说下过程,首页建立一个工程为WpfLoginView,并在Expression Blend 下设置一个如下图的界面

xaml代码如下:

<Window

    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"

    x:Class="WpfLoginView.LoginView"

    x:Name="Window"

    title="LoginView" mc:Ignorable="d" KeyDown="Window_KeyDown" WindowStartupLocation="CenterScreen" SizeToContent="WidthAndHeight" AllowsTransparency="True" WindowStyle="None" Foreground="#FF9DC2EF">

    <Grid x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="120.012"/>

            <ColumnDefinition Width="209.988"/>

            <ColumnDefinition Width="81"/>

        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>

            <RowDefinition Height="72.899"/>

            <RowDefinition Height="30.101"/>

            <RowDefinition Height="43"/>

            <RowDefinition Height="43"/>

            <RowDefinition Height="80"/>

        </Grid.RowDefinitions>

        <Label HorizontalAlignment="Left" Content="用户名" Margin="0,8" Grid.Column="1" Grid.Row="2" Width="60" FontSize="16" Foreground="#FFE2E4EB"/>

        <Label HorizontalAlignment="Left" Margin="0,4,0,12" Content="密  码" Grid.Column="1" Grid.Row="3" FontSize="16" Width="64" Foreground="#FFEEF0F5"/>

        <TextBox x:Name="txtusername" TextWrapping="Wrap" Margin="0,8,8,10" Grid.Column="1" Grid.Row="2" BorderThickness="2" HorizontalAlignment="Right" Width="137.988" d:LayoutOverrides="GridBox">

            <TextBox.BorderBrush>

                <LinearGradientBrush EndPoint="0,20" StartPoint="0,0" MappingMode="Absolute">

                    <GradientStop Color="#FFABADB3" Offset="0.05"/>

                    <GradientStop Color="#FFE2E3EA" Offset="0.07"/>

                    <GradientStop Color="#FFBCDBF9" Offset="1"/>

                </LinearGradientBrush>

            </TextBox.BorderBrush>

        </TextBox>

        <PasswordBox x:Name="txtpassword" Margin="0,6,8,11" Grid.Column="1" Grid.Row="3" BorderThickness="2" HorizontalAlignment="Right" Width="137.988" d:LayoutOverrides="GridBox">

            <PasswordBox.BorderBrush>

                <LinearGradientBrush EndPoint="0,20" StartPoint="0,0" MappingMode="Absolute">

                    <GradientStop Color="#FFABADB3" Offset="0.05"/>

                    <GradientStop Color="#FFE2E3EA" Offset="0.07"/>

                    <GradientStop Color="#FFC1DBF5" Offset="1"/>

                </LinearGradientBrush>

            </PasswordBox.BorderBrush>

        </PasswordBox>

        <Button x:Name="LoginConfirmButton" Content="登  陆" Grid.Column="1" Grid.Row="4" Click="LoginConfirmButton_Click" FontSize="16" VerticalAlignment="Top" Height="24.687" Margin="64,8,91.988,0" Style="{DynamicResource ConfirmCancelButtonStyle}" />

        <Button x:Name="LoginCancelButton" Margin="144.988,8,0,0" Content="取  消" HorizontalAlignment="Left" Width="57" Grid.Column="1" Grid.Row="4" Click="LoginCancelButton_Click" FontSize="16" VerticalAlignment="Top" Height="24.687" Style="{DynamicResource ConfirmCancelButtonStyle}"/>

    </Grid>

</Window>

注意一下这几个属性的设置:WindowStartupLocation="CenterScreen"  AllowsTransparency="True" WindowStyle="None"。两个Button的样式代码我就不贴了,下面我们假如有下面这么一张图片(Login.png),如何成为背景呢?

首先把这张图放在工程中

public LoginView()

{

    this.InitializeComponent()

     txtusername.Focus()//聚焦在用户名输入框中

    // 在此点之下插入创建对象所需的代码。

    ImageBrush b = new ImageBrush()

     b.ImageSource = new BitmapImage(new Uri("pack://application:,,,/Login.png"))

      b.Stretch = Stretch.Fill

      this.Background = b

   }

直接在构造函数中输入以上代码就Ok了。

# -*- coding:utf-8 -*-  

    # file: TkinterCanvas.py  

    #  

    import Tkinter         # 导入Tkinter模块  

    from PIL import Image, ImageTk  

      

    root = Tkinter.Tk()  

    canvas = Tkinter.Canvas(root,  

        width = 500,      # 指定Canvas组件的宽度  

        height = 600,      # 指定Canvas组件的高度  

        bg = 'white')      # 指定Canvas组件的背景色  

    #im = Tkinter.PhotoImage(file='img.gif')     # 使用PhotoImage打开图片  

    image = Image.open("img.jpg")  

    im = ImageTk.PhotoImage(image)  

      

    canvas.create_image(300,50,image = im)      # 使用create_image将图片添加到Canvas组件中  

    canvas.create_text(302,77,       # 使用create_text方法在坐标(302,77)处绘制文字  

       text = 'Use Canvas'      # 所绘制文字的内容  

       ,fill = 'gray')       # 所绘制文字的颜色为灰色  

    canvas.create_text(300,75,  

       text = 'Use Canvas',  

       fill = 'blue')  

    canvas.pack()         # 将Canvas添加到主窗口  

    root.mainloop()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11276534.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存