How can I prevent a window from being deactivated?

痴心易碎 提交于 2019-12-11 16:44:34

问题


Is there any way to prevent a window from being deactivated? The window is in a different process then mine.

This is for Windows.


回答1:


Doing this can be dangerous, but the solution is to handle the WM_ACTIVATE message and check if the wParam is WA_INACTIVE. This means the window has been deactivated. When this happens, you can just reactivate it.

In order to do this for another process's window, you will need to install a message hook with SetWindowsHookEx.

However, it is possible that another application could do the same thing, putting each other in an endless loop of activation/deactivation.

This is also something that should never be done by software that is meant to run on a personal computer.




回答2:


You can trap the WM_ACTIVATEAPP like this:

protected override void WndProc(ref Message m) {
  // Trap WM_ACTIVATEAPP
  if ((m.Msg == 0x1c) && (m.WParam == IntPtr.Zero))
  {
     // If WM_ACTIVATEAPP and WParam is deactivated, return
     return;
  }
  base.WndProc(ref m);
}


来源:https://stackoverflow.com/questions/5142515/how-can-i-prevent-a-window-from-being-deactivated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!