C# 如何使用System.Windows.Forms.SendKeys.SendWait发送签名

C# 如何使用System.Windows.Forms.SendKeys.SendWait发送签名,c#,sendkeys,C#,Sendkeys,对于SendKeys,%符号是Alt键的代码。 但是我没有找到一种发送纯符号的方法。 下面的控制台应用程序显示了它。 您必须在之前启动notepad.exe。 然后,在记事本中只会看到“百分比符号=”而没有百分比。 按键后,程序将“%B”发送到记事本 它在德国系统上打开编辑菜单(Alt-B-->“Bearbeiten”=“Edit”)。 msdn帮助解释+^和%符号的功能,但不是我要做什么才能发送此符号。 有什么建议吗 using System; using System.Diagnostics

对于SendKeys,%符号是Alt键的代码。
但是我没有找到一种发送纯符号的方法。
下面的控制台应用程序显示了它。 您必须在之前启动notepad.exe。
然后,在记事本中只会看到“百分比符号=”而没有百分比。 按键后,程序将“%B”发送到记事本
它在德国系统上打开编辑菜单(Alt-B-->“Bearbeiten”=“Edit”)。
msdn帮助解释+^和%符号的功能,但不是我要做什么才能发送此符号。 有什么建议吗

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace SendTextToNotepadSendKeys
{
    class Program
    {
        [DllImport("user32.dll")]
        public static extern int FindWindow(
            string lpClassName, // class name     
            string lpWindowName // window name 
            );

        [DllImport("user32.dll")]
        public static extern int SetForegroundWindow(
            int hWnd // handle to window
            );

        static void Main(string[] args)
        {
            Console.WriteLine("At least one instance of notepad must be running! if ready press a key!");
            Console.ReadKey();
            Console.WriteLine("");
            int handleThisWindow = (int)Process.GetCurrentProcess().MainWindowHandle;
            int handleNotepad = FindWindow(null, "Unbenannt - Editor"); // change to your windows title - this is the german one
            if (handleNotepad == 0)
            {
                Console.WriteLine("notepad window not found - possibly you have to change the window title in program code! Please press a key!");
                Console.ReadKey();
            }
            else
            {
                // >>>>>>>>>>> here you see it <<<<<<<<<<<<<<
                SetForegroundWindow(handleNotepad);
                System.Windows.Forms.SendKeys.SendWait("percent-sign=%"); // the text ends with "="
                SetForegroundWindow(handleThisWindow);

                Console.WriteLine("the text in notepad ends with '='");
                Console.WriteLine("% is representing the Alt-key, we show it with the following code");
                Console.WriteLine("on german machines Alt-B will open the Edit-menu - possibly change 'B' in the code for your machine");
                Console.ReadKey();

                SetForegroundWindow(handleNotepad);
                System.Windows.Forms.SendKeys.SendWait("%B");
                Console.ReadKey();
            }
        }
    }
}
使用系统;
使用系统诊断;
使用System.Runtime.InteropServices;
命名空间SendTextToNotepadSendKeys
{
班级计划
{
[DllImport(“user32.dll”)]
公共静态外部程序(
字符串lpClassName,//类名
字符串lpWindowName//windowname
);
[DllImport(“user32.dll”)]
公共静态外部int SetForegroundWindow(
int-hWnd//窗口句柄
);
静态void Main(字符串[]参数)
{
WriteLine(“必须至少有一个记事本实例正在运行!如果准备就绪,请按键!”);
Console.ReadKey();
控制台。写线(“”);
int handleThisWindow=(int)Process.GetCurrentProcess().MainWindowHandle;
int handleNotepad=FindWindow(null,“unbenant-Editor”);//更改您的windows标题-这是德语标题
如果(handleNotepad==0)
{
Console.WriteLine(“找不到记事本窗口-可能您必须更改程序代码中的窗口标题!请按键!”);
Console.ReadKey();
}
其他的
{

//>>>>>>>>>>>>>>在这里,您可以看到要发送一个文本
%
,请将其用大括号括起来:
{%}

加号(+)、插入符号(^)、百分号(%)、波浪号(~)和括号()对SendKeys有特殊意义。若要指定其中一个字符,请将其括在大括号({})内。例如,若要指定加号,请使用“{+}”。若要指定大括号字符,请使用“{{}”和“{}”。方括号([])对SendKeys没有特殊意义,但必须将它们括在大括号中。在其他应用程序中,大括号确实具有特殊意义,在发生动态数据交换(DDE)时可能非常重要


我将此函数用于VBS,它很容易理解它在做什么,只需移植到您的首选语言…使用您的文本字符串调用,即对于VBS:

strText=“My%Text~带+Escape^Chars”

strText=EscapeChars(strText)

您将得到一个返回字符串,该字符串在
%、~、+、
^
之间用括号括起来,在SendKeys中非常有效,即
“My{%}Text{}和{+}Escape{^}字符”


感谢Rob,这是它成功的唯一原因,我已经测试过: % ) ( 女巫是最容易发生的

我必须修改你的代码
GREP
部分对我不起作用,所以我做了以下工作:

    Function EscapeChars(vStr)
    On Error Resume Next
    For i = 1 to Len(vStr)
        If "%" = Mid(vStr, i, 1)_
           Or "+" = Mid(vStr, i, 1)_
           Or "^" = Mid(vStr, i, 1)_
           Or "!" = Mid(vStr, i, 1)_
           Or "(" = Mid(vStr, i, 1)_
           Or ")" = Mid(vStr, i, 1)_
           Or "[" = Mid(vStr, i, 1)_
           Or "]" = Mid(vStr, i, 1)_
           Or "~" = Mid(vStr, i, 1) Then
            EscapeChars = EscapeChars & "{" & Mid(vStr, i, 1) & "}"
        Else
            EscapeChars = EscapeChars & Mid(vStr, i, 1)
        End If
    Next
    End Function
基本上,我只需要输入大量的
语句,效果非常好,谢谢

    Function EscapeChars(vStr)
    On Error Resume Next
    For i = 1 to Len(vStr)
        If "%" = Mid(vStr, i, 1)_
           Or "+" = Mid(vStr, i, 1)_
           Or "^" = Mid(vStr, i, 1)_
           Or "!" = Mid(vStr, i, 1)_
           Or "(" = Mid(vStr, i, 1)_
           Or ")" = Mid(vStr, i, 1)_
           Or "[" = Mid(vStr, i, 1)_
           Or "]" = Mid(vStr, i, 1)_
           Or "~" = Mid(vStr, i, 1) Then
            EscapeChars = EscapeChars & "{" & Mid(vStr, i, 1) & "}"
        Else
            EscapeChars = EscapeChars & Mid(vStr, i, 1)
        End If
    Next
    End Function