小弟需要在VB程序中控制另一个程序的行为,具体的流程是在主程序中调用另一个程序(简称B)如果B程序没有运行则调用B程序,如果B程序在运行则使B程序获得焦点(即使得B程序称为当前窗口)代码如下
smswinhwnd=FindWindow(vbNullString,"SmsRCS")  '
if smswinhwnd<>0 then
   PostMessage smswinhwnd,WM_SETFOCUS+WM_SHOWWINDOW,0,0
else
   shell "C:\MYCODE\SmsRCS.exe",vbMaximizedFocus
endif问题出现在PostMessage语句上,不论我在主程序中怎么调用,总是不能把已经运行的SmsRCS.exe程序切成当前窗口,请大家帮帮我。

解决方案 »

  1.   

    Private Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Longsmswinhwnd=FindWindow(vbNullString,"SmsRCS")  '
    if smswinhwnd<>0 then
       SetActiveWindow smswinhwnd
    else
       shell "C:\MYCODE\SmsRCS.exe",vbMaximizedFocus
    endif
      

  2.   

    palmkey:试过了还是不行代码如下 (注:mycodel是全局变量)private sub Command1_Click()
     if mycodel <>0 then
       setActiveWindow mycodel    '这个语句执行了但是没有效果,
     else
       shell "e:\temp\mycode.exe",vbMaximizedFocus
       mycodel=FindWindow(vbNullString,"mycode")
     endif
    End Subprivate Sub Form_Unload(Cancel as Integer)
       PostMessage mycodel ,&H10,0,0           '这个语句可以正常执行
    End Sub
      

  3.   

    你好像没有执行:FindWindow(vbNullString,"SmsRCS")
      

  4.   

    Private Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
    Private Declare Function SetActiveWindow Lib "user32" (ByVal hwnd As Long) As Longsmswinhwnd=FindWindow(vbNullString,"SmsRCS")if smswinhwnd<>0 then
       me.hide
       ShowWindow smswinhwnd,1
       SetActiveWindow smswinhwnd
    else
       shell "C:\MYCODE\SmsRCS.exe",vbMaximizedFocus
       me.hide
    endif