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.
Related
I have few BS_OWNERDRAWN buttons and using Direct2D and Direct Write to draw them.
I also need to draw button text within button rectangle, for this I use IDWriteTextFormat which requires to specify "font size" in DIPs (device independent pixels).
I want font size in those buttons to be of same size as other non owner drawn common controls or same as system font that is present in window caption bar.
Following code is "chopped out" version to present my workaround which of course doesn't give expected results because I get LOGFONT structure of a font in caption bar which gives me the width of a font (single character) but not font size that the IDWriteTextFormat expects to specify font size in DIPs.
class CustomControl
{
protected:
/** Caption text format used to draw text */
CComPtr<IDWriteTextFormat> mpTextFormat;
/** Caption font size (button text size) */
float mFontSize;
};
// Calculate caption bar (default) font size of a top level window
void CustomControl::CalculateFontSize()
{
NONCLIENTMETRICSW metrics;
metrics.cbSize = sizeof(NONCLIENTMETRICSW);
SystemParametersInfoW(SPI_GETNONCLIENTMETRICS, metrics.cbSize, &metrics, 0);
LOGFONTW font = metrics.lfCaptionFont;
mFontSize = static_cast<float>(font.lfHeight);
}
// Create text format that is of same font size as default system font
HRESULT CustomControl::CreateTextFormat()
{
HRESULT hr = S_OK;
if (!mpTextFormat)
{
CalculateFontSize();
hr = mpWriteFactory->CreateTextFormat(
L"Arial",
NULL,
DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL,
mFontSize, // <-- Specifies font size in DIP's..
L"en-us",
&mpTextFormat);
}
return hr;
}
Here is test program that shows the differences in font size between default system font in window caption and custom button text below.
I need help in figuring out how to correctly calculate font size for IDWriteTextFormat parameter to be of same size as text in other common controls that are not BS_OWNERDRAW or in this example default font in window caption.
EDIT:
I figured out issue is in my CalculateFontSize() I wrote mFontSize = static_cast<float>(font.lfHeight); but this is negative number so appending - sign gives the expected result:
mFontSize = static_cast<float>(-font.lfHeight);
Why negative? I'm not sure yet but this answer helped:
How to set font size using CreateFontA?
Now my question remains in that how should I update CalculateFontSize() so that it gets font size of common controls that are not BS_OWNERDRAW instead of a window caption bar font size?
I figured out to calculate font size of other common controls to be used for IDWriteTextFormat the formula is simple:
float CustomControl::CalculateFontSize()
{
const long units = GetDialogBaseUnits();
const DWORD height = HIWORD(units);
return static_cast<float>(height);
}
Only problem with this is if you use custom font for common controls, or if your dialog uses different font then you need to update your CalculateFontSize() to take these changes into account.
However for your TextFormat to be truly consistent with native common controls you also need to apply font weight (boldness), for example after you create TextLayout (by using your TextFormat):
std::size_t caption_len = 0;
StringCchLengthW(mCaption, STRSAFE_MAX_CCH, &caption_len);
DWRITE_TEXT_RANGE range = { 0u, caption_len };
mpTextLayout->SetFontSize(mFontSize, range);
mpTextLayout->SetFontWeight(DWRITE_FONT_WEIGHT::DWRITE_FONT_WEIGHT_BOLD, range);
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.
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);
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.
I am trying to display an image in a dialog dynamically, it works no problem if I put the code in the on paint method and use the dc from there, I can't do this though I need to display after the window is shown, the code I am using is as follows, I am getting the dc of the client window creating the bitmap from a resource and "trying" to display it in the window but nothing displays, Any suggestions what might be wrong?
void CProcessSteps::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDialog::OnShowWindow(bShow, nStatus);
SetupInstructions();<-----------------Call To Method
}
void CProcessSteps::OnPaint()
{
CPaintDC dc(this);
}
void CProcessSteps::SetupInstructions()
{
CDC *pDC = new CDC();<------------------------------Problem starts here
CFontUtil cfu;
cfu.SetFont(&LineFont,30);
CDC memDC;
memDC.CreateCompatibleDC(pDC);
int stepTop = 10;
int stepEnd = 230;
int imageLeft = 30;
STEP_STRUCT* step;
CBitmap iconImage;
iconImage.LoadBitmap ( IDB_TID_CHECK );
memDC.SelectObject(&iconImage);
CRect iconRect;
BITMAP bmInfo;
iconImage.GetObject ( sizeof ( bmInfo ), &bmInfo );
iconRect.SetRect ( imageLeft, stepTop, imageLeft+bmInfo.bmWidth, stepTop+bmInfo.bmHeight );
pDC = this->GetDC();
pDC->BitBlt(imageLeft, stepTop, imageLeft+bmInfo.bmWidth, stepTop+bmInfo.bmHeight, &memDC, 0, 0, SRCCOPY);
//RedrawWindow();<-------- tried this here no luck
int stepCount = m_pageStructure->PageSteps.GetCount();<----------------------------Bellow this works correctly
POSITION pos = m_pageStructure->PageSteps.GetHeadPosition();
while (pos)
{
step = m_pageStructure->PageSteps.GetNext(pos);
CStatic *label = new CStatic;
label->Create(_T( step->StepInstruction ),WS_CHILD | WS_VISIBLE, CRect(80, stepTop, 480, stepEnd), this);
label->SetFont(&LineFont, true);
label->GetWindowRect(rect);
ScreenToClient(rect);
pDC = label->GetDC();
pDC->SelectObject(&LineFont);
pDC->DrawText(step->StepInstruction, &rect, DT_CALCRECT|DT_WORDBREAK);
label->ReleaseDC(pDC);
label->MoveWindow(rect);
stepTop += rect.Height();
stepTop += 30;
stepEnd += rect.Height();
}
}
Reasons why you can't use OnPaint() are not clear.
The usual strategy when one needs to redraw all or part of a window upon some event is to call InvalidateRect().
Windows will in turn send WM_PAINT (handled by your OnPaint() method) to your app, specifying which part of the window should be redrawn.
I think there's more in the BeginPaint-function than just giving you the CDC. And BeginPaint can only be called from the OnPaint-method.
To solve your problem, use the Invalidate-functions to force a repaint from your "SetupInstructions" method. Then do the drawing inside the OnPaint function.
I suppose CProcessSteps derives from CWnd, perhaps a CDialog?
If you want to draw in the client area of a CWnd derived class you have to get the DC using the CWnd GetDC method. I don't understand why you create your own CDC, you should get the CWnd DC at the beginning of SetupInstructions and use this DC everywhere, also to create your memDC.
Also you should be careful when you allocate memory (new CStatic) if you don't call delete for this variables you will have memory leaks. If you really need to create this CStatics dynamically you will have to keep a pointer to all of them in order to delete them before closing the dialog/view.
As people suggested, I don't think you are following the right way by drawing using OnShowWindow. You should use OnPaint to make your drawing stuff, if you don't want to draw the image until the window is fully initialized you should use a member variable of the window (for instance a bool) initialized to false in the constructor and set it to true when you are ready to draw the image. Then calling Invalidate will draw the image. Something like:
In the .h:
class CProcessSteps : CDialog
{
...
private:
bool m_bReadyToDraw;
};
In the .cpp:
CProcessSteps::CProcessSteps() : CDialog()
{
m_bReadyToDraw = false;
}
BOOL CProcessSteps::OnInitDialog()
{
CDialog:OnInitDialog();
m_bReadyToDraw = true;
return TRUE;
}
void CProcessSteps::OnPaint()
{
CPaintDC dc(this);
if(m_bReadyToDraw)
{
CFontUtil cfu;
cfu.SetFont(&LineFont,30);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
...
}
}
Hope it helps.
Javier