Getting font from CFontDialog - c++

I'm creating a simple GUI for changing the input text formatting in Visual C++ and would like to know how to fetch font type from CFontDialog.
Input text is fetched from EditBox to the m_txtEdit and then passed to m_text.
void CTxtDlg::OnOK()
{
m_txtEdit.GetWindowText(m_text);
CDialog::OnOK();
}
m_text is a member of a CDoc class and should be formatted according to what the user selects in the CFontDialog.
The selected font is passed to the LOGFONT m_lf variable which is a member of CTxtDlg class.
The CFont m_font should be made equal to LOGFONT m_lf.
void CTxtDlg::OnBnClickedButton1()
{
CFontDialog dlg;
int response = dlg.DoModal();
dlg.GetCurrentFont(&m_lf);
}
The CView class should output the CString m_text formatted according to what is set in CFont m_font.
How do I accomplish this?

Essentially, your question is completely unrelated to the CFontDialog. Your comment is asking, how to create a CFont object given a LOGFONT structure. That's what the CFont::CreateFontIndirect member function is for:
if ( !m_font.CreateFontIndirect( &m_lf ) ) {
// handle error
}
// use m_font

First add a CFont to your CTxtDlg:
class CTxtDlg {
....
CFont m_font;
}
Then create and use the font:
void CTxtDlg::OnBnClickedButton1()
{
CFontDialog dlg;
int response = dlg.DoModal();
if(response == IDOK) {
dlg.GetCurrentFont(&m_lf);
VERIFY(m_font.CreateFontIndirect(&m_lf));
SetFont(&m_font);
}
}
Please note that as far as I understand it the font must remain valid after SetFont, so you cannot easily destroy the font and recreate it if it is still set in the dialog.

Related

C++ CButton showing only bitmap icon

