什么是INT_PTR以及如何将数据转换为INT_PTR?

时间:2015-10-04 07:25:44

标签: c++ windows

我正在尝试使用Windows API提供的TBBUTTON结构来创建工具栏按钮。当然我尝试在这些按钮上添加一些文字,因此我设置了iString TBBUTTON结构的INT_PTR成员,其类型为typedef struct { int iBitmap; int idCommand; BYTE fsState; BYTE fsStyle; #ifdef _WIN64 BYTE bReserved[6]; #else #if defined(_WIN32) BYTE bReserved[2]; #endif #endif DWORD_PTR dwData; INT_PTR iString; } TBBUTTON, *PTBBUTTON, *LPTBBUTTON;

TBBUTTON tbButton = { MAKELONG(STD_FILENEW,  ImageListID), IDM_NEW,  TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"New" };

MSDN上有example初始化该结构:

(INT_PTR)L"File"

注意那里的INT_PTR转换。当我做同样的事情时,我会看到一些奇怪的字符串:

我在MSDN上阅读了有关INT_PTR的文档,但我仍然不理解它,因为不知怎的,它适用于它们但不适合我......

那么如何将该Unicode字符串转换为<form name="articleForm" data-ng-submit="update(articleForm.$valid)" >

2 个答案:

答案 0 :(得分:1)

这是在MFC下的MSDN上记录的。 Toolbar类在技术上不是Windows API(Win32)的一部分,但实际上是MFC。 https://msdn.microsoft.com/en-us/library/bdabdzxd.aspx

它说:

  

iString

     

用作按钮标签的字符串的从零开始的索引,如果此按钮没有字符串,则为-1。您提供的索引的图像和/或字符串必须先前已使用AddBitmapAddString和/或AddStrings添加到工具栏控件的列表中。

因此,转换字符串是行不通的(并且在C ++中总是更喜欢C ++风格的转换操作符而不是C风格的转换)。

AddString本身expects a Resource ID而不是字符串文字。使用AddStrings来使用内存中的字符串。

所以你的代码应该是这样的(伪代码):

LPCTSTR buttonTexts = L"Button1\0Button2\0"; // single buffer containing multiple null-terminated strings, and must end with two \0 (note the last null is implicit).

CToolbarCtrl* toolbar = ...
int addStringResult = toolbar->AddStrings( buttonTexts );
if( addStringResult == -1 ) die();

TBBUTTON buttons[] = {
    { /* ... */ iString: 0 },
    { /* ... */ iString: 1 }
};
BOOL addButtonResult = toolbar->AddButtons( 2, &buttons );
if( !addButtonResult ) die();

答案 1 :(得分:1)

INT_PTR是一个整数类型,但保证它具有指针的大小。使用reinterpret_cast,您可以在指针类型和INT_PTR之间进行转换。

现在,关于你关于Unicode字符串的问题,我不知道,因为我不知道你所拥有的字符串的代表性。您必须提供更多信息。