如何将 int 转换为 LPARAM C/C++

how to convert int to LPARAM c/c++?

本文关键字:LPARAM C++ 转换 int      更新时间:2023-10-16

这是我尝试过的。它构建和运行良好,但打印空白。

功能:

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::stringstreamstd::to_string将其转换为std::string,然后使用c_str()获取它。

我是这样做的:

LPARAM myLParam;
int myInt;
myLParam = (LPARAM) myInt; // This where the int is converted to the LPARAM