I can't set my custom ICO icons on toolbar buttons in Win32 application - c++

I apologize for my poor English. I write a C ++ application in the Win32 API in Visual Studio 2017 Community on Windows 10. The application has a toolbar with buttons. I want to assign my custom functionality to each button. Therefore, each button must have its custom specific icon. (I added the icons to the application’s ResourceFiles folder.) The problem is that I can’t install my custom ICO icons on the buttons in the toolbar. When I run my application, the buttons are empty without icons. I re-read a lot of information on this topic on SO and on other sites. Still, I can’t install my icons on the buttons. I would be very grateful if anyone would help. Thank you in advance. Below is my code, with which I try to put my icons on the toolbar buttons:
#define IDM_INPUT 0
#define IDM_OUTPUT 1
#define IDM_TRIANGULATE 2
#define IDM_STOP 3
HWND CreateSimpleToolbar(HWND hWndParent)
{
// Declare and initialize the constants used in the function:
// - picture list id for buttons,
const int ImageListID = 0;
// - number of buttons,
const int numButtons = 4;
// - the size of each image for the button.
const int bitmapSize = 16;
const DWORD buttonStyles = BTNS_AUTOSIZE;
// Create the toolbar.
HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL,
WS_CHILD | TBSTYLE_WRAPABLE, 0, 0, 0, 0,
hWndParent, NULL, hInst, NULL);
if (hWndToolbar == NULL)
return NULL;
// Create a list of pictures for buttons.
g_hImageList = ImageList_Create(bitmapSize, bitmapSize,
ILC_COLOR16 | ILC_MASK, // Provide a transparent background.
numButtons, 0);
ImageList_AddIcon(g_hImageList, LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1)));
ImageList_AddIcon(g_hImageList, LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON2)));
ImageList_AddIcon(g_hImageList, LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON3)));
ImageList_AddIcon(g_hImageList, LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON4)));
// Set a list of pictures for buttons.
SendMessage(hWndToolbar, TB_SETIMAGELIST,
(WPARAM)ImageListID,
(LPARAM)g_hImageList);
// Upload pictures for buttons.
SendMessage(hWndToolbar, TB_LOADIMAGES,
(WPARAM)IDB_STD_SMALL_COLOR,
(LPARAM)HINST_COMMCTRL);
// Initialize information about buttons.
TBBUTTON tbButtons[numButtons] =
{
{ MAKELONG(IDI_ICON1, ImageListID), IDM_INPUT, TBSTATE_ENABLED, buttonStyles, {0}, 0, 0 },
{ MAKELONG(IDI_ICON2, ImageListID), IDM_OUTPUT, TBSTATE_ENABLED, buttonStyles, {0}, 0, 0},
{ MAKELONG(IDI_ICON3, ImageListID), IDM_TRIANGULATE, TBSTATE_ENABLED, buttonStyles, {0}, 0, 0},
{ MAKELONG(IDI_ICON4, ImageListID), IDM_STOP, TBSTATE_ENABLED, buttonStyles, {0}, 0, 0}
};
// Add buttons.
SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);
// Resize the toolbar and then show it.
SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0);
ShowWindow(hWndToolbar, TRUE);
return hWndToolbar;
}
IDI_ICON1, IDI_ICON2, IDI_ICON3 and IDI_ICON4 are the identifiers of the four ICO icon files resulting from the addition of these icons to the application resources.
If I check the result of the LoadIcon function in the debugger, I will see the message "Unable to read memories", although the returned HICON value itself is not zero.
Please tell me what needs to be fixed in the above code?

Related

Toolbar / CreateToolbarEx problem C++ doesn't display all bitmaps

