首页  编辑  

已知Word的句柄,向其中输入一段文字

Tags: /C#/API调用/   Date Created:

using System . Runtime . InteropServices;

[ DllImport ( "User32.DLL" )]

public static extern int SendMessage( IntPtr hWnd, uint Msg, int wParam, int lParam);

public int MakeChar( char AChar)

{

    byte [] vBytes = Encoding . Default . GetBytes( new char [] { AChar });

    if (vBytes . Length > 1 )

   {

        return vBytes[ 1 ] | vBytes[ 0 ] << 8 ;

   }

    else return vBytes[ 0 ];

}

public void SendText( IntPtr AHandle, string AText)

{

    const int WM_IME_CHAR = 0x0286 ;

    foreach ( char vChar in AText)

       SendMessage(AHandle, WM_IME_CHAR, MakeChar(vChar), 0 );

}

private void button1_Click( object sender , EventArgs e)

{

    IntPtr vHandle = ( IntPtr ) 1379352 ;

   SendText(vHandle, "Zswang );

}