GetWindowTextA,GetWindowText在Edit Control上返回空值 [英] GetWindowTextA, GetWindowText returns empty value on Edit Control

查看:2526
本文介绍了GetWindowTextA,GetWindowText在Edit Control上返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我调用GetWindowText或者调用GetWindowText时,我会尝试从C ++ / Java中的外部窗口列出并获取编辑控件的内容。 GetWindowTextA它返回一个空的值在Edit Controls,我知道GetWindowText / GetWindowTextW和GetWindowTextA之间有一些差异,但我不知道我做错了,因为它适用于所有其他控件。



这里是C ++代码:

  BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
cout<<---------- CHILD ------------<< endl;
char class_name [80];
char title [80];
GetClassName(hwnd,class_name,sizeof(class_name));
GetWindowText(hwnd,title,sizeof(title));
cout<<\tWindow title:<< title<< endl;
cout<<\tClass name:<< class_name<< endl<< endl;
return TRUE;
}

Java代码:

  User32.INSTANCE.EnumChildWindows(hWnd,new User32.WNDENUMPROC(){
@Override
public boolean callback(Pointer hWnd,Pointer arg){
byte [] windowClassx = new byte [512];
User32.INSTANCE.GetClassNameA(hWnd,windowClassx,512);
String wClass = Native.toString(windowClassx);
System.out .println( - Found sub window / control class:+ new String(windowClassx).trim());
if(wClass.toLowerCase()。equals(edit)){
byte [] windowTextx = new byte [128];
user32.GetWindowText(hWnd,windowTextx,128);
String wText = Native.toString(windowTextx);
System.out.println );
}

return true;
}
},null);


解决方案

我不知道是什么原因导致你的问题,但这对我来说很好:

  byte [] windowText = new byte [512] 
User32.INSTANCE.GetWindowTextA(hWnd,windowText,512);
String wText = Native.toString(windowText).trim();






编辑 >
您应该使用不同的方式获得编辑文本,通过

  User32.SendMessageA(editHwnd,User32.WM_GETTEXT, paramWPARAM,lParamStr); 

例如

 code> import com.sun.jna.Native; 
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.win32.StdCallLibrary;

public class GetTextInNotePad {
public static final String NOTEPAD_CLASS =Notepad;
public static final String EDIT_CLASS =Edit;

接口User32扩展StdCallLibrary {
User32 INSTANCE =(User32)Native.loadLibrary(user32,User32.class);
int WM_SETTEXT = 0x000c;
int WM_GETTEXT = 0x000D;

HWND FindWindowA(String lpClassName,String lpWindowName);
HWND FindWindowExA(HWND hwndParent,HWND hwndChildAfter,String lpClassName,
String lpWindowName);
LRESULT SendMessageA(HWND paramHWND,int paramInt,WPARAM paramWPARAM,LPARAM paramLPARAM);
LRESULT SendMessageA(HWND editHwnd,int wmGettext,long l,byte [] lParamStr);
int GetClassNameA(HWND hWnd,byte [] lpString,int maxCount);
}

public static void main(String [] args){
User32 user32 = User32.INSTANCE;
String lpClassName =Notepad;
HWND notePadHwnd = user32.FindWindowA(lpClassName,null);
HWND editHwnd = user32.FindWindowExA(notePadHwnd,null,EDIT_CLASS,null);
byte [] lParamStr = new byte [512];
LRESULT resultBool = user32.SendMessageA(editHwnd,User32.WM_GETTEXT,512,lParamStr);

System.out.println(lParamStr:+ Native.toString(lParamStr));
}
}


I'm trying to list and get the content of Edit Controls from an external Window in C++ / Java, unfortunately with no success.

When I call GetWindowText or GetWindowTextA it returns an empty value on Edit Controls, I know there are some differences between GetWindowText / GetWindowTextW and GetWindowTextA but I don't know What I'm doing wrong since it works on all other controls.

Here the C++ Code:

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
    cout <<"----------CHILD------------"<<endl;
    char class_name[80];
    char title[80];
    GetClassName(hwnd,class_name, sizeof(class_name));
    GetWindowText(hwnd,title,sizeof(title));
    cout <<"\tWindow title: "<<title<<endl;
    cout <<"\tClass name: "<<class_name<<endl<<endl;
     return TRUE;
}

Java Code:

User32.INSTANCE.EnumChildWindows(hWnd, new User32.WNDENUMPROC() {
    @Override
    public boolean callback(Pointer   hWnd, Pointer   arg) {
        byte[] windowClassx = new byte[512];
        User32.INSTANCE.GetClassNameA(hWnd, windowClassx, 512);
        String wClass = Native.toString(windowClassx);
        System.out.println(" - Found sub window / control class: " + new String(windowClassx).trim());
        if (wClass.toLowerCase().equals("edit")){
            byte[] windowTextx = new byte[128];
            user32.GetWindowText(hWnd, windowTextx, 128);
            String wText = Native.toString(windowTextx);
            System.out.println(wText);
        }

        return true;
    }
}, null);  

解决方案

I'm not sure what is causing your problem, but this has worked just fine for me:

byte[] windowText = new byte[512];
User32.INSTANCE.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();


Edit
You should get the edit text a different way, via

User32.SendMessageA(editHwnd, User32.WM_GETTEXT, paramWPARAM, lParamStr);

e.g.,

import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.win32.StdCallLibrary;

public class GetTextInNotePad {
   public static final String NOTEPAD_CLASS = "Notepad";
   public static final String EDIT_CLASS = "Edit";

   interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      int WM_SETTEXT = 0x000c;
      int WM_GETTEXT = 0x000D;

      HWND FindWindowA(String lpClassName, String lpWindowName);
      HWND FindWindowExA(HWND hwndParent, HWND hwndChildAfter, String lpClassName,
            String lpWindowName);
      LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM, LPARAM paramLPARAM);
      LRESULT SendMessageA(HWND editHwnd, int wmGettext, long l, byte[] lParamStr);
      int GetClassNameA(HWND hWnd, byte[] lpString, int maxCount);
   }

   public static void main(String[] args) {
      User32 user32 = User32.INSTANCE;
      String lpClassName = "Notepad";
      HWND notePadHwnd = user32.FindWindowA(lpClassName , null);
      HWND editHwnd = user32.FindWindowExA(notePadHwnd, null, EDIT_CLASS, null); 
      byte[] lParamStr = new byte[512];
      LRESULT resultBool = user32.SendMessageA(editHwnd, User32.WM_GETTEXT, 512, lParamStr);

      System.out.println("lParamStr: " + Native.toString(lParamStr));
   }
}

这篇关于GetWindowTextA,GetWindowText在Edit Control上返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