为什么获取当前对话框的ID总是失败???

kewencommon 2010-03-25 02:43:59
我想在当前对话框的一个按钮内获取当前对话框的ID,但是总是返回0,
代码简单如下:
long ID;
int nError;
ID = GetWindowLong(GetSafeHwnd(),GWL_ID);
nError = GetLastError();
或者
ID = GetWindowLong(this->m_hWnd,GWL_ID);


ID和nError 总是返回0.
nError返回0,应该是表示GetWindowLong函数操作成功啊。为什么nError返回0啊??
...全文
541 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wingsluo2 2011-03-25
  • 打赏
  • 举报
回复
this->IDD
安全方案 2010-04-08
  • 打赏
  • 举报
回复
呵呵 有错撒
kewencommon 2010-04-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 shenqiabing 的回复:]
对话框不能用这个 你直接用CYourDlg::IDD 就ok了
[/Quote]

这个知道,如果我不知道类名呢?
就是不知道CYourDlg。那如何获取当前对话框的ID值?
Eleven 2010-03-29
  • 打赏
  • 举报
回复
同上。。。
shenqiabing 2010-03-29
  • 打赏
  • 举报
回复
对话框不能用这个 你直接用CYourDlg::IDD 就ok了
I_NBFA 2010-03-27
  • 打赏
  • 举报
回复
The GetDlgCtrlID function retrieves the identifier of the specified control.

Syntax

int GetDlgCtrlID( HWND hwndCtl
);
Parameters

hwndCtl
[in] Handle to the control.
Return Value

If the function succeeds, the return value is the identifier of the control.

If the function fails, the return value is zero. An invalid value for the hwndCtl parameter, for example, will cause the function to fail. To get extended error information, call GetLastError.




Remarks

GetDlgCtrlID accepts child window handles as well as handles of controls in dialog boxes. An application sets the identifier for a child window when it creates the window by assigning the identifier value to the hmenu parameter when calling the CreateWindow or CreateWindowEx function.

Although GetDlgCtrlID may return a value if hwndCtl is a handle to a top-level window, top-level windows cannot have identifiers and such a return value is never valid.

我记得top level窗口没有ID, 也没必要使用ID.
不过先用SetWindowLong设置下也能用, 好象是这样哦.
zxdyu2009 2010-03-27
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 kewencommon 的回复:]
对于9楼:
我也认为你这种说法,但是为什么我上面的做法无法获取ID值呢??
[/Quote]
我也想得到,也是0,可能真是LS说的POPUP得不到ID,只能安慰你了,呵呵。
schlafenhamster 2010-03-27
  • 打赏
  • 举报
回复
if (dwStyle & WS_POPUP)// Dlg 是 “POPUP” 得不到ID
wsprintf (szTemp, "%08lX: WS_POPUP", dwStyle);
else if (dwStyle & WS_CHILD)// 只有 "CHILD" 才可以得到IDwsprintf (szTemp, "%08lX: WS_CHILD, ID: %lX", dwStyle,GetWindowLong(hwnd, GWL_ID));
kewencommon 2010-03-27
  • 打赏
  • 举报
回复
对于9楼:
我也认为你这种说法,但是为什么我上面的做法无法获取ID值呢??

zxdyu2009 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 huliang66 的回复:]
msdn上说:The following values are also available when the hWnd parameter identifies a dialog box
我的理解是当HWND hWnd,是一个对话框的时候,int nIndex 只能取下面的值,楼主看看MSDN
[/Quote]
MSDN中这是样描述的
The following values are also available when the hWnd parameter identifies a dialog box. Value Action
DWL_DLGPROC Retrieves the address of the dialog box procedure, or a handle representing the address of the dialog box procedure. You must use the CallWindowProc function to call the dialog box procedure.
DWL_MSGRESULT Retrieves the return value of a message processed in the dialog box procedure.
DWL_USER Retrieves extra information private to the application, such as handles or pointers.
是也有效,不是只有
huliang66 2010-03-26
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 kewencommon 的回复:]
谢谢上面各位,再问一下
如果已经知道一个对话框的ID值,如何转换为对应的ID名字。
比如:#define IDD_S0000P000000 10009 /*投保单*/
我已经可以获得对话框ID值10009,,我现在想转换为IDD_S0000P000000,,大家知道有什么方法吗??
[/Quote]

没法得到,在程序里面ID和IDD_S0000P000000这个是相同的,没必要非的要那个吧。
kewencommon 2010-03-26
  • 打赏
  • 举报
回复
谢谢上面各位,再问一下
如果已经知道一个对话框的ID值,如何转换为对应的ID名字。
比如:#define IDD_S0000P000000 10009 /*投保单*/
我已经可以获得对话框ID值10009,,我现在想转换为IDD_S0000P000000,,大家知道有什么方法吗??
  • 打赏
  • 举报
回复
对话框不能用这个 你直接用CYourDlg::IDD 就ok了
huliang66 2010-03-25
  • 打赏
  • 举报
回复
msdn上说:The following values are also available when the hWnd parameter identifies a dialog box
我的理解是当HWND hWnd,是一个对话框的时候,int nIndex 只能取下面的值,楼主看看MSDN
zgl7903 2010-03-25
  • 打赏
  • 举报
回复
用SPY++看看 能获取到ID不?

当前对话框的ID在头文件里就有定义了 IDD,
但如果这是个App窗口,此ID只是在初始化时用来标识资源位置的,其它状况下无用
kewencommon 2010-03-25
  • 打赏
  • 举报
回复
对于1楼,你的资料我已经看过,我想问一下,你解析一下我的问题出现的原因吗?谢谢
对于2楼,GetDlgCtrlID函数返回的是子窗口的ID吧?
luckyboy1979 2010-03-25
  • 打赏
  • 举报
回复
试试CWnd::GetDlgCtrlID()函数
huliang66 2010-03-25
  • 打赏
  • 举报
回复
当hWnd标识一个对话框时可以使用下面的值


函数原型:
LONG GetWindowLong( HWND hWnd,int nIndex )

参数:
hWnd:指定窗口的句柄
nIndex:需要获得的信息的类型
值 功能

nIndex取值如下:
GWL_EXSTYLE 得到扩展的窗口风格
GWL_STYLE 得到窗口风格
GWL_WNDPROC 得到窗口回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用
GWL_HINSTANCE 得到应用程序运行实例的句柄
GWL_HWNDPARENT 得到父窗口的句柄
GWL_ID 得到窗口的标识符
GWL_USERDATA 得到和窗口相关联的32位的值(每一个窗口都有一个有意留给创建窗口的应用程序是用的32位
的值)

当hWnd标识一个对话框时可以使用下面的值
Value Action
DWL_DLGPROC 得到对话框回调函数的地址,或者句柄。得到后必须使用CallWindowProc函数来调用
DWL_MSGRESULT 得到对话框回调函数中消息处理过程的返回值
DWL_USER 得到额外的应用程序私有信息,如一些句柄和指针等

返回值:
成功时,返回一个请求的32位的值
失败时,返回0,可以使用GetLastError来取得错误信息
http://blog.csdn.net/hnhyhongmingjiang/archive/2008/03/06/2154410.aspx

16,471

社区成员

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

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

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