Make icon above text for MFC Button - c++

I wanted to create CMFCButton dynamically at runtime (Icon with Text on the button). The icon is created successfully but I wanted to display the icon above text.
I want to implement "Image on Top" property found in Resource Editor for the button.
My Code:
CMFCButton* appButton = new CMFCButton;
appButton->Create( _T("MfcButton1"), WS_CHILD | WS_VISIBLE, CRect(10, 10, 70, 50), this );
appButton->SetIcon( sfi.hIcon );

(according to the "NewControls" MFC sample).
To set an image in a CMFCButton use CMFCButton::SetImage.
To set the image above (or below) the text you can use the undocumented variable m_bTopImage
appButton->m_bTopImage = TRUE;
FYI: the complete samples can be downloaded from :
http://www.microsoft.com/en-us/download/details.aspx?id=5718

Related

CRichEditCtrl 50W font issues

I'm dynamically (in runtime) creating a CRichEditCtrl control and when I set the text to it, the font is different from the other controls (e.g. CStatic - see the pictures) in the dialog (I didn't use any formatting for the rich edit control). The difference is best seen with the characters from alphabets like Chinese or Japanese. I tried using SetDefaultCharFormat method but it was not having the effect I wanted. Then I found out, that I first need to set the text to the rich edit control, and only then use the formatting function.
// 1. dynamically created rich edit ctrl
CRichEditCtrl* dynamic_rich = new CRichEditCtrl();
dynamic_rich->Create(WS_CHILD | WS_TABSTOP | ES_LEFT | ES_MULTILINE | WS_VSCROLL |
WS_HSCROLL | ES_WANTRETURN | WS_VISIBLE | WS_BORDER | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
CRect(10, 0, 184, 23), this, 1234);
dynamic_rich->ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_DRAWFRAME);
dynamic_rich->SetWindowTextW(L"アラビア語"); // calling it here results in the 3rd picture
// which is the closest to the desired result, though it's still not the same as in CStatic
CHARFORMAT cf = { 0 };
dynamic_rich->GetDefaultCharFormat(cf);
_tcscpy_s(cf.szFaceName, L"MS Shell Dlg 2");
cf.dwMask = CFM_FACE;
dynamic_rich->SetDefaultCharFormat(cf);
// 2. statically created rich edit ctrl - defined in .rc file
CRichEditCtrl* static_rich = (CRichEditCtrl*)this->GetDlgItem(IDC_RICHEDIT);
static_rich->SetWindowTextW(L"アラビア語");
// 3. statically created cstatic - for the font comparison
CStatic* label = (CStatic*)this->GetDlgItem(IDC_STATIC);
label->SetWindowTextW(L"アラビア語");
When I leave the default font (without calling SetDefaultCharFormat) - seems blurry
When I call SetDefaultCharFormat, then set the text - the same result
When I set the text, then call SetDefaultCharFormat - not quite the same, but close
The font in the rich edit control still doesn't look the same as in CStatic (seems a bit different and also bigger), but that I can live with. Calling SetDefaultCharFormat every time I change a text is a problem though because I need to be constantly changing the text in the runtime and it could potentially cause an overhead. Does anybody have a better solution? Thanks.

Button TABSTOP in ownerdraw not working

I created a custom button class CMyButton inherited from CButton, then I am using the DrawItem to customize the button.
Using the custtom button, I created 3button on dialog.
The issue is that the TAB key for the button is not working. If I remove drawitem then there is no issue. Can anyone please help on this?
void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rect = lpDrawItemStruct->rcItem;
UINT state = lpDrawItemStruct->itemState;
pDC->DrawFrameControl(rect, DFC_BUTTON , DFCS_BUTTONPUSH | DFCS );
pDC->FillSolidRect(rect, RGB(24, 72, 76));
pDC->SetTextColor(RGB(255, 255, 255));
CString strText;
GetWindowText(strText);
pDC->DrawText(strText,rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
}
I doubt it. If you make a button owner draw, then you are responsible for drawing it all the time. This includes the focused state, etc. See the documentation for DRAWITEMSTRUCT.itemstate.
https://msdn.microsoft.com/en-us/library/windows/desktop/bb775802%28v=vs.85%29.aspx
The focus rectangle is automatically rendered for standard controls on a dialog. If you subscribe to owner-drawing, responsibility to render visual cues is shifted to the custom implementation. While TABbing still works, keyboard focus remains invisible unless the implementation explicitly accounts for it.
You can use the DRAWITEMSTRUCT passed to your DrawItem method to query the item's state. If itemState contains the ODS_FOCUS flag, the control being rendered has the keyboard focus, and should produce the desired visual cue.
As a simple illustration, replace your call to FillSolidRect with the following code:
if ( state & ODS_FOCUS )
// Control has keyboard focus -> render it green
pDC->FillSolidRect( rect, RGB( 0, 255, 0 ) );
else
// Control doesn't have keyboard focus -> render it red
pDC->FillSolidRect( rect, RGB( 255, 0, 0 ) );
This allows you to see, that the TAB key does work as expected: The button control with keyboard focus is rendered green, whereas all other buttons are red.
If you are looking for a more standard appearance you can call CDC::DrawFocusRect (or DrawFocusRect) instead.

Change static background in tab color c++

