| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1322 人关注过本帖
标题:在VB6里编写一个软键盘程序
取消只看楼主 加入收藏
月魔
Rank: 1
等 级:新手上路
帖 子:25
专家分:0
注 册:2006-7-24
收藏
 问题点数:0 回复次数:0 
在VB6里编写一个软键盘程序

准备工作:


先画两个窗体Form1和Form2,Form2是作为接收按键测试用的,在上面放一个文本框
Text1,把


Text1的MultiLine属性设为True,ScrollBars设为3-Both,然后写入如下代码:


Private Declare Function EnableWindow Lib "user32" (ByVal hwnd As Long,
ByVal


fEnable As Long) As Long


Private Sub Form_Load()


'调整大小


Me.Width = Screen.Width * 0.9


Me.Height = Screen.Height * 0.9


Form1.Show


'禁止鼠标和键盘输入


EnableWindow Text1.hwnd, False


End Sub


Private Sub Form_Resize()


'调整文本框大小


Text1.Top = 0


Text1.Left = 0


Text1.Width = Me.ScaleWidth


Text1.Height = Me.ScaleHeight


End Sub


Private Sub Form_Unload(Cancel As Integer)


End


End Sub


Form1作为小键盘用,把它的BorderStyle设为3-Fixed Dialog,把ControlBox属性设


为False,这样小键盘就没有标题栏了。Form1得跟微软智能拼音输入法的小键盘完全一
样的大小


的窗体,经笔者的计算长×高是5310×2130。然后放上10个按扭,其中九个的Caption
分别是


Tab、Caps、↑Shift、Ins、Del、(空格)、Esc、Enter和←(退格)。它们的位置和大小
如下设


置:


Caption Top Left Height Width Name


↑Shift 1230 0 400 840 Command1


Caps 828 0 400 560 Command2


Tab 410 0 400 480 Command3


Ins 1640 0 400 570 Command4


Del 1640 870 400 550 Command5


(空格) 1640 1400 400 2565 Command6


Esc 1640 4670 400 550 Command7


Enter 820 4590 400 630 Command9


←(退格) 820 4590 400 630 Command10


接下来做一个Height为400、Width为360的按扭Command8,Caption为A a(注意中间是
两个空


格,这一点非常重要!),由于按扭太小,系统会自动换行,看上去A在a的上面。复制
该按扭,然


后在窗体上粘贴,程序会提示你是否产生控件数组,选择是,Command8变成了
Command8(0),新产


生的按扭为Command8(1),一直粘贴,直到Command8(46),修改它们的Caption使之与键
盘上的其


它键一样。这里要注意几点:


1、按扭的字体为“小五”,有几个键显示的结果是纵向并排,可把字体设为“六
号”,使之横向


并排,字体使用宋体,否则↑和←显示的不漂亮。


2、&符号要使用两个,即 && 7(&&与7之间只有一个空格),确保每个按扭的Caption的
长度为四


个字母。


3、把窗体的MousePointer设为15 - Size All,以便在运行的时候移动小键盘。


4、把所有按键的MousePointer都设为99 - Custom,而MouseIcon设为手形指针。


5、由于要指示Shift是否按下,Command1的Style属性设置为1-Graphical。


6、Form2的StartUpPosition属性设置为2-屏幕中心。


我们把Command8(x)的这些键暂时叫做“普通键”。由于前面已经定位了9个特殊键,所
以可以通


过菜单栏的“格式”里面的“顶端对齐”、“相同水平间距”等选项很容易地把47个普
通键放到与


键盘对应的位置上。另外,为了显示CAPS LOCK的状态,我们在窗体上加一个标签
Label1,把它的


AutoSize属性设为True,Caption属性设为●,它的Top、Left、Height和With分别为
1340、


4800、180和180。


接下来是写代码了,这里主要用了SendMessage发送消息的API函数,由于按扭CAPTION
设置的巧


妙,所以47个普通键的代码就用的很少。所有代码如下:


