下面代码是在.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!!

解决方案 »

  1.   

    IntPtr hwnd_win ;
    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
      

  2.   

    谢谢!!问题一:   我把这几句放在void CPostMsgDlg::OnOKButton(){}中:   void CPostMsgDlg::OnOKButton() 
      {
          
          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() 中加什方法吗?
      

  3.   

    问题二:   vc++60中下一句怎么实现,不支持Create,报错了!!!    msg = Message.Create(hwnd_textbox ,WM_CHAR ,new IntPtr(c),new IntPtr(0));    谢谢!
      

  4.   

    PostMessage(hwnd_textbox ,WM_CHAR ,(WPARAM)c,0);
      

  5.   

    PostMessage(hwnd_textbox ,WM_CHAR ,(WPARAM)chars[i],0);
      

  6.   

    window class name changed in VC6.0
    use
    pBoxOne = (CEdit*) GetDlgItem(IDC_EDIT1);
    pBoxOne->m_hWnd is windows handle.
      

  7.   

    schwarzenegger
         不是窗口class name 变化的原因,是不是OnOKButton(){}中消息循环过不来的原因?
      

  8.   

    第一个问题
    FindWindow改为::FindWindow试试。
      

  9.   

    FindWindowEx改为::FindWindowEx试试
      

  10.   

    谢谢!::FindWindowEx写了可以去到非零数了,但消息确发不过去!!我是如下写的!////////////////////////////////////////
    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前不加::报错!!加了是否有问题?
      

  11.   

    加::表示是全局的,即系统API而不是被重载的函数。
    在这里应该加上。
      

  12.   

    按你的写法应该是发过去了。唯一不能确定的是你的LPARAM值是否正确。
    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.
      

  13.   

    感谢!::PostMessage没错,查出来了,是执行下两句时:          char str_MyItemText[12]={0};
    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&pound;&iexcl;",NULL); else
        ::MessageBox(NULL,"Form1 not open!","Error&pound;&iexcl;",NULL);   //CloseHandle(hwnd_text);
    //CloseHandle(hwnd_button);
    }  老报如下错:
       “Unhandled exception in Postmsg.exe:0XC0000000005:Access Violation.”
      
      

  14.   

    char str_MyItemText[50]={0};数组大一点,有点像越界。
      

  15.   

    感谢:
     问题找出来了,是:   if (!hwnd_text)
        ::MessageBox(NULL,"Msg send!","Success&pound;&iexcl;",NULL);中hwnd_text句柄不知为什么自动丢了!
      

  16.   

    wangk(倒之) ,大家帮忙看看问题三!
      

  17.   

    问题三:   我想一次发送一个字符串,不是一个字符循环发送,怎么发?char str_MyItemText[]="good Command!";
    lResult = SendMessage(hWndControl,WM_SETTEXT, wParam,(LPARAM)str_MyItemText);