I have create tab
INITCOMMONCONTROLSEX icc;
icc.dwSize = sizeof(INITCOMMONCONTROLSEX);
icc.dwICC = (DWORD)ICC_TAB_CLASSES;
InitCommonControlsEx(&icc);
icc.dwICC = (DWORD)ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icc);
hwndTab = CreateWindow(WC_TABCONTROL,L"",WS_CHILD|WS_VISIBLE|WS_DLGFRAME|WS_CLIPSIBLINGS
,10,10,780,271,hwnd,(HMENU)3,hInstance,NULL);
TCITEM tcitem; tcitem.mask = TCIF_TEXT;
tcitem.pszText = L"Tab1";
TabCtrl_InsertItem(hwndTab,0,&tcitem);
and put a static control into the tab
CreateWindow(L"STATIC",L"Static control 1",
WS_CHILD|WS_VISIBLE,50,30,65,24,hwndTab,(HMENU)NULL,hInstance,NULL);
But the Static control 1 have the grey background. How can I give it a color.
Thanks
Because you've made the static control a child of the tab it will be sending WM_CTLCOLORSTATIC messages to the tab control. If you sub-class the tab control you can catch this message and modify its colours. Note however that with visual styles enabled the tab control isn't a flat single colour - it's more of a gradient, so even returning a matching solid colour won't necessarily look that great.
Tab controls are not meant to be parents. For each tab page, you should create a new modeless dialog as a sibling to the tab and place your static control in it. If you obey the following rules:
reference Common Controls v6 in your manifest
call InitCommonControlsEx() on startup
no WS_CLIPCHILDREN in your main window
no handling of WM_ERASEBACKGROUND
call EnableThemeDialogTexture() in the WM_INITDIALOG handler of the modeless dialog
call SetWindowPos(tab, HWND_TOP, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE) after creating the tab page (or it will be on the wrong end of the Z order)
… then your static control will be drawn with the correct color gradient, as will all other controls (checkboxes, sliders, …) in the modeless dialog, on all Windows version from XP to 10.

MFC:set font for dialog childern

I am tring to create dynamic cntrl in dialog
like CStatic,CButton like below:
ProtocolName_ = new CStatic();
ProtocolName_->Create(protocolNameStr,WS_CHILD | WS_BORDER | WS_VISIBLE,rectTitle,parent,clac_id(index,1));
Start_ = new CButton();
Start_->Create(L"Start",WS_CHILD | WS_BORDER|WS_VISIBLE|BS_AUTOCHECKBOX,rectTitle,parent,clac_id(index,3));
but thay font is other from dialog
why ?
how I can repair the case or set the font?
When do you create your controls? Make sure you create them in CYourDlg::OnInitDialog(). Also, pay attention to the DS_SETFONT style of your dialog resource.
Read more here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms632642(v=vs.85).aspx

MFC MDI tabbed application - want to add non-document windows to the tab group

I have an MFC MDI application I've developed in Visual Studio with a tabbed interface. I would like to open views in the tab group that are non-document views – i.e. they have no associated document, no need to save them, etc. In a way they would behave like a non-modal dialog, but tabbed. [These windows are simply to display information and take commands]
The internal machinery of the MDI apps seems very geared toward working with the DocTemplate – Document – Frame – View object structures along with their associated windows.
Q1) Anybody got any ideas on how to create such windows and add them into the already-established MDI tab group? I’ve tried to create a RichEdit window and added it in, with:
// m_wndListingView will be a non-editable CRichEditCtrl
m_wndListingView->Create(WS_CHILD | WS_VISIBLE | ES_WANTRETURN | WS_VSCROLL |
WS_HSCROLL | ES_MULTILINE | ES_LEFT | ES_AUTOHSCROLL | ES_SAVESEL |ES_READONLY,
CRect(0, 0, 20, 20), pMainFrame, 1234);
// get Tab control and add a new tab
CMFCTabCtrl *mm_wndTabCtrl = &pMainFrame->GetMDITabs();
mm_wndTabCtrl->AddTab (m_wndListingView, _T("LISTING"));
This created and displayed the window .. but it was not added to the tab group.
Q2) If I managed to get a window (perhaps it needs to be a frame window) displayed properly in the tab group, how do I tell the ‘system’ that when the user closes it, I do not want the app to prompt the user to Save the document ? Perhaps I can overload an 'OnClose' method ... but it can't be document::OnClose(), because there is no document.
Thanks for any ideas,
CAS
You need to create a frame and view on which to host your rich edit. This can be done without a document. The view will be the parent of the richedit (rather than pMainFrame).
Something along these lines (warning, untested):
CFrame* pFrame = (Crame*)RUNTIME_CLASS( CFrame )->CreateObject();
CCreateContext context;
context.m_pNewViewClass = RUNTIME_CLASS( CView );
context.m_pCurrentDoc = NULL;
context.m_pCurrentFrame = NULL;
context.m_pLastView = NULL;
context.m_pNewDocTemplate = NULL;
// NOTE: create IDR_SOMERESOURCE string (for tab title), menu, etc as needed
BOOL frameLoaded = pFrame->LoadFrame( IDR_SOMERESOURCE, WS_OVERLAPPEDWINDOW, pMainFrame, &context );
if (frameLoaded)
Frame->InitialUpdateFrame( NULL, TRUE );
// now create your rich edit with the view as its parent