代码之家  ›  专栏  ›  技术社区  ›  Kirschstein

外部应用程序中文本框的SetText。Win32 API

  •  4
  • Kirschstein  · 技术社区  · 16 年前

    使用Winspector,我发现要更改的子文本框的ID为114。为什么这个代码不改变文本框的文本?

        [DllImport("user32.dll")]
        static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);
    
        const int WM_SETTEXT = 0x000c;
    
        private void SetTextt(IntPtr hWnd, string text)
        {
            IntPtr boxHwnd = GetDlgItem(hWnd, 114);
            SendMessage(boxHwnd, WM_SETTEXT, 0, text);
        }
    
    4 回复  |  直到 16 年前
        1
  •  8
  •   Gregyski    16 年前

    以下是我在删除/禁用my GetLastError错误检查的情况下成功用于此目的的内容:

    [DllImport("user32.dll", SetLastError = false)]
    public static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
    public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
    public const uint WM_SETTEXT = 0x000C;
    
    private void InteropSetText(IntPtr iptrHWndDialog, int iControlID, string strTextToSet)
    {
        IntPtr iptrHWndControl = GetDlgItem(iptrHWndDialog, iControlID);
        HandleRef hrefHWndTarget = new HandleRef(null, iptrHWndControl);
        SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, strTextToSet);
    }
    

    我还不能在帖子中对使用(char*)发表评论,但这不是必须的。请参见中的第二个C#重载 p/Invoke SendMessage . 您可以将字符串或StringBuilder直接传递到SendMessage中。

    我还注意到你说你的控制ID是114。你确定WinSpector给了你10进制的值吗?因为您正在将它作为一个以10为基数的数字馈送给GetDlgItem。我使用Spy++来实现这一点,它在base 16中返回控件ID。在这种情况下,您将使用:

    IntPtr boxHwnd = GetDlgItem(hWnd, 0x0114);
    
        2
  •  2
  •   Chandrasekhar Telkapalli    9 年前


    [DllImport("user32.dll")]
    static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);
    [DllImport("user32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int msg, int Param, string s);
    
    const int WM_SETTEXT = 0x000c;
    
    private void SetTextt(IntPtr hWnd, string text)
    {
        IntPtr boxHwnd = GetDlgItem(hWnd, 114);
        SendMessage(boxHwnd, WM_SETTEXT, 0, text);
    }
    
        3
  •  0
  •   Community Mohan Dere    8 年前

    你确定你传递的文本正确吗?SendMessage last param应该是指向包含要设置的文本的char*的指针。
    看看我的“粗糙的黑客”设置文本 How to get selected cells from TDBGrid in Delphi 5
    这是在Delphi5中完成的,其中PChar是char*alias,我只是将其转换为int(Delphi中的整数)。

        4
  •  0
  •   Gautam Jain    11 年前