zoukankan      html  css  js  c++  java
  • WPF使用笔记-计时器,多线程更新界面,焦点移动等

    实习期间开始使用WPF,记录一点东西,没什么技术含量。

    // 计时器

    System.Windows.Threading.DispatcherTimer ShowTimer;

    ShowTimer = new System.Windows.Threading.DispatcherTimer();

    ShowTimer.Tick += new EventHandler(ShowCurTimer);

    ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);

    ShowTimer.Start();

    private void ShowCurTimer(object sender, EventArgs e)

    {

      // 时分秒 this.TimeText.Text = DateTime.Now.ToString("HH:mm:ss");

      // 日期 农历 5月1日 星期日

      string MM = DateTime.Now.ToString("MM").TrimStart('0');

       string dd = DateTime.Now.ToString("dd").TrimStart('0');

       this.DateText.Text = "阳历 ";

      this.DateText.Text += MM;

      this.DateText.Text += "月";

       this.DateText.Text += dd;

       this.DateText.Text += "日 ";

       this.DateText.Text += DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));

    }

    // 线程

    System.Threading.Thread threadRead;

    // 互斥锁(看情况使用)

    object lockMe = new object();

    threadRead = new System.Threading.Thread(threadReadFun);

    threadRead.Start();

    private void threadReadFun()

    {

      while (true)

      {

        System.Threading.Thread.Sleep(TimeSpan.FromSeconds(0.5));

         // 上锁 lock (lockMe) { }

         // 多线程中更新界面

         this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new delegate1(show));

      }

    }

    // 上述线程中无法对界面控件进行更新,可定义委托用于在多线程中更新界面

    public delegate void delegate1();

    private void show()

    {

      this.TextBlockName.Text = "new";

    }

    // 后台操作控件,可通过查找Name

    ((Image)this.FindName("Image1")).Visibility = System.Windows.Visibility.Hidden;

    // 多窗口关闭

    // xaml

    Closing="onClosing"

    // cs

    private void onClosing(object sender, System.ComponentModel.CancelEventArgs e)

    {

      Environment.Exit(0);

    }

    // TextBox

    GotFocus, LostFocus 获得和失去焦点事件,Tab获得焦点后使用 TextBox.SelectAll()选中所有内容 这对键盘重新输入内容很方便


    使用按键控制焦点移动:

    private void keyDown(object sender, KeyEventArgs e)
    {
      UIElement element = Keyboard.FocusedElement as UIElement;
      if (e.Key == Key.A)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Previous));
          e.Handled = true;
        }
      }
      else if (e.Key == Key.D)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
          e.Handled = true;
        }
      }
      else if (e.Key == Key.W)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Up));
          e.Handled = true;
        }
      }
      else if (e.Key == Key.S)
      {
        if (element != null)
        {
          element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Down));
          e.Handled = true;
        }
      }
      else
      {
        return;
      }
    }

  • 相关阅读:
    Halcon 如何将图像转化为矩阵形式
    Halcon 图像分割
    Halcon intensity算子,用于计算灰度的均值和方差
    Halcon draw_region接口
    Halcon scale_image 函数用法技巧
    Halcon 保存图像
    Halcon 读取多张图片
    Halcon 算子 sub_image add_image mult_image div_image
    Halcon 算子 get_grayval 用于读取图像的灰度值
    Halcon 算子 convert_image_type 转换图像类型
  • 原文地址:https://www.cnblogs.com/ht-beyond/p/5468575.html
Copyright © 2011-2022 走看看