Renders only 4 images with sperator instead of the whole 9 total with 2 separators, no idea what causes it the code looks great.
Here is my code.
void CreateMacroToolBar(HWND hDlg)
{
// Load and register Toolbar control class
INITCOMMONCONTROLSEX iccx;
iccx.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC = ICC_BAR_CLASSES;
if (!InitCommonControlsEx(&iccx))
return;
const DWORD buttonStyles = TBSTYLE_AUTOSIZE | TBSTYLE_BUTTON;
const DWORD TOOLBAR_STYLE = WS_CHILD | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE |
CCS_NOPARENTALIGN |
CCS_NORESIZE |
CCS_NODIVIDER;
// Declare and initialize local constants.
const int numButtons = 7;
const int numButtonsTotal = 9;
TBBUTTON tbButtons[numButtonsTotal] =
{
{ 0, IDM_BUTTON_MACRO_RECORD, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)(L"Record a macro" },
{ 1, IDM_BUTTON_MACRO_STOP, 0, buttonStyles, {0}, 0, (INT_PTR)(L"Stop recording" },
{ 2, IDM_BUTTON_MACRO_PLAY, 0, buttonStyles, {0}, 0, (INT_PTR)(L"Play macro" },
{ I_IMAGENONE, -1, 0, TBSTYLE_SEP, {0}, 0, -1}, //SEPERATOR
{ 3, IDM_BUTTON_MACRO_ERASE, 0, buttonStyles, {0}, 0, (INT_PTR)(L"Erase the macro" },
{ 4, IDM_BUTTON_MACRO_LOAD, 0, buttonStyles, {0}, 0, (INT_PTR)(L"Load a macro" },
{ 5, IDM_BUTTON_MACRO_SAVE, 0, buttonStyles, {0}, 0, (INT_PTR)(L"Save a macro" },
{ I_IMAGENONE, -1, 0, TBSTYLE_SEP, {0}, 0, -1}, //SEPERATOR
{ 6, IDM_MACRO_ABOUTBOX, TBSTATE_ENABLED, buttonStyles, {0}, 0, (INT_PTR)L"About macro options" }
};
RECT rect;
HWND hwndTB;
hwndTB = CreateToolbarEx (GetDlgItem(hDlg, IDC_STATIC_TOOLBAR_MACRO), //Apply toolbar to static text.
TOOLBAR_STYLE, //Toolbar style.
ID_MACRO_TOOLBAR, //Toolbar ID.
numButtons, //Button of buttons (bmp's) (without seperators)
hDLLModule, //Current application instance (where the bitmap is)
IDR_TOOLBAR_MACRO, //Bitmap ID.
tbButtons, //Buttons struct
numButtonsTotal, //Total buttons (with seperators)
16, 15, 16, 15, //Button sizes and Bitmap sizes.
sizeof(TBBUTTON) );
if (!hwndTB) {
printf("Loading Macros failed!\n");
return;
}
//SendMessage(hwndTB, TB_AUTOSIZE, 0, 0); //Auto size to show more toolbars TODO: [DOESN'T WORK]
SendMessage(hwndTB, TB_SETMAXTEXTROWS, 0, 0); //Removes the label from Toolbar and adds Tooltips.
}
my IDE setup
my toolbar bitmap spliced image looks good
Here is how the bitmap looks in bitmap file .bmp (not the white area isn't accounted for I just did a bad print screen)
Here is how the static text looks like where it gets rendered
Here is how the static text Properties looks like where the toolbar gets rendered
Finished product when loading looks like this, note it only loads up the first 4 icons + 1 separator total of 5
Instead of 9 as needed with separators, or 7 if it loads only the images
A toolbar's height is determined by the height of the buttons, and a toolbar's width is the same as the width of the parent window's client area, also by default. In this case you have passed GetDlgItem(hDlg, IDC_STATIC_TOOLBAR_MACRO) this is likely the static label. Which would limit the width of the toolbar to the width of that static control. I believe if you pass hDlg as the first parameter to CreateToolbarEx it will resolve your issue. Or create a new window to contain it that's large enough, who's parent is hDlg.
Removing these 2 styles from all buttons fixes the problem.
CCS_NOPARENTALIGN | CCS_NORESIZE

Add Dialog Box in Tab Controls

I can't seem to add the certain dialog box in the TAB CONTROL that i've created.
Can someone help me?
I've created tab items (TCITEM) and the tab control using CreateWindow.
tab_handle is an HWND global variable.
//create items for tab
TCITEM tab1Item;
tab1Item.mask = TCIF_TEXT;
tab1Item.pszText = "Tab 1";
TCITEM tab2Item;
tab2Item.mask = TCIF_TEXT;
tab2Item.pszText = "Tab 2";
//create tab
CreateWindow(WC_TABCONTROL, "Test", TCS_FLATBUTTONS | WS_CHILD | WS_VISIBLE, 10, 20, 450, 230, this->m_hWnd, (HMENU) IDD_DLGTAB1, (HINSTANCE)GetWindowLong(this->m_hWnd, GWL_HINSTANCE), NULL);
//getting of tab
tab_handle = GetDlgItem(this->m_hWnd, IDD_DLGTAB1);
//inserting of tab items in tab
RECT tab_rectangle;
GetClientRect(tab_handle, &tab_rectangle);
int width = (tab_rectangle.right - tab_rectangle.left);
int height = (tab_rectangle.bottom - tab_rectangle.top);
//create dialog
HWND dialog_handle = CreateDialogParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_DIALOG2), tab_handle, (DLGPROC) Tab1Dlg::DlgProc, (LPARAM) lParam);
TabCtrl_InsertItem(tab_handle, 0, &tab1Item);
TabCtrl_InsertItem(tab_handle, 1, &tab2Item);
MoveWindow(dialog_handle, tab_rectangle.left+20, tab_rectangle.top+20,(width - 300),(height - 300), TRUE);
ShowWindow(dialog_handle, SW_SHOW);
You have to create the dialog first. Then create the tab control as a child control of the dialog.
It's easier to use the dialog editor to drag and drop a tab control in to dialog. Then you can skip CreateWindow(WC_TABCONTROL...) and use GetDlgItem(dialog_handle, IDC_TAB1) to find the tab control.
You also need to create 2 border-less child dialogs in resource editor (not popup dialog). Then use CreateDialog(0, MAKEINTRESOURCE(IDD_PAGE1), dialog_handle, TabChildProc) to put child dialogs inside the tab.
If making a modal dialog, you may want to use DialogBox instead of CreateDialogParam and do the initialization in WM_INITDIALOG
HINSTANCE hinst = GetModuleHandle(NULL);
HWND dialog_handle = CreateDialogParam(hinst,
MAKEINTRESOURCE(IDD_DIALOG2), 0, (DLGPROC)Tab1Dlg::DlgProc, (LPARAM)0);
ShowWindow(dialog_handle, SW_SHOW);
RECT rc;
GetClientRect(dialog_handle, &rc);
CreateWindow(WC_TABCONTROL, "Test", TCS_FLATBUTTONS | WS_CHILD | WS_VISIBLE,
rc.left + 10, rc.top + 10,
rc.right - 20, rc.bottom - 20 - 30,
dialog_handle, (HMENU)IDC_TAB1, hinst, NULL);
tab_handle = GetDlgItem(dialog_handle, IDC_TAB1);
TCITEM tci = { 0 };
tci.mask = TCIF_TEXT;
char buf[50];
tci.pszText = buf;
strcpy_s(buf, "Page1");
tci.cchTextMax = strlen(buf);
TabCtrl_InsertItem(tab_handle, 0, &tci);
strcpy_s(buf, "Page2");
tci.cchTextMax = strlen(buf);
TabCtrl_InsertItem(tab_handle, 1, &tci);

Resize cursor is showing on the border of a fixed dialog frame

It is a bit difficult to provide you with a minimal working example here but I am going to try and explain this issue that I have only just noticed.
The Context
So, I have a regular CDialogEx derived class, defined like this:
class CChristianLifeMinistryStudentsDlg : public CDialogEx
I have set it up so that the borders will not resize:
The main application (also CDialogEx based) has a fixed window. That behaves correct.
From the menu the user displays a resizable dialogue (an editor).
On this dialog is a button the user can press which will in turn display the popup modal dialog I am referring to.
What Happens
When this dialog is displayed I have noticed this when you hover the mouse over the dialog borders:
I don't understand why this is happening.
Cursor Management
In the "editor" that spawns this popup window I do have some cursor management like this:
BOOL CChristianLifeMinistryEditorDlg::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
if (CPersistentWaitCursor::WaitCursorShown())
{
RestoreWaitCursor();
return TRUE;
}
return CDialogEx::OnSetCursor(pWnd, nHitTest, message);
}
But, I have tried temporarily to invoke this popup from my main application dialog which does not have an cursor management and the result is still the same.
Spy Results
As requested I have just used Spy to examine the window styles:
As anticipated we suddenly have WS_THICKFRAME set, when it was not in the resource editor!
So
In my RC file the dialog has the DS_MODALFRAME flag set but at runtime it ends up having the WS_THICKFRAME set. As far as I am aware I never make these changes for these affected dialog objects.
Update
I have found out the following:
BOOL CChristianLifeMinistryStudentsDlg::OnInitDialog()
{
LONG_PTR lStyle = GetWindowLongPtr(GetSafeHwnd(), GWL_STYLE);
if (lStyle & WS_THICKFRAME)
AfxMessageBox(_T("Thick"));
else if (lStyle & DS_MODALFRAME)
AfxMessageBox(_T("Modal"));
CDialogEx::OnInitDialog();
If I put the check code before the CDialogEx::OnInitDialog(); call the style is set as DS_MODALFRAME. But if I put the same check code after the CDialogEx::OnInitDialog(); call it is then changed to WS_THICKFRAME. Why?
OK
So, the CDialogEx::OnInitDialog method calls CWnd::LoadDynamicLayoutResource(LPCTSTR lpszResourceName). This in turn calls CWnd::InitDynamicLayout(). And in that method it does this:
if (!bIsChild && (pDialog != NULL || pPropSheet != NULL))
{
CRect rect;
GetClientRect(&rect);
ModifyStyle(DS_MODALFRAME, WS_POPUP | WS_THICKFRAME);
::AdjustWindowRectEx(&rect, GetStyle(), ::IsMenu(GetMenu()->GetSafeHmenu()), GetExStyle());
SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(), SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOOWNERZORDER);
}
There we go. So it is because I am using CDialogEx as my base class. Is this a bug in MFC?
Clarification
The "Editor" (parent window of the popup that owns the button) does use dynamic layout functonality:
But in this instance the popup does not need to. But it is because my popup is derived from CDialogEx that this is happening.
The plot thickens
So this is the MFC code that is always called with CDialog::OnInitDialog:
BOOL CWnd::LoadDynamicLayoutResource(LPCTSTR lpszResourceName)
{
if (GetSafeHwnd() == NULL || !::IsWindow(GetSafeHwnd()) || lpszResourceName == NULL)
{
return FALSE;
}
// find resource handle
DWORD dwSize = 0;
LPVOID lpResource = NULL;
HGLOBAL hResource = NULL;
if (lpszResourceName != NULL)
{
HINSTANCE hInst = AfxFindResourceHandle(lpszResourceName, RT_DIALOG_LAYOUT);
HRSRC hDlgLayout = ::FindResource(hInst, lpszResourceName, RT_DIALOG_LAYOUT);
if (hDlgLayout != NULL)
{
// load it
dwSize = SizeofResource(hInst, hDlgLayout);
hResource = LoadResource(hInst, hDlgLayout);
if (hResource == NULL)
return FALSE;
// lock it
lpResource = LockResource(hResource);
ASSERT(lpResource != NULL);
}
}
// Use lpResource
BOOL bResult = CMFCDynamicLayout::LoadResource(this, lpResource, dwSize);
// cleanup
if (lpResource != NULL && hResource != NULL)
{
UnlockResource(hResource);
FreeResource(hResource);
}
if (bResult)
{
InitDynamicLayout();
}
return bResult;
}
For some reason this call BOOL bResult = CMFCDynamicLayout::LoadResource(this, lpResource, dwSize); is return TRUE. As a result the dialog eventually calls InitDynamicLayout. In my other dialogs that are popups this does not happen. Instead, bResult ends up as FALSE and thus the frame is not resized.
So why does it think it worked?
Worked it out. I don't remember doing this but for some reason some of my controls on the dialog had dynamic properties set. For example:
I had to set all of these properties back to None. Then it behaved.
You can easily tell if a given dialog resource has any dynamic properties by opening your resource file in a text editor. For example:
IDD_DIALOG_OUR_CHRISTIAN_LIFE_AND_MINISTRY_MATERIAL AFX_DIALOG_LAYOUT
BEGIN
0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 10, 0,
0, 0, 0, 0,
50, 0, 0, 0,
50, 0, 0, 0,
0, 0, 0, 0,
0, 0, 10, 0,
0, 0, 0, 0,
50, 0, 0, 0,
50, 0, 0, 0,
0, 0, 0, 0,
0, 0, 10, 0,
0, 0, 0, 0,
50, 0, 0, 0,
50, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0
END
If something like the above is present then your dialog will be deemed as having a dynamic layout, and thus the settings for the dialog are modified:
ModifyStyle(DS_MODALFRAME, WS_POPUP | WS_THICKFRAME);
The resource will look like this when it has no dynamic control properties:
IDD_DIALOG_OUR_CHRISTIAN_LIFE_AND_MINISTRY_MATERIAL AFX_DIALOG_LAYOUT
BEGIN
0
END
I chose to manually reset each control via the IDE. However, I guess you could modify the text file manually.
As to why I had controls with dynamic properties in the first place, well, I can't tell you. I might have been fiddling in the past with the dialog and not realised the side effect to the border frame. Or, possibly, I may have copied controls from one resource on to another and it carried the dyanmic values.
The interesing side note is that whilst the MFC code set the border as thick, it did not change it sufficiently to enable dialog resizing. But that is another matter!
At least we now know the cause of the issue and how to easily identify the dialogs in the resource that have dynamic layouts.

How do I create a button in c++ with my own custom image?

I've been looking for a way to make a picture a button in c++ for a few hours now.. I've found stuff on using bitmaps, what what i am currently using to display the image is GDI+, because i want to use jpg/png files.
This is how i created my Image with gdiplus:
void Example_DrawImage(HDC hdc) {
Graphics graphics(hdc);
image = Image::FromFile(L"Path:/To/Image");
myBitmap = dynamic_cast<Bitmap*>(image);
Pen pen(Color(0, 0, 0, 0), 2);
graphics.DrawImage(image, 10, 10);
}
I converted that to a bitmap with:
myBitmap = dynamic_cast<Bitmap*>(image);
Then, in WM_CREATE I created a button which it's standard style is a Windows XP Button:
button = CreateWindow(TEXT("button"), TEXT("Hello"),
WS_VISIBLE | WS_CHILD | BS_BITMAP,
10, 10, /* x & y*/ 80, 25, /*width & height*/
hwnd, (HMENU) 1, hInstance, NULL
);
button is globally defined as HWND button;
All I want is to have a button that is a jpg picture. I tried doing it manually by seeing if a mouse click was inside a certain area, but I could not find a way to find the position of the Image.

how to change the position of the child window inside the parent window and show the toolbar?

I have the following code which passes a window handler form OpenCV window to win32 handler, therefore I can show the grabbed images from camera to the screen and the images will show as a child window of my main API.
but the problem is that when I want to add a tooldbar to my program, the image window handler comes at the top of the toolbar. how can I sort this out?
//create a window and set the handler from openCV to win32
cv::namedWindow("test",cv::WINDOW_AUTOSIZE);
hWnd2 = (HWND) cvGetWindowHandle("test");
hParent = ::GetParent(hWnd2);
::SetParent(hWnd2, hWnd);
::ShowWindow(hParent, SW_HIDE);
_liveCapturing=true;
lastPicNr = 0;
SetWindowTextW(hStatus, L"Live Capturing ... ");
if(FullScreen()){
::ShowWindow(hWnd, SW_MAXIMIZE);
}
code for the toolbar :
HWND CreateToolbar(HWND hwnd){
HWND hTbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE | CCS_TOP , 0, 0, 0, 0, hwnd, (HMENU)12, GetModuleHandle(NULL), NULL);
SendMessage(hTbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
TBBUTTON tbb[3];
TBADDBITMAP tbab;
tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hTbar, TB_ADDBITMAP, 0, (LPARAM)&tbab);
return hTbar;
}
Probably you have found the solution a long time ago, but i want to post my anwers in case other users need it.
You can simply add the OpenCV window with the same code you have to a child window in your window (which you set it position in advance). For example you can add it to a static text window (label) ...
If you want to move the OpenCV window, call SetWindowPos() with the desired coordinates.
SetWindowPos(hWnd2, 0, 0, 30, 0, 0, SWP_NOSIZE | SWP_NOZORDER);