I'm using C++ on Visual Studio 2012 update 4, and I have a Dialog where I want to display a button showing a bitmap (.bmp file), without borders
I have extended CButton to add my tooltip, and so on.
Using the Resource View to open the Dialog .rc file, I set the button Property Bitmap to true. Then, from the Dialog OnInitDialog function, I used this code to set the bitmap, identified as IDB_HELP
myButton.SetBitmap((HBITMAP)LoadImage(AfxGetApp()->m_hInstance,
MAKEINTRESOURCE(IDB_HELP), IMAGE_BITMAP, 16, 16, LR_COLOR));
But it displays this and I don't want that half-border.
I tried making it Flat and Transparent in the Resource View, but it only gets uglier.
Then I tried to only draw the image by setting Owner Draw to true and then redefining DrawItem in my button class, but I can't quite figure that out either.
Any easy way to make an icon-only button?
You have to use owner draw button or custom draw. Below is a simple example, it uses icon instead of bitmap (it's easier to assign transparent background for it)
class CMyButton:public CButton
{
void OnPaint()
{
CPaintDC dc(this);
CRect rc = dc.m_ps.rcPaint;
dc.FillSolidRect(&rc, GetSysColor(COLOR_3DFACE));
BOOL offset = (BST_PUSHED & GetState()) ? 1 : 0;
int w = 24;
int h = 24;
HICON hicon = (HICON)LoadImage(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDC_ICON),
IMAGE_ICON, w, h, LR_DEFAULTCOLOR);
DrawIconEx(dc, offset, offset, hicon, w, h, 0, 0, DI_NORMAL);
DestroyIcon(hicon);
}
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(CMyButton, CButton)
ON_WM_PAINT()
END_MESSAGE_MAP()
Usage:
BOOL CMyDialog::OnInitDialog()
{
BOOL res = CDialogEx::OnInitDialog();
static CMyButton bn;
bn.SubclassDlgItem(IDC_BUTTON1, this);
return res;
}
You do NOT need to do your own icon painting algorithm if you use a CMFCButton and you are a comfortable using an ICO file instead of a BMP. Although you can directly say in your resources file a button is of this type, I do not recommend it, because it adds an unmaintainable hexadecimal piece of text on the rc file. And if you use several rc files, one for each language, it's really devilish!
So lets go. In your form class, declare a member
CMFCButton m_button1;
The DoDataExchange should look like:
void MyDialog::DoDataExchange(CDataExchange* pDX)
{
__super::DoDataExchange(pDX);
DDX_Control(pDX, IDC_BUTTON1, m_button1);
// ...
}
Then the OnInitDialog should be something like:
BOOL CMyDialog::OnInitDialog()
{
if(!__super::OnInitDialog())
return FALSE;
m_button1.m_nFlatStyle= CMFCButton::BUTTONSTYLE_NOBORDERS;
m_button1.SetIcon(IDI_HELP);
return TRUE;
}
Use CMFCbutton and set the border style to BUTTONSTYLE_NOBORDERS;
Use a .ico instead of png for pictures.
Also points to note:
Load the library or exe which has the icon.
Pass the dll/exe loaded handle to loadicon.
Use MFC button handle to set the icon with property set as Noborder.
Example code:
m_HResdll = LoadLibrary("C:\\Repos\\iFIX\\SCADABin\\en\\UAAClientConfigurationRes.dll");
//m_hTrustIcon = LoadIcon(m_HResdll, MAKEINTRESOURCE(IDI_ICON1));
m_hTrustIcon = (HICON)LoadImage(m_HResdll, MAKEINTRESOURCE(IDI_ICON2),1,18,22, LR_DEFAULTCOLOR);
unsigned int err = GetLastError();
m_btnTrustIcon.SetIcon(m_hTrustIcon);
this->m_btnTrustIcon.EnableWindow(true);

How to replace an CMFCPropertyGridCtrl with a new one inside CDockablePane

i have created CMFCPropertyGridCtrl inside an CDockablePane and i want to replace this CMFCPropertyGridCtrl with a new one, then i override OnEraseBkgnd.
OnEraseBkgnd called only in the application start, and when i want to call it by Invalidate or InvalidateRect it didn't fire.
How can i call OnEraseBkgnd?
Thanks in advance.
void CCL2PropertiesPane::HostPropertyGridControl(CMFCPropertyGridCtrl* pPropertyGridControl)
{
if(NULL == pPropertyGridControl)
return;
if(m_pPropertyGridControl)
RemoveCurrentPropertyGridControl();
m_pPropertyGridControl = pPropertyGridControl;
SetWindowText(m_pPropertyGridControl->GetName());
CRect clientRectangle;
GetClientRect(&clientRectangle);
m_pPropertyGridControl->Create(WS_CHILD | WS_VISIBLE, clientRectangle, this, PROPERTIES_DOCKABLE_PANE_ID);
}
//--------------------------------------------------------------------------------
void CCL2PropertiesPane::RemoveCurrentPropertyGridControl()
{
m_pPropertyGridControl = NULL;
SetWindowText(GetPaneName());
CRect clientRectangle;
GetClientRect(&clientRectangle);
//here i want to call OnEraseBkgnd
InvalidateRect(clientRectangle);
//Invalidate();
}
//--------------------------------------------------------------------------------
BOOL CCL2PropertiesPane::OnEraseBkgnd(CDC* pDC)
{
CRect clientRectangle;
GetClientRect(&clientRectangle);
CBrush whiteBrush(RGB(250, 250, 250));
pDC->FillRect(clientRectangle, &whiteBrush);
return TRUE;
}
Add ON_WM_ERASEBKGND to CCL2PropertiesPane's message map to properly erase the background. Or move the FillRect function in to OnPaint.
Regarding:
void CCL2PropertiesPane::RemoveCurrentPropertyGridControl()
{
m_pPropertyGridControl = NULL;
...
}
Above code is appropriate for initialization, but it doesn't remove or destroy anything. The control is still there, it only makes the program forget how to find the control. To hide the control use:
m_pPropertyGridControl->ShowWindow(SW_HIDE);
To destroy the control use DestroyWindow(), but that's not recommended with above setup.

How to change font size and font of static control when the property is transparent

I create two CStatic controls. One property is set to transparent mode; another one is normal.
After I change font size, one is OK, it is changed, but the set transparent mode one is not changed in size.
Does anybody know why?
//////////////////////////////////////////////////
/* Resource File */
LTEXT "This Is Normal Text.",IDC_FONT2,7,119,303,21,WS_BORDER
LTEXT "This Include Transparent.",IDC_FONT,7,7,306,21,WS_BORDER | NOT WS_GROUP | WS_TABSTOP,WS_EX_TRANSPARENT
/* FontTest.CPP */
class CFontSizeDlg : public CDialogEx
{
public:
CStatic m_myFont;
CStatic m_myFont2;
}
/* FontTest.CPP */
void CFontSizeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_FONT, m_myFont);
DDX_Control(pDX, IDC_FONT2, m_myFont2);
}
void CFontSizeDlg::OnBnClickedButton2()
{
CFont hNewFont;
LOGFONT lf; // Used to create the CFont.
CFont *currentFont = GetFont();
currentFont->GetLogFont(&lf);
lf.lfHeight = 25;
lf.lfWidth = 10;
hNewFont.DeleteObject();
hNewFont.CreateFontIndirect(&lf); // Create the font.
// Use the font to paint a control.
m_myFont2.SetFont(&hNewFont);
m_myFont.SetFont(&hNewFont);
// hNewFont.Detach(); // will create GDI leak
hNewFont.DeleteObject();
}
You need to make sure that the scope of the 'new' font is the same as the static control(s). In your example, the font is destroyed when the button event handler is done. Try making hNewFont a member variable and setting it once.