'API声明


Private Declare Function ReleaseCapture Lib "user32" () As Long


Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
(ByVal


hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As
Long


Private Declare Function SetWindowPos& Lib "user32" (ByVal hwnd As Long,
ByVal


hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long,


ByVal cy As Long, ByVal wFlags As Long)


Private Declare Sub GetKeyboardStateByString Lib "user32"


Alias "GetKeyboardState" (ByVal pbKeyState As String)


Private Declare Sub SetKeyboardStateByString Lib "user32"


Alias "SetKeyboardState" (ByVal lppbKeyState As String)


'常数


Const VK_CAPITAL = &H14


Const WM_CHAR = &H102


Const HTCAPTION = 2


Const WM_NCLBUTTONDOWN = &HA1


Const WM_KEYDOWN = &H100


Const VK_DELETE = &H2E


Const VK_INSERT = &H2D


'定义变量


Dim rc as Long


Private Sub Command1_Click()


'Shift键


If Command1.BackColor = &H8000000B Then


Command1.BackColor = &H80000005


Else


Command1.BackColor = &H8000000B


End If


End Sub


'退格键


Private Sub Command10_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_CHAR, 8, 1)


End Sub


'大小写转换键


Private Sub Command2_MouseDown(Button As Integer, Shift As Integer, X As


Single, Y As Single)


Dim CAPITALKey As String * 256


CAPITALKey = Space$(256)


GetKeyboardStateByString (CAPITALKey)


If Label1.ForeColor = vbBlack Then


Label1.ForeColor = vbGreen


Mid$(CAPITALKey, VK_CAPITAL + 1, 1) = Chr$(1)


Else


Label1.ForeColor = vbBlack


Mid$(CAPITALKey, VK_CAPITAL + 1, 1) = Chr$(0)


End If


Call SetKeyboardStateByString(CAPITALKey)


End Sub


'TAB键


Private Sub Command3_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_CHAR, 9, 1)


End Sub


'Ins键


Private Sub Command4_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_KEYDOWN, VK_INSERT, &H510001)


End Sub


'Del键


Private Sub Command5_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_KEYDOWN, VK_DELETE, 1)


End Sub


'空格键


Private Sub Command6_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_CHAR, 32, 1)


End Sub


'Esc键


Private Sub Command7_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_CHAR, 27, 1)


End Sub


'普通键


Private Sub Command8_Click(Index As Integer)


Dim keyx As Integer


If Command1.BackColor = &H80000005 Xor Label1.ForeColor = vbGreen Then


keyx = Asc(Mid(Command8(Index).Caption, 1, 1))


Else


keyx = Asc(Mid(Command8(Index).Caption, 4, 1))


End If


rc = SendMessage(Form2.Text1.hwnd, WM_CHAR, keyx, 1)


Command1.BackColor = &H8000000B


End Sub


'回车键


Private Sub Command9_Click()


rc = SendMessage(Form2.Text1.hwnd, WM_CHAR, 13, 1)


End Sub


'让小键盘在最前面


Private Sub Form_Load()


rtn = SetWindowPos(Me.hwnd, -1, 0, 0, 0, 0, 3)


End Sub


'移动没有标题的小键盘


Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single,
Y


As Single)


If Button = 1 Then


Dim ReturnVal As Long


X = ReleaseCapture()


ReturnVal = SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0)


End If


End Sub


最后要提醒您的是:如果你要用于多个Text要做如下两步:


1、把FORM2中的EnableWindow Text1.hwnd, False去掉。


2、把软键盘代码中所有的Form2.Text1.Hwnd换成如下代码:
Form2.ActiveControl.hwnd


瞧,超酷的小键盘就这样做成了!该程序在VB6+WIN98下运行通过,如果你需要本事例

搜索更多相关主题的帖子: 键盘 Long 编写 ByVal 
2006-07-24 11:00
快速回复:在VB6里编写一个软键盘程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.090257 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved