wpf 学习笔记

     </LinearGradientBrush.GradientStops>
 </LinearGradientBrush>
 <Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
     <Setter Property="Template">
         <Setter.Value>
             <ControlTemplate TargetType="{x:Type Button}">
                 <Border Background="{TemplateBinding Background}">
                     <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                 </Border>
             </ControlTemplate>
         </Setter.Value>
     </Setter>
     <Style.Triggers>
         <Trigger Property="IsMouseOver" Value="True">
             <Setter Property="Background" Value="{x:Null}"/>
         </Trigger>
     </Style.Triggers>
 </Style>

</Window.Resources>

//调用

6. grid 分割线

       <Grid.RowDefinitions>
           <RowDefinition Height="9*"/>
           <RowDefinition Height="9*"/>
           <RowDefinition Height="12*"/>
           <RowDefinition Height="12*"/>
           <RowDefinition Height="24*"/>
           <RowDefinition Height="3*"/>
       </Grid.RowDefinitions>
       <Rectangle Stroke="#FF6F6D6D"  Grid.Row="1"/>
       <Rectangle Stroke="#FF6F6D6D"  Grid.Row="2"/>
       <Rectangle Stroke="#FF6F6D6D" Grid.Row="3"/>
       <Rectangle Stroke="#FF6F6D6D" Grid.Row="4"/>
       <Rectangle Stroke="#FF6F6D6D" Grid.Row="5"/>

7. textblock 样式(渐变色 )

<TextBlock VerticalAlignment="Bottom"  HorizontalAlignment="Left" FontSize="25" FontWeight="Black" Cursor="AppStarting" Height="30">
    <TextBlock.Style>
        <Style  TargetType="{x:Type TextBlock}">
            <Setter Property="Foreground">
                <Setter.Value>
                    <LinearGradientBrush StartPoint="0.5,0" EndPoint="0.5,0.5">
                        <GradientStop Color="#FF87E1FF" Offset="0.0" />
                        <GradientStop Color="#FF009FFF" Offset="0.5" />
                    </LinearGradientBrush>
                </Setter.Value>
            </Setter>
        </Style>
 </TextBlock.Style><Run Text="抢答器专家"/>
</TextBlock>

//设置字体颜色
 <TextBlock Foreground="#FF03786B">加分</TextBlock>

8.textbox 样式(圆角)

<TextBox VerticalAlignment="Center" HorizontalAlignment="Center" Height="30" Width="60" >
    <TextBox.Resources>
        <Style TargetType="{x:Type Border}">
            <Setter Property="CornerRadius" Value="2"/>
            <Setter Property="BorderBrush" Value="#c1d0dc"/>
        </Style>
    </TextBox.Resources>
</TextBox>

9.button

//设置圆角
<Button.Resources>
    <Style TargetType="{x:Type Border}">
        <Setter Property="CornerRadius" Value="3"></Setter>
    </Style>
</Button.Resources>

//设置阴影
 <Button.Effect>
     <DropShadowEffect BlurRadius="20" ShadowDepth="0" Color="#DADADA" Opacity="0.2"/>
 </Button.Effect>

10.ComboBox

xaml:
<ComboBox Width="80" SelectionChanged="ComboBox_SelectionChanged">
    <ComboBoxItem IsSelected="True">0</ComboBoxItem>
    <ComboBoxItem>5</ComboBoxItem>
    <ComboBoxItem>10</ComboBoxItem>
    <ComboBoxItem>20</ComboBoxItem>
</ComboBox>

//允许编辑  设置IsEditable="True",即前面变成文本框形式
<ComboBox x:Name="AddScoreCombox" Width="80" IsEditable="True">
    <ComboBoxItem>0</ComboBoxItem>
    <ComboBoxItem>10</ComboBoxItem>
    <ComboBoxItem>11</ComboBoxItem>
    <ComboBoxItem IsSelected="True">19</ComboBoxItem>
</ComboBox>




cs:
 private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     int count = Convert.ToInt32(combobox.SelectedValue.ToString().Replace("System.Windows.Controls.ComboBoxItem: ", ""));
    
     SubWindow subWindow = SubWindow.pwindow;
     subWindow.Close();
     SubWindow.hcount = count;
     SubWindow subWindow2 = new SubWindow();
     subWindow2.Show();
 }



//动态生成Items
 for(int i = 1; i <= SubWindow.count; i++)
 {
     combobox.Items.Add(i.ToString());
 }

