zoukankan      html  css  js  c++  java
  • C#向win32程序窗口中的文本框设置指定文本

    public partial class Form1 : Form
     {

    //设置文本内容的消息

    private const int WM_SETTEXT = 0x000C;

     //鼠标点击消息
    const int BM_CLICK = 0x00F5;[DllImport("user32.dll")]


    private static extern IntPtr FindWindow(
      string lpClassName,
      string lpWindowName);

    [DllImport("User32.dll")]
    private static extern IntPtr FindWindowEx(
      IntPtr hwndParent,
      IntPtr hwndChildAfter,
      string lpszClass,
    string lpszWindows);
    [DllImport("User32.dll")]
    private static extern Int32 SendMessage(
      IntPtr hWnd,       
      int Msg,       
      IntPtr wParam,      
    StringBuilder lParam);

            [DllImport("user32.dll ", CharSet = CharSet.Unicode)]
            public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
    pri}vate

     void button1_Click(object sender, EventArgs e)
    {
      // 返回写字板主窗口句柄
      IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad");
      if (!hWnd.Equals(IntPtr.Zero))
      {
        //返回写字板编辑窗口句柄
        IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
        if (!edithWnd.Equals(IntPtr.Zero))
          // 发送WM_SETTEXT 消息: "Hello World!"
          SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("Hello World!"));
      }
    }
    }

    另:

     Message msg = Message.Create(hwnd_button, BM_CLICK, new IntPtr(0), new IntPtr(0));
    //点击hwnd_button句柄对应的按钮
     PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);


     

  • 相关阅读:
    米洛个人修炼术:导致上班累成狗的三大主因
    米洛个人修炼术:如何倍增你的功力修行速度达成健康体魄
    米洛个人修炼术:注意这三方面,天天都早起
    米洛个人修炼术:情绪的四种常用处理方式,其实都是有问题的
    好公司和差公司的对比
    课程作业二
    课程作业一
    作业四
    寒假作业三
    寒假作业二
  • 原文地址:https://www.cnblogs.com/Rising/p/2276001.html
Copyright © 2011-2022 走看看