全屏随飞机地点置显得图片的小程序,keyDown事件

想法:将屏幕截图作为程序背景图,在之上弹出提示窗口,选择确定后进行定时图片随机位置显示。(支持ESC键退出)

C#全屏随机位置显示图片的小程序,

想法:将屏幕截图作为程序背景图,在之上弹出提示窗口,选择确定后进行定时图片随机位置显示。(支持ESC键退出)

  • 需要添加的控件:Timer

图片 1

1 Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); 2 3 public Form1() 4 { 5 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; 6 this.BackgroundImage = GetNoCursor(); 7 InitializeComponent(); 8 } 9 10 private void Form1_Load(object sender, EventArgs e) 11 { 12 timer1.Interval = 500; 13 if (MessageBox.Show("消息", "标题", MessageBoxButtons.YesNo) == DialogResult.Yes) 14 { 15 timer1.Enabled = true; 16 } 17 else 18 { 19 this.Close(); 20 } 21 } 22 23 private void Form1_KeyDown(object sender, KeyEventArgs e) 24 { 25 if (e.KeyData == Keys.Escape) 26 { 27 timer1.Enabled = false; 28 MessageBox.Show("消息", "标题", MessageBoxButtons.OK); 29 this.Close(); 30 } 31 } 32 33 private Bitmap GetNoCursor() 34 { 35 Bitmap Source = new Bitmap(bounds.Width, bounds.Height); //根据屏幕大小创建Bitmap对象 36 Graphics g = Graphics.FromImage(Source); 37 g.CopyFromScreen(0, 0, 0, 0, Source.Size); //获取没有鼠标的屏幕截图 38 g.Dispose(); //释放资源 39 return Source; 40 } 41 42 private void timer1_Tick(object sender, EventArgs e) 43 { 44 Image img = Resource1.Image1;//获取用于显示的资源文件 45 if (img != null) 46 { 47 Graphics g = this.CreateGraphics(); 48 Random rd = new Random(); 49 int picXPoint = rd.Next(0, bounds.Right - img.Width); 50 int picYPoint = rd.Next(0, bounds.Height - img.Height); 51 Point ulCorner = new Point(picXPoint, picYPoint); 52 g.DrawImageUnscaled(img, ulCorner); 53 } 54 else 55 { 56 timer1.Enabled = false; 57 MessageBox.Show("没有图片,感谢使用"); 58 this.Close(); 59 } 60 }

 

想法:将屏幕截图作为程序背景图,在之上弹出提示窗口,选择确定后进行定时图片随机位置显示。(...

在做3D漫游时,分别运用WASD和方向键,控制视角前后左右,KeyDown事件记录漫游开始标记,但是WASD可以Debug进入,方向键却始终无法进入,很奇怪的是,进不了KeyDown,却能响应KeyUp事件

  • 需要添加的控件:Timer

但是Ctrl 方向键和Alt 方向键却可以进入,经过一番查找。很多文章中分析可能是因为方向键默认是用来处理控件焦点移动,由控件自己处理,暂时没有权威说法,但此问题是按下述套路解决的。

图片 2

 

 

Control::ProcessDialogKey这个函数可以告诉程序案件时由控件自己处理还是写的程序来处理

  • 需要修改的Form1属性为下图红色区域:

This method is called during message preprocessing to handle dialog characters, such as TAB, RETURN, ESC, and arrow keys.This method is called only if the IsInputKey.aspx) method indicates that the control is not processing the key.The ProcessDialogKey simply sends the character to the parent's ProcessDialogKey method, or returns false if the control has no parent.The Form.aspx) class overrides this method to perform actual processing of dialog keys.This method is only called when the control is hosted in a Windows Forms application or as an ActiveX control.

 图片 3

 

图片 4

大概意思就是TAB, RETURN, ESC, and arrow keys这几个键是控件自己处理,但是重写以后可以自定义处理。(我只是理解他的意思,但做不到准确翻译,表达能力有限)。

图片 5

 

 

解决方案就是,遇到方向键,return false,那么控件就不会自动处理,交给KeyDown响应了。

  • 资源文件的添加:添加->新建项->资源文件

C#代码

图片 6

protected override bool ProcessDialogKey(Keys keyData)
{

     if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right)
     {
          return false;
      }
      else
      {
           return base.ProcessDialogKey(keyData);
      }
}
  • ESC键退出程序:

C /CLI代码

在Form1.Designer.cs中增加

virtual  bool ProcessDialogKey( System::Windows::Forms::Keys keyData)override
{
    if (keyData == System::Windows::Forms::Keys::Up || keyData == System::Windows::Forms::Keys::Down || keyData == System::Windows::Forms::Keys::Left || keyData == System::Windows::Forms::Keys::Right)
    {
          return false;
    }
    else
    {
          return __super::ProcessDialogKey(keyData);
    }
}

this.KeyDown = Form1_KeyDown;

 

  • 代码如下:

     1         Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty));
     2 
     3         public Form1()
     4         {
     5             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
     6             this.BackgroundImage = GetNoCursor();
     7             InitializeComponent();
     8         }
     9 
    10         private void Form1_Load(object sender, EventArgs e)
    11         {
    12             timer1.Interval = 500;
    13             if (MessageBox.Show("消息", "标题", MessageBoxButtons.YesNo) == DialogResult.Yes)
    14             {
    15                 timer1.Enabled = true;
    16             }
    17             else
    18             {
    19                 this.Close();
    20             }
    21         }
    22 
    23         private void Form1_KeyDown(object sender, KeyEventArgs e)
    24         {
    25             if (e.KeyData == Keys.Escape)
    26             {
    27                 timer1.Enabled = false;
    28                 MessageBox.Show("消息", "标题", MessageBoxButtons.OK);
    29                 this.Close();
    30             }
    31         }
    32 
    33         private Bitmap GetNoCursor()
    34         {
    35             Bitmap Source = new Bitmap(bounds.Width, bounds.Height);    //根据屏幕大小创建Bitmap对象
    36             Graphics g = Graphics.FromImage(Source);
    37             g.CopyFromScreen(0, 0, 0, 0, Source.Size);  //获取没有鼠标的屏幕截图
    38             g.Dispose();    //释放资源
    39             return Source;
    40         }
    41 
    42         private void timer1_Tick(object sender, EventArgs e)
    43         {
    44             Image img = Resource1.Image1;//获取用于显示的资源文件
    45             if (img != null)
    46             {
    47                 Graphics g = this.CreateGraphics();
    48                 Random rd = new Random();
    49                 int picXPoint = rd.Next(0, bounds.Right - img.Width);
    50                 int picYPoint = rd.Next(0, bounds.Height - img.Height);
    51                 Point ulCorner = new Point(picXPoint, picYPoint);
    52                 g.DrawImageUnscaled(img, ulCorner);
    53             }
    54             else
    55             {
    56                 timer1.Enabled = false;
    57                 MessageBox.Show("没有图片,感谢使用");
    58                 this.Close();
    59             }
    60         }
    

     

本文由星彩网app下载发布于计算机编程,转载请注明出处:全屏随飞机地点置显得图片的小程序,keyDown事件

TAG标签: 星彩网app下载
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。