如何从一个返回整数的函数返回HWND。 [英] How to return HWND from a function which is designed to return integer.

查看:109
本文介绍了如何从一个返回整数的函数返回HWND。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个函数被设计为返回整数类型,但我需要返回一个窗口的HWND(一个窗口的句柄)相同的功能。



我想按如下方式进行投射。但是我没有按预期看到结果。

我在转换时遇到了什么错误吗?



如果您有任何想法,请告诉我。



提前致谢。



问候,

Joy

Hi All,

I have a function is designed to return integer type, but I need to return HWND ( handle of an window) of an window from the same function.

I am trying to cast as below.However I am not seeing results as expected.
Am I doing somthing somthing wrong in conversion?

Please let me know, if you have any idea.

Thanks in advance.

Regards,
Joy

推荐答案

在大多数情况下,你不需要这样的情况。我可以解释一下:在用户大小上,HWND没有算术运算;它们是一些抽象的指针;你只从Windows API获取HWND值并将HWND作为其他API函数的参数传递,从不在用户端进行任何计算。



但是,有很少见在发送或处理消息时需要的情况,例如在HWND和LPARAM之间进行转换。然后,做一个类型转换。



无论如何,学习C ++类型转换是必须的,这是编程中非常重要的一部分:http://www.cplusplus.com/doc/tutorial/typecasting/ [ ^ ]。



-SA
In most cases, you don''t need such case. I can explain: on the user size, there is no arithmetic operations with HWND; they are some abstract pointers; you only get HWND values from Windows API and pass HWND as parameters of other API function, never do any calculations with then on the user side.

However, there are rare cases when it is needed, such as casting between HWND and LPARAM, when sending or handling messages. Then, well, make a type cast.

Anyway, it''s a must to learn C++ typecast, which is very important part of programming: http://www.cplusplus.com/doc/tutorial/typecasting/[^].

—SA






最坏的情况是使用类型铸造。在这种情况下,您必须非常小心数据类型和您使用的转换,因为可能会损坏值。



更好的解决方案是修改函数/方法,以便它在输出参数中返回HWND:

Hi,

the worst case is to use a type casting. In this case you have to be very carefull about the data types and the conversion that you use, because of possible corruption of the value.

Much better sollution would be to modify the function/method, so that it will return the HWND in an output parameter:
int WonderfullMethod(HWND& outWnd)
{
   // the integer to return in the end
   int   iRetVal = 0;
   HWND  someWnd;

   // some code modifying the local variables declared above
   // ...

   // before we return, assign the value to the output parameter
   outWnd = someWnd;

   return iRetVal; 
}



在这种情况下,该方法将返回一个整数,并使用HWND值填充输出参数。



希望这会有所帮助。



祝你好运,

JK


In this case, the method will return an integer, and fill the output parameter with the HWND value.

Hope this helps.

Best regards,
J. K.


这篇关于如何从一个返回整数的函数返回HWND。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