zoukankan      html  css  js  c++  java
  • C# 小技巧

    WPF自定义按键实现窗口的关闭、最小化和拖动

     1 private void Grid_MouseMove(object sender, MouseEventArgs e) 
    2 {
    3   if (e.LeftButton == MouseButtonState.Pressed) this.DragMove();
    4 }
    5
    6 private void ItmMin_Click(object sender, RoutedEventArgs e)
    7 {
    8   this.WindowState = WindowState.Minimized;
    9 }
    10
    11 private void ItmClose_Click(object sender, RoutedEventArgs e)
    12 {
    13   this.Close();
    14 }

    自定义类有时需要通过类似数组下标方式访问数据 (例:x = a[i,j] ) 可以定义索引器,和访问器比较类似:

     1 public CplxNumber this[int x, int y]//x,y是下标,在函数内当做参数使用 
    2 {
    3   get
    4   {
    5     if (x <= 0 || y <= 0 || x > _height || y > _width) return null;
    6     else return new CplxNumber(_data[(x - 1) * _width * 2 + (y - 1) * 2], _data[(x - 1) * _width * 2 + (y - 1) * 2 + 1]);
    7   }
    8   set
    9   {
    10     if (x <= 0 || y <= 0 || x > _height || y > _width) return;
    11     else { _data[(x - 1) * _width * 2 + (y - 1) * 2] = value.Real; _data[(x - 1) * _width * 2 + (y - 1) * 2 + 1] = value.Imag; }
    12   }
    13 }

    运算符重载,也是比较常用的的小技巧,形式是 public static <type> opeartor <opeartor >(<type> a,<type> b) 示例如下:

    1 public static CplxNumber operator *(CplxNumber c1,CplxNumber c2) 
    2 {
    3   return new CplxNumber(c1.Real*c2.Real-c1.Imag*c2.Imag,c1.Real*c2.Imag+c1.Imag*c2.Real);
    4 }

    通过窗口打开和保存文件

     1 /// <summary>
    2 /// 弹出文件选择窗口获得选择文件
    3 /// </summary>
    4 /// <returns></returns>
    5 private BitmapSource GetSource()
    6 {
    7   BitmapSource result = null;
    8
    9   OpenFileDialog dialog = new OpenFileDialog();
    10   dialog.Title = "打开图片";
    11   dialog.Filter = "常用位图(*.bmp;*.jpg;*.png)|*.bmp;*.jpg;*.png";
    12
    13   if (true == dialog.ShowDialog() && File.Exists(dialog.FileName))
    14   {
    15     FileStream imageStreamSource = File.OpenRead(dialog.FileName);
    16     BitmapDecoder decoder = BitmapDecoder.Create(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
    17     result = decoder.Frames[0];
    18   }
    19
    20   return result;
    21 }
    22
    23 private void OutPut_Click(object sender, RoutedEventArgs e)
    24 {
    25
    26   SaveFileDialog dialog = new SaveFileDialog();
    27   dialog.Filter = "图片(*.jpg)|*.jpg";
    28   dialog.FileName = Title;
    29   dialog.Title = "保存图片";
    30
    31 if (dialog.ShowDialog() == true)
    32 {
    33   // 利用JpegBitmapEncoder,对图像进行编码,以便进行保存
    34   JpegBitmapEncoder encoder = new JpegBitmapEncoder();
    35   encoder.Frames.Add(BitmapFrame.Create(_image));
    36   // 保存文件
    37   FileStream fileStream = new FileStream(dialog.FileName, FileMode.Create, FileAccess.ReadWrite);
    38   encoder.Save(fileStream);
    39   // 关闭文件流
    40   fileStream.Close();
    41
    42   MessageBox.Show("保存成功!");
    43   }
    44 }



  • 相关阅读:
    30天敏捷结果(2):用三个故事驱动你的一周
    30天敏捷结果(24):恢复你的精力
    30天敏捷结果(6):周五回顾,找到三件做的好以及三件需要改善的事情
    js 数组循环和迭代
    没有+求和
    js检测数组类型
    redis 在windows 下的安装和使用
    版本控制(一)——初步概念
    苹果树的故事(转发的)
    mongoDB之在windows下的安装
  • 原文地址:https://www.cnblogs.com/GhostZCH/p/2437595.html
Copyright © 2011-2022 走看看