1

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

功能:

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

4

3 回答 3

2

WM_SETTEXT期望lParam指向一个 -0终止的字符数组。在那里放置一个整数是没有意义的。

WM_SETTEXT上面链接的文档中:

参数

指向作为窗口文本的以 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 环境中编译。

于 2013-09-08T11:30:35.020 回答
1

LPARAM 应该是空终止字符数组的地址,而不是数字。您想将您的表示转换为这样的数组。可能的方法是:

于 2013-09-08T11:33:26.140 回答
0

我就是这样做的:

LPARAM myLParam;
int myInt;
myLParam = (LPARAM) myInt; // This where the int is converted to the LPARAM
于 2013-09-10T15:30:08.010 回答