11.在子窗口控制主窗口动态生成控件时,会出现两个相同的窗口

解决办法:

在主窗口里

 public static SubWindow pwindow = null;

     public SubWindow()
     {
         InitializeComponent();
         pwindow = this;   //将当前窗口给pwindow,方便在setting里使用
     }

在子窗口里,

 SubWindow subWindow = SubWindow.pwindow;
 subWindow.Close();  //关闭上一个窗口

 SubWindow.hcount = count;
 SubWindow subWindow2 = new SubWindow();
 subWindow2.Show();

12.RadioButton

  <RadioButton GroupName="zu" Margin="20,10,0,10" Grid.Row="0"  Content="12" Click="RadioButton_Click"> </RadioButton>
  <RadioButton IsChecked="True" GroupName="zu" Margin="20,10,0,10" Grid.Row="1" Content="34" Click="RadioButton_Click"></RadioButton>


private void RadioButton_Click(object sender, RoutedEventArgs e)
{
    //获取radiobutton的值
    RadioButton radioButton = sender as RadioButton;
    radiobuttoncContent = radioButton.Content.ToString();
    if (radioButton.IsChecked == true)
    {
        SubWindow subWindow = SubWindow.pwindow;
        subWindow.Close();
        SubWindow subWindow2 = new SubWindow();
        subWindow2.Show();

    }
}

13.slider

<WrapPanel Margin="20,70,0,0">
    <TextBlock FontSize="14" LineHeight="12" Foreground="#333333">调整</TextBlock>
    <Slider x:Name = "slider2" Width="90" Minimum = "0" Maximum = "3"  ValueChanged = "slider1_ValueChanged" Margin = "23,0,0,0"></Slider>
    <TextBox x:Name="textbox1" Text="{Binding Textbox1}" Width="25" Height="25" Margin="5,-3,0,0" VerticalContentAlignment="Center" HorizontalContentAlignment="Center"></TextBox>
</WrapPanel>


 public static int buttonSize = 0;

private void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
    string val = Convert.ToInt32(e.NewValue).ToString();
    //string msg = String.Format("Current value: {0}", val);
    this.textbox1.Text = val;
    buttonSize =Convert.ToInt32(val);
    SubWindow subWindow = SubWindow.pwindow;
    subWindow.Close();
    SubWindow subWindow2 = new SubWindow();
    subWindow2.Show();
}

14.动态生成控件

 private void CreateCanvas3()
 {
     LinearGradientBrush brush3 = (LinearGradientBrush)this.FindResource("Brush3");
     LinearGradientBrush brush2 = (LinearGradientBrush)this.FindResource("Brush2");
     Canvas canvas = new Canvas()
     {
         Margin = new Thickness(30 + buttonsize * 3 + Llength, 40 + buttonsize * 4 + hlength * 2, 90 + buttonsize * 3 + Llength, 40 + buttonsize * 3 + hlength * 2)
     };
     //成绩
     Border border = new Border()
     {
         BorderThickness = new Thickness(2),
         BorderBrush = System.Windows.Media.Brushes.WhiteSmoke,
         Background = brush3,
         CornerRadius = new CornerRadius(4),
         Margin = new Thickness(30 + buttonsize * 6, 20 + buttonsize * 6, 2 + buttonsize * 6, 0+buttonsize * 6),
         Height = 35 + buttonsize * 4,
         Width = 80 + buttonsize * 5
     };
    
     Label label = new Label()
     {
         Content = "123",
         FontSize = 14 + buttonsize * 2,
         Margin = new Thickness(15, 1, 15, 0),
         HorizontalContentAlignment = HorizontalAlignment.Center,
         VerticalContentAlignment = VerticalAlignment.Center,
     };
     //名称
     Border border2 = new Border()
     {
         BorderThickness = new Thickness(2,0,2,2),
         BorderBrush = System.Windows.Media.Brushes.WhiteSmoke,
         Background = brush3,
         CornerRadius = new CornerRadius(0,0,4,4),
         Margin = new Thickness(30 + buttonsize * 6, 53 + buttonsize * 6, 0 + buttonsize * 6, 0 + buttonsize * 6),
         Height = 35 + buttonsize * 4,
         Width = 80 + buttonsize * 5
     };
     Label label2 = new Label()
     {
         Content = "456",
         FontSize = 14 + buttonsize * 2,
         Margin = new Thickness(18, 1, 15, 0),
         HorizontalContentAlignment = HorizontalAlignment.Center,
         VerticalContentAlignment = VerticalAlignment.Center,
     };
     border.Child = label;
     border2.Child = label2;
     //组号
     Border border1 = new Border()
     {
         BorderThickness = new Thickness(2),
         BorderBrush = System.Windows.Media.Brushes.WhiteSmoke,
         Background = brush2,
         CornerRadius = new CornerRadius(50),
         Margin = new Thickness(15 + buttonsize * 4, 17 + buttonsize * 4, 0, 0),
         Height = 42 + buttonsize * 2,
         Width = 40 + buttonsize * 2
     };
     Label label1 = new Label()
     {
         Content = "1",
         FontSize = 14 + buttonsize * 2,
         HorizontalContentAlignment = HorizontalAlignment.Center,
         VerticalContentAlignment = VerticalAlignment.Center,
         Margin = new Thickness(0, 4, 0, 0),
         MinWidth = 40
     };
     border1.Child = label1;
     canvas.Children.Add(border);
     canvas.Children.Add(border2);
     canvas.Children.Add(border1);
     canvaswidth = 110;
     buttonwrap.Children.Add(canvas);
 }

