Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;自定义按钮图标_C++_Winapi - Fatal编程技术网

C++ c++;自定义按钮图标

C++ c++;自定义按钮图标,c++,winapi,C++,Winapi,我想做一个动态宽度的按钮。 这是我的密码: CreateWindowEx(BS_PUSHBUTTON, "BUTTON", "OK", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 50, 220, 100, 24, hwnd, (HMENU)ID_BUTTON, GetModuleHandle(NULL), 0); 但是如果我把标签“OK”改

我想做一个动态宽度的按钮。 这是我的密码:

CreateWindowEx(BS_PUSHBUTTON, "BUTTON", "OK",
            WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
            50, 220, 100, 24, hwnd,
            (HMENU)ID_BUTTON,
            GetModuleHandle(NULL),
            0);
但是如果我把标签“OK”改为“更长的东西”,那么按钮就不够宽了。 如何设置动态宽度?

试试


好的,David,请下次提供更多信息,提及所有您不理解的内容,因为从您在评论中提出的问题,我可以推断您不仅不熟悉Win API,而且通常对C/C++编程也非常陌生

HWND buttonHandle = CreateWindowEx(BS_PUSHBUTTON, "BUTTON", "OK",
                                   WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
                                   50, 220, 100, 24,
                                   hwnd,
                                   (HMENU)ID_BUTTON,
                                   GetModuleHandle(NULL),
                                   0);
SIZE size;

if (!Button_GetIdealSize(buttonHandle, &size)) {
  // Call of `Button_GetIdealSize` failed, do proper error handling here.
  // For that you have various options:
  // 1. Exit current scope and return error code;
  // 2. Throw an exception;
  // 3. Terminate execution of your application and print an error message.
  // Of course these options can be mixed.
  // If you don't understand what I'm talking about here, then either skip this
  // check or start reading books on software development with C/C++.
}

// At this point `size` variable was filled with proper dimensions.
// Now we can use it to actually resize our button...

if (!MoveWindow(buttonHandle, 50, 220, (int)size.cx, (int)size.cy, TRUE)) {
  // Call of `MoveWindow` failed, do proper error handling here, again.
}

// We are done!

注意:您的问题的标题不正确。C++与按钮和Win API无关,特别是纯C。更好的标题应该是:Win API:如何正确调整按钮大小以适应其内容?

,我认为文档清楚地说明了如何使用它。宏有两个参数:按钮的句柄和指向
SIZE
structure的指针,当调用宏时,这些参数将填充适当的尺寸。因此,当您调用
CreateWindowEx
时,它返回了新创建按钮的句柄。现在将此句柄传递到
按钮\u GetIdealSize
,如文档所示。从该宏中获取大小后,可以使用来调整button.BOOL button_GetIdealSize(HWND HWND,size*pSize)的大小;我怎样才能得到心灵感应?对不起。我认为这将是足够的信息。我使用eclipse+minGW,我必须以某种方式定义按钮_GetIdealSize()。但是我必须包括什么?大卫,请注意文档。如果再次转到并向下滚动,您将注意到汇总表。它的最后一行说明需要包含的标题名为
Commctrl.h
。在mingw中,Commctrl.h不包含按钮\u GetIdealSize(),这里是Commctrl.h: