CRichEditCtrl 50W font issues - c++

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.

Related

Setting a CMFCPropertySheet as RTL

I have seen several similar question on this subject but I can seem to resolve it.
For example, on CodeProject:
https://www.codeproject.com/Messages/2873837/Re-How-to-set-RTL-layout-for-a-CPropertySheet.aspx
And on SO:
RTL layout issue for Property Sheets (MFC)
So, I have a CMFCPropertySheet that is my main application window and it is set to Arabic when the program starts:
The problem, as it the case for other users, is that whilst the pages are correctly set to RTL layout the sheet is not.
What is the correct way to get the sheet itself to display RTL?
I tried to use PreCreateWindow and it made no difference. I tried to use SetProcessDefaultLayout too. No joy.
Ideally, the window style should be changed in OnNcCreate before the window starts creating and positioning its child controls. This way, the child tab, as well as child buttons, will be positioned accordingly (OK/Cancel/Apply button will be aligned to the left side as well).
Example:
BEGIN_MESSAGE_MAP(...)
ON_WM_NCCREATE()
...
END_MESSAGE_MAP()
BOOL CMyPropertySheet::OnNcCreate(LPCREATESTRUCT pc)
{
BOOL res = CMFCPropertySheet::OnNcCreate(pc);
SetWindowLongPtr(m_hWnd, GWL_EXSTYLE,
WS_EX_LAYOUTRTL | GetWindowLongPtr(m_hWnd, GWL_EXSTYLE));
return res;
}
Alternatively, do this in OnInitDialog, use ::FindWindowEx(m_hWnd, 0, WC_TABCONTROL, 0) to find tab control's handle and change its style. This way the buttons are not re-positioned. Example:
BOOL CMyPropertySheet::OnInitDialog()
{
BOOL res = CMFCPropertySheet::OnInitDialog();
SetWindowLongPtr(m_hWnd, GWL_EXSTYLE,
WS_EX_LAYOUTRTL | GetWindowLongPtr(m_hWnd, GWL_EXSTYLE));
HWND htabctrl = ::FindWindowEx(m_hWnd, 0, WC_TABCONTROL, 0);
SetWindowLongPtr(htabctrl, GWL_EXSTYLE,
WS_EX_LAYOUTRTL | GetWindowLongPtr(htabctrl, GWL_EXSTYLE));
return res;
}
Side note:
You can also call SetProcessDefaultLayout(LAYOUT_RTL) at the start of the process (for example in CMyWinApp::InitInstance). Then change the layout depending on the result from GetProcessDefaultLayout. So you remember not to accidentally change the style for the Latin version...

(WIN32 API) Edit Control Style not applying to RichEdit in CreateWindowEx

I used the following code and it worked fine, allowed the user to only enter in numbers. I wanted to increase the functionality by using RichEdit so I added that.
I went from using:
wchar_t sampletext[] = L"foobar";
HWND inputText = CreateWindowEx(WS_EX_CLIENTEDGE, L"EDIT", sampletext,
ES_NUMBER | WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_MULTILINE,
10, 10, 500, 75, hWnd, NULL, *hInst, NULL);
to:
LoadLibrary(L"riched32.dll");
wchar_t sampletext[] = L"foobar";
HWND inputText = CreateWindowEx(WS_EX_CLIENTEDGE, L"RichEdit", sampletext,
ES_NUMBER | WS_CHILD | WS_VISIBLE | WS_TABSTOP | ES_MULTILINE,
10, 10, 500, 75, hWnd, NULL, *hInst, NULL);
Changing it allowed me to use CTRL+Z and CTRL+A and all but now for some reason I could enter characters that weren't numbers.
How do I fix this so Rich Edit only accepts number in this text field?
or
Alternatively, how would I create my own custom filter that would only accept numbers into the text field?
EDIT:
Here's an image of me typing
The "RichEdit" control class has its own set of styles.
But ES_NUMBER is one of them, according to MSDN. However, note the comment by ElmueSoft.
To filter input to only digits without help from the control, you can subclass it. You'd need to handle quite a few messages though. WM_CHAR is the most obvious, but WM_PASTE and WM_SETTEXT can have non-numeric text passed in.
Good information on subclassing:
http://blogs.msdn.com/b/oldnewthing/archive/2009/05/07/9592397.aspx
http://blogs.msdn.com/b/oldnewthing/archive/2003/11/11/55653.aspx

How to show caret in a disabled MFC CEdit control?

If an edit control is disabled, the caret disappears. that case, if the edit is displaying a long text, then it is not possible to see the rest of the words.With the caret i, we can do that.
I think you must use an enabled readonly edit control instead of a disabled one.
Check 'readonly' in the edit box property window or use ES_READONLY when calling Create.
Also, don't forget to check Auto HScroll (ES_AUTOHSCROLL) ( and AutoVScroll if multiline).
CEdit* pEdit = new CEdit;
pEdit->Create(ES_MULTILINE | WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER
| ES_AUTOHSCROLL | ES_READONLY, (10, 10, 100, 100), this, 1);

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