首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

窗口程序在失去焦点后未响应解决方案

2013-12-06 
窗口程序在失去焦点后未响应我有一个程序,在VS下运行没有一点问题,但是如果独立运行,不管是Debug版还是Rel

窗口程序在失去焦点后未响应
我有一个程序,在VS下运行没有一点问题,但是如果独立运行,不管是Debug版还是Release版,在启动界面以及开始菜单界面都不能让程序失去焦点,否则程序就会未响应,哪怕是一失去焦点就马上让程序获得焦点也不行,程序虽然不会未响应,但是却会变得极不稳定,程序必须要等进入主界面后才能够正常起来。不知道这是为什么?
主要现象就是:
启动程序后,在菜单界面下让程序失去焦点,大约1~2秒后程序就会未响应。
启动程序后,在菜单界面下让程序失去焦点,程序不会未响应,但是会变得极不稳定,比如有时窗口会像发地震一样抖动,但是我的程序里除了在创建完窗口后调用了一次MoveWindow,并没有在其他地方添加任何移动窗口的代码,再比如有时窗口的客户区会莫名其妙就透明了,但是窗体不透明,仅仅客户区那一块透明,搞得就好像在窗口上挖了个洞似的,以前想实现这样的功能都实现不了,现在到好了,不要这个功能的时候它却自动跑来了。再比如有时窗口会出现闪白现象,但是我是使用双缓冲来显示的,照理来说应该不会出现这种情况才对。当窗口不稳定的时候才会发生上述的现象。

而如果我在菜单界面保持焦点不丢失,进入到主界面后程序就正常了,我检查过我程序那前一部分的代码,感觉应该是没有什么问题才对,而且如果代码真有问题,为何在VS下运行时一点问题都没有。

求大神指点迷津。 Visual?Studio 调试 C\C++ 焦点 未响应
[解决办法]
我觉得是逻辑上有问题。用纸把你的整体思路缕一遍看一下。
然后假如通过vs启动没问题而之间点击exe文件启动出现问题的话,我想是不是因为vs运行过程中程序一直拥有焦点而且目前cpu的优先级最高的主任务就是你的程序,而点击可执行文件启动发生问题,我想可能是因为cpu仅仅使用了一个优先级比较低的线程处理你的任务,而且cpu忙着在电脑不同线程之间切换使得你的程序获得的焦点时断时续的,所以发生问题。
以上是我的一孔之见。
[解决办法]
SetForegroundWindow
The SetForegroundWindow function puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user. The system assigns a slightly higher priority to the thread that created the foreground window than it does to other threads. 

BOOL SetForegroundWindow(
  HWND hWnd   // handle to window to bring to foreground
);
 
Parameters
hWnd 
Handle to the window that should be activated and brought to the foreground. 
Return Values
If the window was brought to the foreground, the return value is nonzero.

If the window was not brought to the foreground, the return value is zero. 

Remarks
The foreground window is the window at the top of the Z order. It is the window that the user is working with. In a preemptive multitasking environment, you should generally let the user control which window is the foreground window. 

Windows NT 5.0 and later: An application cannot force a window to the foreground while the user is working with another window. Instead, SetForegroundWindow will activate the window (see SetActiveWindow) and call theFlashWindowEx function to notify the user.

Windows CE: The thread that owns the window is not given a priority boost. 

QuickInfo
  Windows NT: Requires version 3.1 or later.
  Windows: Requires Windows 95 or later.
  Windows CE: Requires version 1.0 or later.
  Header: Declared in winuser.h.
  Import Library: Use user32.lib.

See Also
Windows Overview, Window Functions,FlashWindowEx, GetForegroundWindow, SetActiveWindow

 

热点排行