wsprintfW 只打印几十个? [英] wsprintfW printing only tens?

查看:24
本文介绍了wsprintfW 只打印几十个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个轨迹栏,在某些时候它的值应该改变文本:

I have a trackbar and at some point it's value is supposed to change a text:

case WM_HSCROLL:
    {
        LRESULT pos = SendMessage(trackBar, TBM_GETPOS, 0, 0);
        WCHAR buf[3];
        wsprintfW(buf, L"%ld", pos);

        SetWindowText(trackBarValue, (LPCSTR)buf);
    }
    break;

轨迹栏的范围从 15 到 35.出于某种原因,只有十位被打印到文本中(因为我的轨迹栏的值在 15 到 19 之间,文本为 1,当我的轨迹栏的值在 20 到 29 之间时,我的文字是 2,它变成了 3,因为我的轨迹栏的值在 30 到 35 之间.

The trackbar's range goes from 15 to 35. For some reason, only the tens gets printed to the text (As my trackbar's value is between 15 and 19, the text is 1, when my trackbar's value is between 20 and 29, my text is 2, and it becomes 3 as my trackbar's value is between 30 and 35.

当然,我希望我的文字显示我的轨迹栏的绝对值,而不仅仅是十位!

Of course, I want my text to show the absolute value of my trackbar, not only its tens!

我该怎么办?

编辑:运行调试器后,我知道 buf 确实由 trackbar 的值决定.问题似乎出在 SetWindowText 行上.

Edit: After running the debugger, I know that buf DOES become by trackbar's value. The problem seems to be with the SetWindowText line.

一种解决方案是将 SetWindowText 更改为 SetWindowTextW 并删除 (LPCSTR) 强制转换.谢谢大家.

One solution was to change SetWindowText to SetWindowTextW and remove the (LPCSTR) cast. Thanks people.

推荐答案

您将宽字符字符串转换为简单字符串,这是错误的.你必须使用 WideCharToMultiByte,像这样:

You are casting a wide char string to a simple string, which is wrong. You have to use WideCharToMultiByte, like this:

size_t size = WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);

CHAR *szTo = new CHAR[size];
WideCharToMultiByte(CP_ACP, 0, buf, -1, szTo, size, NULL, NULL);

// don't forget to delete[] szTo

或者,您可以将您的应用程序定义为 Unicode 兼容,因此 SetWindowText 宏将解析为 SetWindowTextW:

Alternatively, you could define your application as Unicode-compliant, so the SetWindowText macro will resolve to SetWindowTextW:

#define UNICODE
#define _UNICODE
#include <windows.h>

这篇关于wsprintfW 只打印几十个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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