I'm completely new to MFC and have been working on this issue for a couple of days now and can't find any solution that works.
Problem:
I have a dialog class (Modal Dialog Box) with a style defined in an .rc file (Code below) and get the resource id of the icon (int m_icon is the same as IDR_MAINFRAME) from another class (OtherClass.rc).
All the text information inside the Dialog Box is set dynamically (Code below) but the same doesn't work with the Icon. The marked Icon in the image below is what I'm trying to set.
The Icon Resource is defined in another .rc file and the LoadImage seems to work as I can set the small Icon in the top left of the window. The only problem is setting the big icon in this image. (Not shown at all, just an empty space)
OtherClass.rc
IDR_MAINFRAME ICON "res\\MyIcon.ico"
Dialog.rc
ABOUTBOX DIALOGEX 0, 0, 285, 77
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "<<Aboutbox>>"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
ICON ABOUT_ICON, ABOUT_ICON, 11, 10, 21, 20
LTEXT "", IDC_STATIC, 40, 10, 163, 8, SS_NOPREFIX
LTEXT "<<Package Name 1.00>>", ABOUT_NAME, 40, 20, 163, 8, SS_NOPREFIX
LTEXT "<<FileName>>", ABOUT_FILENAME, 40, 30, 163, 8, SS_NOPREFIX
DEFPUSHBUTTON "OK", IDOK, 217, 7, 60, 14, WS_GROUP
END
Dialog.cpp
BOOL AboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Window Title
SetWindowText(L"About " + m_title);
// Set Icon
HICON hIcon = (HICON)LoadImage(GetModuleHandleW(NULL), MAKEINTRESOURCE(m_icon), IMAGE_ICON, 96, 96, LR_DEFAULTCOLOR);
SetIcon(hIcon, TRUE);
SetIcon(hIcon, FALSE);
// Text
SetDlgItemText(ABOUT_NAME, m_name);
SetDlgItemText(ABOUT_FILENAME, m_filename);
return TRUE;
}
What I've tried doing is:
1. GetDlgItem(ABOUT_ICON)->SetIcon(hIcon, TRUE);
2. SendMessage(WM_SETICON, ICON_BIG, (LPARAM)hIcon);
and many more things along those lines but the icon space just remains empty. Neither the LoadImage nor the GetDlgItem(ABOUT_ICON) returns a nullptr (already checked that).
You shouldn't have to do any thing like SetIcon or SendMessage to get an icon to show. What's wrong is your RC file is wrong. It should look like this:
ABOUTBOX DIALOGEX 0, 0, 285, 77
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "<<Aboutbox>>"
FONT 8, "MS Shell Dlg", 0, 0, 0x0
BEGIN
ICON ABOUT_ICON, IDC_STATIC, 11, 10, 21, 20
LTEXT "", IDC_STATIC, 40, 10, 163, 8, SS_NOPREFIX
LTEXT "<<Package Name 1.00>>", ABOUT_NAME, 40, 20, 163, 8, SS_NOPREFIX
LTEXT "<<FileName>>", ABOUT_FILENAME, 40, 30, 163, 8, SS_NOPREFIX
DEFPUSHBUTTON "OK", IDOK, 217, 7, 60, 14, WS_GROUP
END
Take a look at the ICON line after the BEGIN line.
You had it as ABOUT_ICON, ABOUT_ICON, 11, 10, 21, 20, but the 2nd parameter tells the framework what the ID of the control is. Since you want a Static control, you use the ID of IDC_STATIC which tells the framework there is no ID, but instead it should create a generic Static control.
If this doesn't fix it, I would investigate if ABOUT_ICON is wired up correctly, that it actually is pointing at an icon.
Also, why are you working in the RC file? MFC gives you a great GUI editor. For example, you could modify it using the UI:
Related
I managed to get custom text color in REC, but I can't make it to use the desired font (Consolas).
(EDIT) Creating REC:
LoadLibrary(TEXT("Msftedit.dll"));
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, MSFTEDIT_CLASS, L"",
WS_CHILD | WS_VISIBLE | ES_MULTILINE,
0, 0, 300, 300,
hwnd, NULL, GetModuleHandle(NULL), NULL);
-
CHARFORMAT cf = {};
cf.cbSize = sizeof(CHARFORMAT);
cf.dwMask = CFM_COLOR | CFM_FACE;
cf.crTextColor = RGB(255, 0, 255);
cf.bPitchAndFamily = DEFAULT_PITCH | FF_MODERN;
memcpy(cf.szFaceName, L"Consolas", sizeof(L"Consolas"));
SendMessage(hEdit, EM_SETCHARFORMAT, NULL, (LPARAM)&cf);
I'm using
#ifndef UNICODE
#define UNICODE
#endif
Few answers that i found but didn't help me much...
Win32 : set default font and text color for rich edit,
How to set a font in rich edit 4?
Note: the REC has no text in it
This is what i get...
5 'i' and 5 '0' should have same spacing with Consolas, Right?
I was trying to make windres compile a resource file from Visual Studio. I properly converted all the files (and the one included) to ANSI, but I still get this error every time I try to compile it :
windres.exe -J rc -O coff -i C:\Users\Gabriel\DOCUME~1\DoConfig\DoConfig\DoConfig.rc -o Debug\DoConfig.res
mingw32-g++.exe -o ..\Debug\DoConfig.exe Debug\DoConfig.o Debug\stdafx.o Debug\DoConfig.res -lwinmm.lib -lcomctl32.lib -lkernel32.lib -luser32.lib -lgdi32.lib -lwinspool.lib -lcomdlg32.lib -ladvapi32.lib -lshell32.lib -lole32.lib -loleaut32.lib -luuid.lib -lodbc32.lib -lodbccp32.lib -mwindows
C:\Users\Gabriel\DOCUME~1\DoConfig\DoConfig\DoConfig.rc:4:0: fatal error: when writing output to : Invalid argument
^
compilation terminated.
windres.exe: C:\\Users\\Gabriel\\DOCUME~1\\DoConfig\\DoConfig\\DoConfig.rc:22: syntax error
windres.exe: preprocessing failed.
Process terminated with status 1 (0 minute(s), 3 second(s))
3 error(s), 0 warning(s) (0 minute(s), 3 second(s))
Here is the start of the source code of the corresponding file :
#include "resource.h"
#include <windows.h>
IDI_DOCONFIG ICON "DoConfig.ico"
STRANGE_LAYOUT BITMAP "Strange_Layout.bmp"
NORMAL_LAYOUT BITMAP "Normal_Layout.bmp"
MENU_MAIN MENU
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
{
MENUITEM "Quit", 40001
MENUITEM "Note", 40002
}
DLG_CONFIG DIALOGEX 0, 0, 387, 281
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Doukutsu Monogatari - Settings"
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
FONT 9, "Arial"
{
CONTROL "Use Gamepad", Button_Use_Gamepad, BUTTON, BS_AUTOCHECKBOX | WS_CHILD | WS_VISIBLE | WS_TABSTOP, 12, 16, 100, 12
CONTROL "", Button_Jump_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_GROUP, 52, 48, 12, 12
CONTROL "", Button_Attack_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 64, 12, 12
CONTROL "", Button_Weapon_Plus_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 80, 12, 12
CONTROL "", Button_Weapon_Minus_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 96, 12, 12
CONTROL "", Button_Item_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 112, 12, 12
CONTROL "", Button_Map_1, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 52, 128, 12, 12
CONTROL "", Button_Jump_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE | WS_GROUP, 68, 48, 12, 12
CONTROL "", Button_Attack_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 64, 12, 12
CONTROL "", Button_Weapon_Plus_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 80, 12, 12
CONTROL "", Button_Weapon_Minus_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 96, 12, 12
CONTROL "", Button_Item_2, BUTTON, BS_AUTORADIOBUTTON | WS_CHILD | WS_VISIBLE, 68, 112, 12, 12
EDIT : Note that I looked at the .rc file with a hex editor and all the newlines are fine, the conversion to ANSI worked properly
EDIT 2 : The path to the .rc file doesn't have any spaces or anything that should screw with windres :
C:\Users\Gabriel\Documents\DoConfig\DoConfig\DoConfig.rc
EDIT 3 : The windows.h file is in my include directory, I checked manually. Plus, the .cpp file uses it and it didn't fail to compile.
EDIT 4 : Removing the line that includes the windows.h file doesn't change anything, it still gives the same error.
EDIT 5 : Naming every class by string instead of by name directly worked and eliminated all error messages.
Control's class name should be "BUTTON", not BUTTON
Button_Use_Gamepad should be defined as an integer
Corrected format:
CONTROL "Use Gamepad", 101, "BUTTON", BS_AUTOCHECKBOX | WS_TABSTOP, 12, 16, 100, 12
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.
I'm a newbie in windows programming and am continuously running into different kinds of problems, most of which I have been able to solve by myself.
My problem at hand is the caret (or cursor) shown in text areas. The thing that indicates where you are typing your text? Well it is shown, at least, but it doesn't blink like it should.
I have an EDIT box created in WM_CREATE like so:
case WM_CREATE:
{
if(!logged) {
HWND userField = CreateWindow(
"EDIT", // Predefined class; Unicode assumed
NULL, // Button text
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Styles
C_WIDTH/2 - 80, // x position
C_HEIGHT - 240, // y position
160, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
// initialize NONCLIENTMETRICS structure
NONCLIENTMETRICS ncm;
ncm.cbSize = sizeof(ncm);
// obtain non-client metrics
SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(ncm), &ncm, 0);
// create the new font
HFONT hNewFont = CreateFontIndirect(&ncm.lfMessageFont);
// set the new font
SendMessage(userField, WM_SETFONT, (WPARAM)hNewFont, 0);
}
}
break;
That is all code concerning the edit box. I'm sorry if I'm not being clear enough or my supply of code is lacking; I'm unsure of what parts of code is relevant here and what are irrelevant. I don't think I should paste my whole code here, either.
The problem, again, is that the caret in the textbox (userField) does not blink.
Please ask for more details if you need them.
Using your code, I didn't get a flashing caret. But then i added:
SetFocus( userField );
and voilĂ , a flashing caret :-)
This may not be the problem the OP was experiencing, but I was experiencing the same symptom, and I'm posting my solution here in case someone else experiences this problem...
In short, if you subclass an edit control, and handle the WM_SETFOCUS event, you need to call DefSubclassProc() or your caret won't show up. Presumably, you can call ShowCaret() yourself, but you're probably safer just calling DefSubclassProc() in case there's other processing that needs to happen.
After playing around - making my code a bit tidier and stuff - I accidentally solved this on my own
I changed
HWND userField = CreateWindow(
"EDIT", // Predefined class; Unicode assumed
NULL, // Button text
WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT, // Styles
C_WIDTH/2 - 80, // x position
C_HEIGHT - 240, // y position
160, // Button width
25, // Button height
hwnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
NULL); // Pointer not needed.
Into
HWND userField = CreateWindow("EDIT", NULL, WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT,
C_WIDTH/2 - 80, C_HEIGHT - 240, 160, 25, hwnd, NULL, g_hInstance, NULL);
The only difference there is the hInstance: in the first code it was apparently wrong. I changed it into my global reference of hInstance.
I have a CTabCtrl on my dialog, and it has several labels (CStatic) on them. The problem is, the tab control has a white background, and the labels have grey backgrounds. I know why - the parent of the labels is actually the dialog, not the tab control. However, I should be able to use CWnd::OnCtlColor to provide a custom background brush for the labels:
HBRUSH MyDialog::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
HBRUSH hBrush = __super::OnCtlColor(pDC, pWnd, nCtlColor);
const int dialogId = pWnd->GetDlgCtrlID();
if (dialogId == IDC_MY_CONTROL)
{
pDC->SetBkMode(TRANSPARENT);
hBrush = m_nullBrush;
}
return hBrush;
}
Here I use m_nullBrush to provide a brush to paint the background of the labels with, the only trouble is, I don't know how to get the tab's background colour, and instead have got it hardcoded with m_nullBrush.CreateStockObject(WHITE_BRUSH);.
Even if I re-parent the labels onto the tab control, they still end up with a grey background (even though the tab control has a white background).
How do I retrieve the background colour of a CTabCtrl?
You can put your controls in a child dialog and you must enable theme for this child dialog using EnableThemeDialogTexture.
#include "Uxtheme.h"
...
BOOL CTabDemoDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
COneDlg* OneDlg= new COneDlg;
OneDlg->Create(IDD_ONE, this);
AddPage(OneDlg, L"One");
return TRUE;
}
void CTabDemoDlg::AddPage(CDialog *Dialog, const wchar_t* Title)
{
if (IsAppThemed())
EnableThemeDialogTexture(*Dialog, ETDT_ENABLETAB);
CRect Rect;
TabCtl.GetWindowRect(Rect);
Rect.top+= 20;
Rect.InflateRect(-4, -4);
ScreenToClient(Rect);
Dialog->MoveWindow(Rect);
TabCtl.InsertItem(0, Title);
}
IDD_ONE DIALOGEX 0, 0, 224, 111
STYLE DS_SETFONT | DS_FIXEDSYS | DS_CONTROL | WS_CHILD | WS_VISIBLE
EXSTYLE WS_EX_CONTROLPARENT
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
CONTROL "Check1",IDC_CHECK1,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,20,16,39,10
LTEXT "Static",IDC_STATIC,20,36,19,8
EDITTEXT IDC_EDIT1,20,48,40,14,ES_AUTOHSCROLL
PUSHBUTTON "Button1",IDC_BUTTON1,84,16,50,14
END