I develop a c++ application with MinGW compiler.
I changed the CreateWindow() functions default font, something like this:
HFONT font;
font = CreateFont(15, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0, 0, 0, "Tahoma");
HWND btn;
btn = CreateWindowEx(BS_PUSHBUTTON, "BUTTON", "Title",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0, 0, 0,
hwnd,
(HMENU)ID_BUTTON,
GetModuleHandle(NULL),
0);
SendMessage(btn, WM_SETFONT, (WPARAM) font, TRUE);
But I created menu:
HMENU hMenu, hSubMenu;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenuW(hSubMenu, MF_STRING, ID_FILE_OPEN_BTN, "Open" );
AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, "File" );
But I dont know how to change the font in the AppendMenuW(), can anybody help me?
Related
How can i make so user can't scroll down console.
for now this is what i have:
HWND consoleWindowHandle = GetConsoleWindow();
SetWindowPos(
consoleWindowHandle,
HWND_TOPMOST,0, 0,0, 0,
SWP_DRAWFRAME | SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
ShowWindow(
consoleWindowHandle, SW_NORMAL);
ShowScrollBar(GetConsoleWindow(), SB_VERT, 0);
EnableScrollBar(consoleWindowHandle, SB_BOTH,ESB_DISABLE_BOTH);
SetWindowLong(consoleWindowHandle, GWL_STYLE, GetWindowLong(consoleWindowHandle, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX);
I have a window, which I want it to behave like a toggle button. Once clicked it will add 4px border and clicking after will make the border disappear. I figured how to make the window behave like a toggle button using BS_PUSHLIKE and Button_SetCheck() but can't seem to figure out how to adjust the border size for this window.
Thanks to all who take their time to help
Maybe you can use MoveWindow to resize the window, and then draw the border yourself, like this,
Draw a borderless window first:
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
100, 100, 800, 600, nullptr, nullptr, hInstance, nullptr);
LONG lStyle = GetWindowLong(hWnd, GWL_STYLE);
lStyle &= ~(WS_CAPTION | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_SYSMENU);
SetWindowLong(hWnd, GWL_STYLE, lStyle);
Then handle the window border in the WM_LBUTTONDOWN message:
int num = 0;
case WM_LBUTTONDOWN:
{
RECT rcWind;
HDC dc = GetDC(hWnd);
GetWindowRect(hWnd, &rcWind);
if (num >= 0)
{
num--;
RECT rcClient;
MoveWindow(hWnd, rcWind.left - 4, rcWind.top - 4, 8 + rcWind.right - rcWind.left, 8 + rcWind.bottom - rcWind.top, TRUE);
GetClientRect(hWnd, &rcClient);
HPEN hPen = CreatePen(PS_SOLID, 4, RGB(255, 128, 1));
HGDIOBJ hOldPen = SelectObject(dc, hPen);
Rectangle(dc, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
DeleteObject(hPen);
}
else if (num < 0)
{
MoveWindow(hWnd, rcWind.left + 4, rcWind.top + 4, rcWind.right - rcWind.left - 8, rcWind.bottom - rcWind.top - 8, TRUE);
num++;
}
}
break;
When I create my window, it is smaller than the width and height specified, that should be 840x840. The actual window created is only approx 825x782 (not including menus etc). I can't work out why the window is small and have never had this problem before. Thanks in advance.
CreateWindowW(wc.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200, 840, 840,
NULL, NULL, NULL, NULL);
The solution is to use AdjustWindowRect function as commented by Simon Mourier, where rect is the size of the client window.
RECT rect = {0, 0, width, height};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, true);
CreateWindowW(wc.lpszClassName, L"Window",
WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 200,
rect.right - rect.left, rect.bottom - rect.top,
NULL, NULL, hPrevInst, NULL);
My problem is that my button has a background color for some reason, and I do not know why. I do not put the desktop background in the entire area of the button.
case WM_CREATE:
{
hwndButton = CreateWindowEx(0, L"BUTTON", L"Some static text",
WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
25, 125, 300, 300, hWnd, 0, 0, 0);
}
case WM_DRAWITEM:
{
//RECT r;
LPDRAWITEMSTRUCT lpDIS = (LPDRAWITEMSTRUCT)lParam;
if (hwndButton == lpDIS->hwndItem) {
SetBkColor(lpDIS->hDC, RGB(0, 255, 0));
//FillRect(lpDIS->hDC,&r,CreateSolidBrush(RGB(100,100,200)));
SetTextColor(lpDIS->hDC, RGB(100, 0, 100));
WCHAR staticText[99] = L"test";
TextOut(lpDIS->hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, staticText, 10);
}
Your code only draws as much of the button background as the text requires, use ExtTextOut and specify ETO_OPAQUE to fill the entire space. Either that or use FillRect to actually draw the button background and use ExtTextOut without ETO_OPAQUE to draw with a transparent background.
Here is the code from MSDN to create a toolbar. I have modified a little to use a custom icon instead.
HWND hToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, 0,
TBSTYLE_FLAT | CCS_ADJUSTABLE | CCS_NODIVIDER | WS_CHILD | WS_VISIBLE | TBSTYLE_WRAPABLE,
0, 0, 0, 0, hwnd, (HMENU)ID_TOOLBAR, g_hInstance, 0);
if ( !hToolbar )
return FALSE;
SendMessage(hToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
const int numButtons = 12;
HIMAGELIST hImageList = ImageList_Create(16, 16, ILC_COLOR16 | ILC_MASK, numButtons, 0);
SendMessage(hToolbar, TB_SETIMAGELIST, (WPARAM)0, (LPARAM)hImageList);
SendMessage(hToolbar, TB_LOADIMAGES, (WPARAM)IDB_STD_SMALL_COLOR, (LPARAM)HINST_COMMCTRL);
TBBUTTON tbButtons[numButtons] =
{
// IDI_ICON1 - the custom icon
{ MAKELONG(0, 0), IDI_ICON1, TBSTATE_ENABLED,
BTNS_AUTOSIZE, {0}, 0, (INT_PTR)L"Add Download" }
};
SendMessage(hToolbar, TB_ADDBUTTONS, numButtons, (LPARAM)tbButtons);
SendMessage(hToolbar, TB_AUTOSIZE, 0, 0);
But the custom icon is not getting displayed. Its just blank. I will post a screenshot if needed. What am i doing wrong? Thanks in advance.