基本信息
源码名称:C# 软键盘终极版 源码下载
源码大小:0.34M
文件格式:.zip
开发语言:C#
更新时间:2016-07-20
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 5 元×
微信扫码支付:5 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
实现数字软键盘
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Media;
using System.Runtime.InteropServices;
namespace MyControls
{
public partial class KeyB : Form
{
SoundPlayer sp = new SoundPlayer(Properties.Resources.Click2);
private bool _show;
public KeyB()
{
InitializeComponent();
_show = false;
}
private void KeyB_Load(object sender, EventArgs e)
{
num_0.Click = new EventHandler(NumClick);
num_1.Click = new EventHandler(NumClick);
num_2.Click = new EventHandler(NumClick);
num_3.Click = new EventHandler(NumClick);
num_4.Click = new EventHandler(NumClick);
num_5.Click = new EventHandler(NumClick);
num_6.Click = new EventHandler(NumClick);
num_7.Click = new EventHandler(NumClick);
num_8.Click = new EventHandler(NumClick);
num_9.Click = new EventHandler(NumClick);
}
#region 按键效果
private void num_1_MouseDown(object sender, MouseEventArgs e)
{
num_1.Image = imageList1.Images[0];
}
private void num_1_MouseUp(object sender, MouseEventArgs e)
{
num_1.Image = Properties.Resources.n_1;
}
private void num_2_MouseDown(object sender, MouseEventArgs e)
{
num_2.Image = imageList1.Images[1];
}
private void num_2_MouseUp(object sender, MouseEventArgs e)
{
num_2.Image = Properties.Resources.n_2;
}
private void num_3_MouseDown(object sender, MouseEventArgs e)
{
num_3.Image = imageList1.Images[2];
}
private void num_3_MouseUp(object sender, MouseEventArgs e)
{
num_3.Image = Properties.Resources.n_3;
}
private void num_4_MouseDown(object sender, MouseEventArgs e)
{
num_4.Image = imageList1.Images[3];
}
private void num_4_MouseUp(object sender, MouseEventArgs e)
{
num_4.Image = Properties.Resources.n_4;
}
private void num_5_MouseDown(object sender, MouseEventArgs e)
{
num_5.Image = imageList1.Images[4];
}
private void num_5_MouseUp(object sender, MouseEventArgs e)
{
num_5.Image = Properties.Resources.n_5;
}
private void num_6_MouseDown(object sender, MouseEventArgs e)
{
num_6.Image = imageList1.Images[5];
}
private void num_6_MouseUp(object sender, MouseEventArgs e)
{
num_6.Image = Properties.Resources.n_6;
}
private void num_7_MouseDown(object sender, MouseEventArgs e)
{
num_7.Image = imageList1.Images[6];
}
private void num_7_MouseUp(object sender, MouseEventArgs e)
{
num_7.Image = Properties.Resources.n_7;
}
private void num_8_MouseDown(object sender, MouseEventArgs e)
{
num_8.Image = imageList1.Images[7];
}
private void num_8_MouseUp(object sender, MouseEventArgs e)
{
num_8.Image = Properties.Resources.n_8;
}
private void num_9_MouseDown(object sender, MouseEventArgs e)
{
num_9.Image = imageList1.Images[8];
}
private void num_9_MouseUp(object sender, MouseEventArgs e)
{
num_9.Image = Properties.Resources.n_9;
}
private void num_0_MouseDown(object sender, MouseEventArgs e)
{
num_0.Image = imageList1.Images[9];
}
private void num_0_MouseUp(object sender, MouseEventArgs e)
{
num_0.Image = Properties.Resources.n_0;
}
private void back_MouseDown(object sender, MouseEventArgs e)
{
back.Image = imageList1.Images[10];
}
private void back_MouseUp(object sender, MouseEventArgs e)
{
back.Image = Properties.Resources.backspace;
}
#endregion
#region 系统调用
public static class Win32API
{
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
public static extern int SendMessage(
int hwnd,
int wMsg,
int wParam,
int lParam);
[DllImport("user32.dll", EntryPoint = "PostMessageW")]
public static extern int PostMessage(
int hwnd,
int wMsg,
int wParam,
int lParam);
[DllImport("user32.dll")]
public static extern int GetForegroundWindow();
[DllImport("user32.dll")]
public static extern int GetFocus();
[DllImport("user32.dll")]
public static extern int AttachThreadInput(
int idAttach,
int idAttachTo,
int fAttach);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(
int hwnd,
int lpdwProcessId);
[DllImport("kernel32.dll")]
public static extern int GetCurrentThreadId();
public const int WM_MOUSEACTIVATE = 0x21;
public const int WM_KEYDOWN = 0x100;
public const int MA_NOACTIVATE = 3;
public const int WS_EX_NOACTIVATE = 0x8000000;
}
#endregion
private void AttachThreadInput(bool b)
{
//设置线程亲和,附到前台窗口所在线程,只有在线程内才可以获取线程内控件的焦点
//线程亲和: AttachThreadInput(目标线程标识, 当前线程标识, 非零值关联) 零表示取消
//窗口所在的线程的标识: GetWindowThreadProcessId(窗体句柄, 这里返回进程标识)
//当前的前台窗口的句柄: GetForegroundWindow()
//当前程序所在的线程标识: GetCurrentThreadId()
Win32API.AttachThreadInput(
Win32API.GetWindowThreadProcessId(
Win32API.GetForegroundWindow(), 0),
Win32API.GetCurrentThreadId(), Convert.ToInt32(b));
}
/// <summary>
/// 数字键点击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void NumClick(object sender, EventArgs e)
{
sp.Play();
AttachThreadInput(true); //设置线程亲和的关联
int getFocus = Win32API.GetFocus();
//o为object类型,使用强制转换成vkButton
char keyvalue = ((PictureBox)sender).Name.Substring(4, 1).ToCharArray()[0];
//向前台窗口发送按键消息
Win32API.PostMessage(getFocus, Win32API.WM_KEYDOWN, (byte)keyvalue, 0);
AttachThreadInput(false); //取消线程亲和的关联
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
//若在窗体上产生鼠标点击事件的消息则使消息返回值标记为不激活
//程序内部的窗口切换仅需返回 MA_NOACTIVATE 即可,若相对其它
//的程序窗口切换时 还需要设置 WS_EX_NOACTIVATE 样式
if (m.Msg == Win32API.WM_MOUSEACTIVATE)
m.Result = (IntPtr)Win32API.MA_NOACTIVATE;
//MA_ACTIVATE 激活窗体,但不删除鼠标消息。
//MA_NOACTIVATE 不激活窗体,也不删除鼠标消息。
//MA_ACTIVATEANDEAT 激活窗体,删除鼠标消息。
//MA_NOACTIVATEANDEAT 不激活窗体,但删除鼠标消息。
}
private const int WS_EX_TOOLWINDOW = 0x00000080;
private const int WS_EX_NOACTIVATE = 0x08000000;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= (WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW);
cp.Parent = IntPtr.Zero; // Keep this line only if you used UserControl
return cp;
//return base.CreateParams;
}
}
//protected override CreateParams CreateParams
//{
// get
// {
// CreateParams cp = base.CreateParams;
// //为窗体样式添加不激活标识
// cp.ExStyle = Win32API.WS_EX_NOACTIVATE;
// return cp;
// }
//}
//关闭键盘
private void close_Click(object sender, EventArgs e)
{
_show = false;
sp.Play();
this.Hide();
}
//退格操作
private void back_Click(object sender, EventArgs e)
{
sp.Play();
AttachThreadInput(true); //设置线程亲和的关联
int getFocus = Win32API.GetFocus();
//向前台窗口发送按键消息
Win32API.PostMessage(getFocus, Win32API.WM_KEYDOWN, 0X08, 0);
AttachThreadInput(false); //取消线程亲和的关联
}
public void btnshow()
{
if (!_show)
{
this.Show();
_show = true;
}
}
private void KeyB_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
}
}