如何将 int 转换为 LPARAM C/C++
how to convert int to LPARAM c/c++?
这是我尝试过的。它构建和运行良好,但打印空白。
功能:
void wmessage(LPARAM msg, HWND hwnd)
{
SendMessage(hwnd,
WM_SETTEXT,
NULL,
msg);
}
用法:
//wmessage((LPARAM)"Not logged in22", noEdit); //prints
//wmessage((LPARAM)(t - clock()), noEdit); //prints blank
//wmessage((LPARAM)(555), noEdit); //prints blank
int num= (t - clock()); // t is a clock_t variable
wmessage((LPARAM)num, noEdit); //prints blank
所以我搜索了一下,但我似乎找不到任何关于如何做到这一点的提及。
目的是让此文本框在倒计时时打印一个以秒为单位的时间,因此它需要是一个 int
>WM_SETTEXT
期望lParam
指向以0
结尾的字符数组。在其中放置整数没有意义。
来自上面链接的WM_SETTEXT
的文档:
l帕拉姆
指向以 null 结尾的字符串(即窗口文本)的指针。
要将文本设置为"555",您可能希望这样做
char * txt = "555";
wmessage((LPARAM) txt, <some window handle>);
如果要将数值变量设置为文本,请将其转换为所需的文本表示形式。有几种方法可以做到这一点。使用sprintf()
是最灵活的方法:
#include <time.h> /* for clock_t, clock() */
#include <stdio.h> /* for sprintf() */
clock_t t = <some value>;
clock_t num = (t - clock());
char buffer [16] = "";
sprintf(buffer, "%ld", num);
wmessage((LPARAM) buffer, <some window handle>);
应该注意的是,这个答案的例子不是在 unicode 环境中编译的。
> LPARAM 应该是以 null 结尾的字符数组的地址,而不是数字。您希望将表示转换为这样的数组。可能的方法是:
- 使用
std::sprintf
将 int 转换为 char 数组,然后传递 char 数组。 - 使用
std::stringstream
或std::to_string
将其转换为std::string
,然后使用c_str()
获取它。
我是这样做的:
LPARAM myLParam;
int myInt;
myLParam = (LPARAM) myInt; // This where the int is converted to the LPARAM
相关文章:
- 收到消息时,lParam 的行为是什么WM_KEYDOWN?
- 将双倍转换为 LPARAM 的麻烦
- 如何将lparam值转换为我传递的类型
- wstring to LPARAM and WPARAM
- Windows编程:为什么我们要将lParam转换为CREATESTRUCT来获取应用程序状态
- 如何使用TreeView_SetItem更改 lParam
- PostMessage()的LPARAM是如何构建的
- 我如何使用这些帖子消息Lparam和Wparam的?
- 如何将 LPARAM lParam 转换为 CString
- Lparam不断变化TVITEM
- TVItem lparam存储字符串
- CPP,Winapi -WM_Create从lparam获得createstruct*的正确方法
- Win32:将lParam传递给CreateWindow并跟踪打开的窗口
- 将指针'this'传递为 LPARAM
- C++ wParam and Lparam confusion
- LPARAM在LB或CB中输出chines字符
- 从存储在 CTreeCtrl 的 LPARAM 中的结构中检索地址/指针 (IXMLDOMNode*) - 不起作用
- 传递额外的 wParam/lParam 参数
- 如何将 int 转换为 LPARAM C/C++
- win32(windows.h)中是否有一个方便函数可以将lParam转换为POINT