using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
private static extern Int32 GetForegroundWindow();
[DllImport("user32.dll")]
private static extern Int32 GetWindowText(Int32 hWnd, StringBuilder lpsb, Int32 count);
public Form1()
{
InitializeComponent();
Timer mytimer = new Timer();
mytimer.Tick += new EventHandler(mytimer_Tick);
mytimer.Start();
/*
// Create a TextBox control.
TextBox tb = new TextBox();
this.Controls.Add(tb);
//將此改為windows視窗物件
//https://msdn.microsoft.com/zh-tw/library/system.windows.window(v=vs.110).aspx
tb.KeyPress += new KeyPressEventHandler(keypressed);
*/
}
private void keypressed(Object o, KeyPressEventArgs e)
{
//判斷按鍵的 Keychar
char Key_Char = e.KeyChar;
// MessageBox.Show(((int)(Key_Char)).ToString());
MessageBox.Show(((Key_Char)).ToString());
}
private void button1_Click(object sender, EventArgs e)
{
Timer mytimer = new Timer();
mytimer.Tick += new EventHandler(mytimer_Tick);
mytimer.Start();
}
private void mytimer_Tick(object sender, EventArgs e)
{
GetCurrentWindow();//取得活動視窗
}
private void GetCurrentWindow()
{
Int32 handle = 0;
StringBuilder sb = new StringBuilder(256);
handle = GetForegroundWindow();
if (GetWindowText(handle, sb, sb.Capacity) > 0)
{
label1.Text = "視窗標題:" + sb.ToString();
}
}
}
}