奇怪的现象,GetFocus()在98下运行正确,2000返回错误2,搞不懂,代码一样

zhuwenzheng 2003-02-12 02:56:22
如上
...全文
33 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
everandforever 2003-02-13
  • 打赏
  • 举报
回复
Use the GetForegroundWindow function to retrieve the handle to the window with which the user is currently working.

GetActiveWindow 也可以试试啊。
zhdleo 2003-02-13
  • 打赏
  • 举报
回复
gz.
zhuwenzheng 2003-02-13
  • 打赏
  • 举报
回复
TO hnyyy(前进) :
就是说不一定能获得任何窗口的句柄了
zhuwenzheng 2003-02-13
  • 打赏
  • 举报
回复
那怎么办,我现在用钩子做了个DLL,在DLL中获得当前窗口的句柄,这样有问题吗?为什么98下可以呢?
hnyyy 2003-02-13
  • 打赏
  • 举报
回复
MSDN上说:

窗口焦点

在 Win32 中,每个执行线程只能设置或获取由当前线程创建的窗口的焦点。这样可防止应用程序相互影响。某个应用程序的响应延迟不能导致其他应用程序暂停它们对用户操作的响应,而在 Windows 3.x 中经常发生这种情况。

因此,在 Win32 中下列 API 函数以不同的方式运行:

GetActiveWindow( VOID ) SetActiveWindow( HWND )
GetCapture( VOID ) SetCapture( HWND )
GetFocus( VOID ) SetFocus( HWND )
ReleaseCapture( VOID )

上表中的 Get 函数现在能返回 NULL,这在 Windows 3.x 中是不会发生的。因此,使用 GetFocus 前测试它的返回值是非常重要的。该函数不是返回另一线程的窗口句柄,而是返回 NULL。例如,调用 GetFocus 而另一线程有焦点。请注意,即使前面的 SetFocus 调用成功设置了焦点,GetFocus 调用也可能返回 NULL。同样的注意事项适用于 GetCapture 和 GetActiveWindow。

Set 函数只能指定由当前线程创建的窗口。如果试图传递由另一线程创建的窗口句柄,Set 函数调用将失败。

zhuwenzheng 2003-02-13
  • 打赏
  • 举报
回复
TO hnyyy(前进) :
hwnd1=GetFocus()恰恰返回是NULL,不知道为什么,我想获得当前的工作窗口,我已经晕了,到底为什么
hnyyy 2003-02-13
  • 打赏
  • 举报
回复

hwnd1=GetFocus();
if(hwnd1 != NULL)
SendMessage(hwnd1, WM_VSCROLL, MAKELONG(SB_LINEDOWN, 0), 0L);


使用 GetFocus 前测试它的返回值是非常重要的。该函数不是返回另一线程的窗口句柄,而是返回 NULL。例如,调用 GetFocus 而另一线程有焦点。请注意,即使前面的 SetFocus 调用成功设置了焦点,GetFocus 调用也可能返回 NULL。

hnyyy 2003-02-13
  • 打赏
  • 举报
回复
到底用在何处呢?

hwnd1=::GetFocus();//无论用在何处,这样应该没问题
ripyu 2003-02-13
  • 打赏
  • 举报
回复
sdtudy
zhuwenzheng 2003-02-13
  • 打赏
  • 举报
回复
我的目的是使当前窗口的SCROLLBAR能被控制,想通过发WM_VSCROLL来实现,但是句柄老是有错
zhuwenzheng 2003-02-13
  • 打赏
  • 举报
回复
难道在2000下不能用键盘控制使得水平的SCROLLBAR滚动吗?
zhuwenzheng 2003-02-13
  • 打赏
  • 举报
回复
TO everandforever(Forever) :
我已经试过了,不可以
用户 昵称 2003-02-12
  • 打赏
  • 举报
回复
没遇到过,等待ing。
everandforever 2003-02-12
  • 打赏
  • 举报
回复
MSDN 没说 GETFOCUS()和SENDMESSAGE()会对 LASTERROR 产生影响.

GetFocus returns the window with the keyboard focus for the current thread's message queue. If GetFocus returns NULL, another thread's queue may be attached to a window that has the keyboard focus.

Use the GetForegroundWindow function to retrieve the handle to the window with which the user is currently working.
loopyifly 2003-02-12
  • 打赏
  • 举报
回复
来学习
zhuwenzheng 2003-02-12
  • 打赏
  • 举报
回复
SetLastError(0);
hwnd1=GetFocus();
SendMessage(hwnd1, WM_VSCROLL, MAKELONG(SB_LINEDOWN, 0), 0L);
sprintf(sz,"error=%d ",GetLastError());
TextOut(GetWindowDC(0),100,200,sz,sizeof(sz));
这样的话GETLASTERROR返回1400,说句柄无效
用GetForegroundWindow()可以,但是SCROLLBAR功能没有
everandforever 2003-02-12
  • 打赏
  • 举报
回复
SetLastError(0);
GetFocus()
GetLastError()

看看
zhaolaoxin 2003-02-12
  • 打赏
  • 举报
回复
此时用GETLASTERROR()得到的不一定是GetFocus()的错误值
zhuwenzheng 2003-02-12
  • 打赏
  • 举报
回复
就是GetLastError()返回值
aben456 2003-02-12
  • 打赏
  • 举报
回复
GetFocus
The GetFocus function retrieves the handle to the window that has the keyboard focus, if the window is attached to the calling thread's message queue.

HWND GetFocus(VOID);
Parameters
This function has no parameters.

Return Values
The return value is the handle to the window with the keyboard focus. If the calling thread's message queue does not have an associated window with the keyboard focus, the return value is NULL.

Remarks
GetFocus returns the window with the keyboard focus for the current thread's message queue. If GetFocus returns NULL, another thread's queue may be attached to a window that has the keyboard focus.

Use the GetForegroundWindow function to retrieve the handle to the window with which the user is currently working. You can associate your thread's message queue with the windows owned by another thread by using the AttachThreadInput function.

Windows 98 and Windows NT 4.0 SP3 and later: To get the window with the keyboard focus on the foreground queue or the queue of another thread, use the GetGUIThreadInfo function.

Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winuser.h; include Windows.h.
Library: Use User32.lib.

只说会返回NULL,怎么会返回2呢
难道是GETLASTERROR()?
加载更多回复(1)

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