有没有办法使按钮内的文本水平和垂直居中对齐?
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT Item;
Item = (LPDRAWITEMSTRUCT)lParam;
SelectObject(Item->hDC, CreateFont(17, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, "Arial Black"));
FillRect(Item->hDC, &Item->rcItem, CreateSolidBrush(0xE0E0E0) );
SetBkMode(Item->hDC, 0xE0E0E0);
SetTextColor(Item->hDC, RGB(255,255,255));
int len;
len = GetWindowTextLength(Item->hwndItem);
LPSTR lpBuff;
lpBuff = new char[len+1];
GetWindowTextA(Item->hwndItem, lpBuff, len+1);
DrawTextA(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER);
}
break;
答案 0 :(得分:1)
您已经在使用DT_CENTER
标志将文本水平居中。 DrawText()
还具有DT_VCENTER
和DT_SINGLELINE
标志,以使文本垂直居中。只需将标志结合在一起即可。
此外,您还有资源和内存泄漏。您正在从HFONT
泄漏CreateFont()
,从HBRUSH
泄漏CreateSolidBrush()
,并且从new[]
泄漏文本缓冲区。使用完后,您需要释放它们。
尝试一下:
case WM_DRAWITEM:
{
LPDRAWITEMSTRUCT Item = reinterpret_cast<LPDRAWITEMSTRUCT>(lParam);
HFONT hFont = CreateFont(17, 0, 0, 0, FW_NORMAL, 0, 0, 0, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, TEXT("Arial Black"));
HFONT hOldFont = (HFONT) SelectObject(Item->hDC, hFont);
HBRUSH hBrush = CreateSolidBrush(RGB(0xE0, 0xE0, 0xE0));
FillRect(Item->hDC, &Item->rcItem, hBrush);
DeleteObject(hBrush);
SetBkMode(Item->hDC, TRANSPARENT); // <-- 0xE0E0E0 was not a valid mode value!
SetTextColor(Item->hDC, RGB(255,255,255));
int len = GetWindowTextLength(Item->hwndItem) + 1;
LPTSTR lpBuff = new TCHAR[len];
len = GetWindowText(Item->hwndItem, lpBuff, len);
DrawText(Item->hDC, lpBuff, len, &Item->rcItem, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
delete[] lpBuff;
SelectObject(Item->hDC, hOldFont);
DeleteObject(hFont);
}
break;
答案 1 :(得分:-1)
来自https://docs.microsoft.com/en-us/windows/desktop/Controls/button-styles
可能需要使用import random
while True:
dice_number = random.randint(1, 6)
print('You rolled ' + str(dice_number))
print("Do you wish to continue? [Y/n]" )
if input() == 'Y' or 'y':
pass
else:
break
来完成您正在描述的事情。