MFC, EndDialog, repeated dialog creation causes unexpected behavior

I need to adjust the dialog window dynamically based on its size. To do so I employ the following technique:
I load it up and get its size from the CDialog::OnInitDialog() handler.
If the size is too big, I end the dialog by calling CDialog::EndDialog
And then update global variable and reinit the dialog-derived class again with the size adjustment.
What happens is that on the second pass, some APIs start acting strangely. For instance, MessageBox does not show (thus all ASSERT macros stop working) and some SetWindowText APIs crash the app. Any idea why?
Here're the code snippets:
#define SPECIAL_VALUE -1
//From CWinApp-derived class
BOOL CWinAppDerivedClass::InitInstance()
{
//...
for(;;)
{
CDialogDerivedClass dlg(&nGlobalCounter);
m_pMainWnd = &dlg;
if(dlg.DoModal() != SPECIAL_VALUE)
break;
}
//...
}
And then from the dialog class itself:
//From CDialogDerivedClass
BOOL CDialogDerivedClass::OnInitDialog()
{
//The following API shows message box only on the 1st pass, why?
::MessageBox(NULL, L"1", L"2", MB_OK);
//...
if(checkedDialogSizeIndicatesReload)
{
this->EndDialog(SPECIAL_VALUE);
return FALSE;
}
//Continue loading dialog as usual
...
}
EDIT: I noticed by chance that if I comment out the following line it seems to work. Any idea why?
//m_pMainWnd = &dlg;
Variable dlg is not yet a window at the place where you are setting m_pMainWnd (the dialog box is displayed only after OnInitInstance returns TRUE); the Following code should work:
for(;;)
{
CDialogDerivedClass dlg(&nGlobalCounter);
// m_pMainWnd = &dlg;
if(dlg.DoModal() != SPECIAL_VALUE)
break;
}
m_pMainWnd = &dlg;
InitDialog is the last message processed before the dialog window appears on the screen - you can detect and adjust the size in place and not have the kind of funky global variable thing you are doing.
if(checkedDialogSizeIndicatesReload)
{
// look up SetWindowPos -
// I am nt sure if there is another parameter or not that is optional
int x,y,cx,cy;
WINDOWPLACEMENT wp;
GetWindowPlacement(&wp);
// calc new size here
SetWindowPos(this,x,y,cx,cy);
}
// window appears when the message handler returns

MFC: Dynamically change control font size?

I have a CListCtrl class that I'd like to be able to easily change the font size of. I subclassed the CListCtrl as MyListControl. I can successfully set the font using this code in the PreSubclassWindow event handler:
void MyListControl::PreSubclassWindow()
{
CListCtrl::PreSubclassWindow();
// from http://support.microsoft.com/kb/85518
LOGFONT lf; // Used to create the CFont.
memset(&lf, 0, sizeof(LOGFONT)); // Clear out structure.
lf.lfHeight = 20; // Request a 20-pixel-high font
strcpy(lf.lfFaceName, "Arial"); // with face name "Arial".
font_.CreateFontIndirect(&lf); // Create the font.
// Use the font to paint a control.
SetFont(&font_);
}
This works. However, what I'd like to do is create a method called SetFontSize(int size) which will simply change the existing font size (leaving the face and other characteristics as is). So I believe this method would need to get the existing font and then change the font size but my attempts to do this have failed (this kills my program):
void MyListControl::SetFontSize(int pixelHeight)
{
LOGFONT lf; // Used to create the CFont.
CFont *currentFont = GetFont();
currentFont->GetLogFont(&lf);
LOGFONT lfNew = lf;
lfNew.lfHeight = pixelHeight; // Request a 20-pixel-high font
font_.CreateFontIndirect(&lf); // Create the font.
// Use the font to paint a control.
SetFont(&font_);
}
How can I create this method?
I found a working solution. I'm open to suggestions for improvement:
void MyListControl::SetFontSize(int pixelHeight)
{
// from http://support.microsoft.com/kb/85518
LOGFONT lf; // Used to create the CFont.
CFont *currentFont = GetFont();
currentFont->GetLogFont(&lf);
lf.lfHeight = pixelHeight;
font_.DeleteObject();
font_.CreateFontIndirect(&lf); // Create the font.
// Use the font to paint a control.
SetFont(&font_);
}
The two keys to getting this to work were:
Removing the copy of the LOGFONT, lfNew.
Calling font_.DeleteObject(); before creating a new font. Apparently there can't be an existing font object already. There is some ASSERT in the MFC code that checks for an existing pointer. That ASSERT is what was causing my code to fail.