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.
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);
I'm trying to check OpenGL version creating a fake OpenGL context, but always I'm getting version 0. I create a fake context with this code:
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
WINDOWINFO info;
HWND mHWND;
info.cbSize = sizeof(WINDOWINFO);
GetWindowInfo(mHWND,&info);
HDC mDC;
mDC = GetDC(mHWND);
int fmt = ChoosePixelFormat( mDC, &pfd );
SetPixelFormat( mDC, fmt, &pfd );
HGLRC mOGLCtx;
mOGLCtx = wglCreateContext( mDC );
wglMakeCurrent(mDC, mOGLCtx);
And try to check version with this:
glGetString(GL_VERSION)
Try to get (GetActiveWindow, GetForegroundWindow) or create (CreateWindowEx) window handle before getting its DC or try to GetDC(0).
Can create minimal OpenGL context with that:
HWND wnd = CreateWindow(
"STATIC",
"GL",
WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
0, 0, 16, 16,
NULL, NULL,
NULL, NULL );
HDC dc = GetDC( wnd );
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), 1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL,
PFD_TYPE_RGBA, 32,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
16, 0,
0, PFD_MAIN_PLANE, 0, 0, 0, 0
};
int fmt = ChoosePixelFormat( dc, &pfd );
SetPixelFormat( dc, fmt, &pfd );
HGLRC rc = wglCreateContext( dc );
wglMakeCurrent( dc, rc );
Check OpenGL version with the same manner:
glGetString(GL_VERSION)
To be more efficient, after all:
// Clean GL context
wglDeleteContext( rc );
ReleaseDC( wnd, dc );
DestroyWindow( wnd );
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?