下面代码是在.net中调试通过的,非常好用!我需要在vc++6。0中实现它,请各路老兄指点,帮我转换一下!是把一字符串发送到另一窗体的textBox1中!///////////////////////////////////////////////////
const int WM_CHAR = 0x0102;
IntPtr hwnd_win ;
IntPtr hwnd_textbox ;
hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");
hwnd_textbox = FindWindowEx(hwnd_win ,new IntPtr(0) ,"WindowsForms10.EDIT.app3","textBox1");
string strtext = "测试aaa";
UnicodeEncoding encode = new UnicodeEncoding();
char[] chars = encode.GetChars(encode.GetBytes(strtext));
Message msg ;
foreach (char c in chars )
{
msg = Message.Create(hwnd_textbox ,WM_CHAR ,new IntPtr(c),new IntPtr(0));
PostMessage(msg.HWnd ,msg.Msg ,msg.WParam ,msg.LParam);
} /////////////////////////////////////
thanks!!
const int WM_CHAR = 0x0102;
IntPtr hwnd_win ;
IntPtr hwnd_textbox ;
hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");
hwnd_textbox = FindWindowEx(hwnd_win ,new IntPtr(0) ,"WindowsForms10.EDIT.app3","textBox1");
string strtext = "测试aaa";
UnicodeEncoding encode = new UnicodeEncoding();
char[] chars = encode.GetChars(encode.GetBytes(strtext));
Message msg ;
foreach (char c in chars )
{
msg = Message.Create(hwnd_textbox ,WM_CHAR ,new IntPtr(c),new IntPtr(0));
PostMessage(msg.HWnd ,msg.Msg ,msg.WParam ,msg.LParam);
} /////////////////////////////////////
thanks!!
IntPtr hwnd_textbox ;
========》
HWND hwnd_win ;
HWND hwnd_textbox ;foreach (char c in chars )
===》
for (int i=0; i<strlen(chars); i++ )
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemtextunicodeencodingclassctortopic1.asp
{
HWND hwnd_win ;
HWND hwnd_textbox
hwnd_win = (HWND)FindWindow("WindowsForms10.Window.8.app3","Form1");
//hwnd_win=FindWindowEx(hwnd_win,0,"WindowsForms10.Window.8.app3",0);
hwnd_textbox = FindWindowEx(hwnd_win, 0,"WindowsForms10.EDIT.app3","textBox1"); } 总是找不到hwnd_textbox, hwnd_textbox为0x0000000000...... 但我放在如下的WinMain中能找到! int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hwnd_win ;
HWND hwnd_textbox;
hwnd_win = FindWindow("WindowsForms10.Window.8.app3","Form1");
// hwnd_win=FindWindowEx(hwnd_win,0,"WindowsForms10.Window.8.app3",0);
hwnd_textbox=FindWindowEx(hwnd_win, 0,"WindowsForms10.EDIT.app3","textBox1"); } 能找到!hwnd_textbox不为空了!我需要在 void CPostMsgDlg::OnOKButton() 中加什方法吗?
use
pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);
pBoxOne->m_hWnd is windows handle.
不是窗口class name 变化的原因,是不是OnOKButton(){}中消息循环过不来的原因?
FindWindow改为::FindWindow试试。
hwnd_text = ::FindWindow("WindowsForms10.Window.8.app3","Form1");
hwnd_text=::FindWindowEx(hwnd_text, 0,"WindowsForms10.EDIT.app3","textBox1"); char str_MyItemText[12]={0};
strcpy(str_MyItemText,"C:\\sfq\\data\\20060112.xml"); for (int i=0; i<strlen(str_MyItemText); i++ )
::PostMessage(hwnd_text ,WM_CHAR ,(WPARAM)str_MyItemText[i],0);PostMessage前不加::报错!!加了是否有问题?
在这里应该加上。
MSDN的说法:
lParam
Specifies the repeat count, scan code, extended-key flag, context code, previous key-state flag, and transition-state flag, as shown in the following table.
0-15
Specifies the repeat count for the current message. The value is the number of times the keystroke is autorepeated as a result of the user holding down the key. If the keystroke is held long enough, multiple messages are sent. However, the repeat count is not cumulative.
16-23
Specifies the scan code. The value depends on the OEM.
24
Specifies whether the key is an extended key, such as the right-hand ALT and CTRL keys that appear on an enhanced 101- or 102-key keyboard. The value is 1 if it is an extended key; otherwise, it is 0.
25-28
Reserved; do not use.
29
Specifies the context code. The value is 1 if the ALT key is held down while the key is pressed; otherwise, the value is 0.
30
Specifies the previous key state. The value is 1 if the key is down before the message is sent, or it is 0 if the key is up.
31
Specifies the transition state. The value is 1 if the key is being released, or it is 0 if the key is being pressed.
strcpy(str_MyItemText,"C:\\sfq\\data\\20060112.xml"); hwnd_text的值不知为何变了! 还有问题:
我在下面的方法中执行完后:void CPostMsgDlg::OnOKButton()
{
HWND hwnd_text=0;
HWND hwnd_button=0; char str_MyItemText[12]={0};
strcpy(str_MyItemText,"C:\\sfq\\data\\20060112.xml"); hwnd_text = ::FindWindow("WindowsForms10.Window.8.app3","Form1");
hwnd_text=::FindWindowEx(hwnd_text, 0,"WindowsForms10.EDIT.app3","textBox1");
for (int i=0; i<strlen(str_MyItemText); i++ )
::PostMessage(hwnd_text ,WM_CHAR ,(WPARAM)str_MyItemText[i],0);
if (!hwnd_text)
::MessageBox(NULL,"Msg send!","Success£¡",NULL); else
::MessageBox(NULL,"Form1 not open!","Error£¡",NULL); //CloseHandle(hwnd_text);
//CloseHandle(hwnd_button);
} 老报如下错:
“Unhandled exception in Postmsg.exe:0XC0000000005:Access Violation.”
问题找出来了,是: if (!hwnd_text)
::MessageBox(NULL,"Msg send!","Success£¡",NULL);中hwnd_text句柄不知为什么自动丢了!
lResult = SendMessage(hWndControl,WM_SETTEXT, wParam,(LPARAM)str_MyItemText);