本人想用HOOK技术做了个热键切换任务栏中各窗口的小程序,
可发现 SetForegroundWindow(HWND hWnd)在WIN2000下不起作用。
以下是MSDN原文:
Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. 
请问各位高手:在WIN2K中,后台HOOK程序可运用什么方法中将下层的窗口
提到顶层显示出来呢?

解决方案 »

  1.   

    SetForegroundWindow will activate the window (see SetActiveWindow) and call the FlashWindowEx function to notify the user.
    Foreground and Background Windows
    Each process can have multiple threads of execution, and each thread can create windows. The thread that created the window with which the user is currently working is called the foreground thread, and the window is called the foreground window. All other threads are background threads, and the windows created by background threads are called background windows. Each thread has a priority level that determines the amount of CPU time the thread receives. Although an application can set the priority level of its threads, normally the foreground thread has a slightly higher priority level than the background threads. Because it has a higher priority, the foreground thread receives more CPU time than the background threads. The foreground thread has a normal base priority of 9; a background thread has a normal base priority of 7. The user sets the foreground window by clicking a window, or by using the ALT+TAB or ALT+ESC key combination. To retrieve a handle to the foreground window, use the GetForegroundWindow function. To check if your application window is the foreground window, compare the handle returned by GetForegroundWindow to that of your application window.An application sets the foreground window by using the SetForegroundWindow function. Windows NT 4.0 and earlier, Windows 95: If the new foreground window is a top-level window, the system activates it; otherwise, it activates the associated top-level window. Windows 98, Windows 2000: The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true: The process is the foreground process. 
    The process was started by the foreground process. 
    The process received the last input event. 
    There is no foreground process. 
    The foreground process is being debugged. 
    The foreground is not locked (see LockSetForegroundWindow). 
    The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo). 
    Windows 2000: No menus are active. 
    Windows 2000: A process that can set the foreground window can enable another process to set the foreground window by calling the AllowSetForegroundWindow function, or by calling the BroadcastSystemMessage function with the BSF_ALLOWSFW flag. The foreground process can disable calls to SetForegroundWindow by calling the LockSetForegroundWindow function. Built on Thursday, May 11, 2000
      

  2.   

    SetWindowPos(&wndTop,
    rectWindow.left,
    rectWindow.top,
    rectWindow.Width(),
    rectWindow.Height(),
    SWP_SHOWWINDOW | SWP_NOACTIVATE);
      

  3.   

    楼上大哥请看清题目先~~
    user32导出了一个未公开函数:
    void WINAPI SwitchToThisWindow (HWND hWnd, BOOL bRestore);
    第2个参数传TRUE.
    或者用公开的standard way:
    AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(hwnd, NULL), TRUE);
    SetForegroundWindow(hwnd);
    AttachThreadInput(GetCurrentThreadId(), GetWindowThreadProcessId(hwnd, NULL), FALSE);
      

  4.   

    首先感谢各位朋友的热心帮助。
    我使用 wormie4evr() 的第二种方法顺利完成了预想的功能。
    但是楼上两位老兄提供的某些api系统不认识,特在此想再问一问为什么。以下是vc6.0的debug error:
    E:\PROGRAM\KeyHook\KeyHook.cpp(102) : error C2065: 'SwitchToThisWindow' : undeclared identifierE:\PROGRAM\KeyHook\KeyHook.cpp(102) : error C2065: 'AllowSetForegroundWindow' : undeclared identifier
      

  5.   

    SwitchToThisWindow 是 undocumented 的, 需要自己写 LIB, 或 GetProcAddress + LoadLibrary 得到地址.AllowSetForegroundWindow 是前景窗口用来认证你的程序的, 因此不适用于你的问题.
      

  6.   

    非常感谢wormie4evr()的回复。
    1.运用GetProcAddress+LoadLibrary的方法我实现了undocumented API的调
      用,也使我以前碰到的这类问题得到了解决。真是万分感谢。
    2.但是小弟对自己写LIB,进行隐试调用的方法不了解,可否指点一二?
    3.很遗憾的说一声,SwitchToThisWindow还是不能实现原题中的目标功能,效果
      好像同SetForegroundWindow一样。下面是我的调用代码:
    void (WINAPI * m_pfnSwitchToThisWindow)(HWND,BOOL) = NULL;
    HINSTANCE m_hDll = LoadLibrary( "user32.DLL" );
    if(m_hDll)
       m_pfnSwitchToThisWindow = (void (WINAPI *)(HWND,BOOL))
                  GetProcAddress(m_hDll,"SwitchToThisWindow");
    if(m_pfnSwitchToThisWindow)
       (*m_pfnSwitchToThisWindow)(hwnd,TRUE);
      

  7.   

    freeshoot,
    回复迟了十分不好意思.
    我也试过了, SwitchToThisWindow 是有问题 ...2.但是小弟对自己写LIB,进行隐试调用的方法不了解,可否指点一二?
    ------------------
    这个我也不大清楚, VC 的 name mangling 很搞7搞8的. 用 def 可能可以, 建议去别的板块问问?