15.控件右击事件

右击事件是MouseRightButtonUp

//windows.resource里定义一个ContextMenu
<ContextMenu x:Key="ContextMenu">
    <MenuItem Name="setting" Header="设置" Click="MenuItem_Click">
    </MenuItem>
</ContextMenu>


//右击事件是MouseRightButtonUp,右击之后出现ContextMenu里的设置
 <Grid MouseRightButtonUp="Mouse_RightButtonUp" Margin="0,10,0,0">
     <WrapPanel x:Name="buttonwrap">
     </WrapPanel>
 </Grid>


private void Mouse_RightButtonUp(object sender, MouseButtonEventArgs e)
{
    Grid sp = (Grid)sender;
    ContextMenu cm = new ContextMenu();
   ContextMenu ct= this.FindResource("ContextMenu") as ContextMenu;
   sp.ContextMenu = ct;
}


  //打开设置窗口
  private void MenuItem_Click(object sender, RoutedEventArgs e)
  {
      setting st = new setting();
      st.ShowDialog();
  }

16.窗体靠近屏幕左边隐藏,并生成一个小球

参考:

C# GetWindowRect用法-CSDN博客

c# 获取当前活动窗口句柄,获取窗口大小及位置 - jack_Meng - 博客园 (cnblogs.com)")

WPF实现边缘依靠效果 - IPS99技术分享

C# 完美实现窗口边缘吸附功能 - 流泪的冰淇淋 - 博客园 (cnblogs.com)")

wpf 如何让当前窗口隐藏_wpf 窗口关闭设置看不到-CSDN博客

wpf 类似于360加速球,的拖动和点击功能的实现_-CSDN问答

[小结][N种方法]实现WPF不规则窗体 - DebugLZQ - 博客园 (cnblogs.com)")

WPF中控制窗口显示位置的三种方式 - LJD泊水 - 博客园 (cnblogs.com)")

WPF中三种方法得到当前屏幕的宽和高_fullprimaryscreenheight-CSDN博客

在要隐藏的页面的xaml里:


    <Grid MouseLeftButtonDown="Grid_MouseLeftButtonDown">
        <Image Stretch="Fill" Source="/WPFSharpWindow;component/cow.png" />
    </Grid>

cs:
  //定义2个变量记录信息
  Point _pressedPosition;
  bool _isDragMoved = false;

    
  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern IntPtr GetForegroundWindow();

  [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

  [DllImport("user32.dll")]
  [return: MarshalAs(UnmanagedType.Bool)]
  static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

  [StructLayout(LayoutKind.Sequential)]
  public struct RECT
  {
      public int Left;                             //最左坐标
      public int Top;                             //最上坐标
      public int Right;                           //最右坐标
      public int Bottom;                        //最下坐标
  }

 


![img](https://img-blog.csdnimg.cn/img_convert/829eede2853f126bd0f72f33db59bf64.png)
![img](https://img-blog.csdnimg.cn/img_convert/8f8a90f8ce5265daedc2f583a7c0b592.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

/最上坐标
      public int Right;                           //最右坐标
      public int Bottom;                        //最下坐标
  }

 


[外链图片转存中...(img-NX1ZJ9Bp-1714251124410)]
[外链图片转存中...(img-r14DhU9c-1714251124410)]

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值