本文介绍了VB.NET 如何使用 SendMessage 发送密钥 46 (Chr(46))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 VB.net 自动化 IBM iSeries 仿真器,创建的 GUI 使用SetParent"API 将外部 iSeries 仿真器窗口嵌入到 WindowsForm 面板中.为了与 iSeries 窗口通信,我使用 SendMessage API 将密钥发送到屏幕.我知道 .net 中的 sendkeys.keys,但这样我就不必设置 ForegroundWindow 并重新激活我的表单.

I am Automating IBM iSeries Emulators using VB.net, created GUI that embeds the external iSeries Emulator Window in a WindowsForm Panel using the "SetParent" API .. To communicate with the iSeries Window I use the SendMessage API to send Key's to the Screen. I am aware of the sendkeys.keys in .net, but this way I dont have to SetForegroundWindow and reactivate my Form.

我的程序读取秤的输出并验证接收到的数据是否正确,如果正确,它将使用此代码将输出发送到 iSeries 屏幕(UserApi 是我的 Lib):

My program reads the output of a scale and validates if the received data is good, if so it will send the output to the iSeries Screen using this code (UserApi is my Lib):

For Each element As Char In CStr(txtto400.Text)
    UserApi.SendMessage(as400WindowHandle, 256, CType(Convert.ToInt32(element), IntPtr), CType(0, IntPtr))
Next

我在其他项目上做过几次,这种方法效果很好,但在我的 TextBox (txtto400.Text) 中,我的体重有一个类似000.88 KG"的值.SendMessage 不会发送作为句点 (.) 的密钥46",而是00088 KG"

I done this a few times on other projects and this method works good, but in my TextBox (txtto400.Text) my weight has a Value like "000.88 KG".SendMessage will not send the key "46" which is a period (.) Instead it would be "00088 KG"

有谁知道为什么这不起作用?有什么建议我可以做什么吗?

Does anyone have an idea why this dont work? Any suggestions what I could do?

这是我解决它的方法.

我的 SendMessage 函数如下所示:

My SendMessage function looks like this:

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Public Shared Function SendMessage( _
   ByVal hwnd As IntPtr, _
   ByVal wMsg As UInt32, _
   ByVal wParam As IntPtr, _
   ByVal lParam As IntPtr) As IntPtr
End Function

我创建了一个保存 VK_OEM_PERIOD 的变量:

I created a variable that holds the VK_OEM_PERIOD:

Public Const VK_OEM_PERIOD = &HBE

创建了一个检查 Chr 的函数:

Created a Function that will check the Chr:

Public Function vk_key(key As Char) As Integer
 Select Case key
     Case Is = Chr(46)
         Return VK_OEM_PERIOD
     Case Else
        Return AscW(key)
 End Select
End Function

这是我将它传递给 iSeries 窗口的方法:

here is how I pass it to the iSeries Window:

For Each element As Char In txtto400.Text
   UserApi.SendMessage(as400WindowHandle, 256, vk_key(element), 0)
Next

推荐答案

WM_KEYDOWN 消息需要非系统键的虚拟键代码"作为 wParam(请参阅 MSDN).

The WM_KEYDOWN message expects "The virtual-key code of the nonsystem key" as wParam (see MSDN).

这恰好对应于字母数字符号的键字符的 ASCII 值,但不适用于所有其他键.在一段时间内,我认为您需要发送 VK_OEM_PERIOD.

This happens to correspond to the key character's ASCII value for alphanumeric signs but not for all other keys. For a period I think you need to send VK_OEM_PERIOD.

您可以在此处找到虚拟键代码表:http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx

You can find the table of virtual-key codes here: http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731%28v=vs.85%29.aspx

这篇关于VB.NET 如何使用 SendMessage 发送密钥 46 (Chr(46))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 00:06