Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am doing an enhancement on an application that uses a CDateTimeCtrl, CMonthCalCtrl and SysDateTimePick32.

The new requirement is to localize the CDateTimeCtrl, CMonthCalCtrl, SysDateTimePick32 to the Brazilian Portuguese locale such that these controls should show the day and month names in Brazilian Portuguese language.

I can not change the locale of the Operating System.

I tried using the SetThreadLocale(0416), but it did not work.

The few modules of this application is built using WPF and there altering the thread culture helped me to achieve my requirement. But, I am stuck with these controls.

These controls might not localization on thread level but I am not sure.

Any help on this is much appreciated as I am running out of time.
Posted
Updated 27-Aug-15 20:40pm
v4

1 solution

At least with older Windows versions this is not possible (uses always the Windows user locale). This cite from DTM_SETFORMAT[^] indicates that it might be possible with Vista and later when not using user defined formats:
Quote:
A DTP control tracks locale changes when it is using the default format string. If you set a custom format string, it will not be updated in response to locale changes.
But that might apply also only to the user locale.

For the CDateTimeCtrl you can derive your own class to perform the formatting of the date/time string (untested):
class CMyDateTimeCtrl : public CDateTimeCtrl
{
	DECLARE_DYNAMIC(CMyDateTimeCtrl)
public:
// ...
	void Init(const COleDateTime& dt);
// ...
protected:
	COleDateTime	m_dt;

	afx_msg void	OnDtnFormat(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void	OnDtnFormatQuery(NMHDR *pNMHDR, LRESULT *pResult);
};

BEGIN_MESSAGE_MAP(CMyDateTimeCtrl, CDateTimeCtrl)
	ON_NOTIFY_REFLECT(DTN_FORMAT, OnDtnFormat)
	ON_NOTIFY_REFLECT(DTN_FORMATQUERY, OnDtnFormatQuery)
END_MESSAGE_MAP()

void CMyDateTimeCtrl::Init(const COleDateTime& dt)
{
	// Must set the value before the format.
	// The format callback handlers need a valid date/time!
	m_dt = (dt.GetStatus() == COleDateTime::valid) ? dt : COleDateTime::GetCurrentTime();
	VERIFY(SetTime(m_dt));
	VERIFY(SetFormat(_T("X"));
}

void CMyDateTimeCtrl::OnDtnFormat(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMEFORMAT pDTFormat = reinterpret_cast<LPNMDATETIMEFORMAT>(pNMHDR);
	pDTFormat->szDisplay[0] = _T('\0'); // 64 character buffer
	// Create string here passing the locale, the format, and 
	//  m_dt.GetAsSystemTime() to GetDateFormat(Ex) or GetTimeFormat(Ex) resp. 
	// The format string can be hard coded or retrieved using GetLocaleInfo(Ex).
	*pResult = 0;
}

void CMyDateTimeCtrl::OnDtnFormatQuery(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMDATETIMEFORMATQUERY pDTFmtQuery = reinterpret_cast<LPNMDATETIMEFORMATQUERY>(pNMHDR);

	// Set pDTFmtQuery->szMax.cx and pDTFmtQuery->szMax.cy here
	
	// Dynamic calculation (LPCTSTR lpszMaxText is max. length text):
	CDC * pDC = GetDC();
	CFont * pFont = GetFont();
	CFont Font;
	if (!pFont)
	{
		VERIFY(Font.CreateStockObject(DEFAULT_GUI_FONT));
		pFont = &Font;
	}
	CFont * pOrigFont = pDC->SelectObject(pFont);
	VERIFY(::GetTextExtentPoint32(pDC->m_hDC,
		lpszMaxText, _tcslen(lpszMaxText),
		&pDTFmtQuery->szMax));
	pDC->SelectObject(pOrigFont);
	ReleaseDC(pDC);
	*pResult = 0;
}}
 
Share this answer
 
Comments
Member 3889407 30-Aug-15 2:49am    
Thanks for your response. My requirement is not with the date and time formatting.

The requirement is to change the CDateTimeCtrl, CMonthCalCalCtrl's look and feel similar to what we get when we set the user locale to Brazilian Portuguese.

But I want to do that without changing the user locale. Probably by means of thread culture which the new WPF DatePicker and Calendar control allow.

Can we achieve that with just tweaking the thread culture or with the help of any other methods?

Your views on this will be much appreciated.

Thanks.
Jochen Arndt 31-Aug-15 3:19am    
I understand your requirements. I posted the code to show how you can at least change the format of the input field to another locale (e.g. the one used by the current thread).

If the new controls still use the user locale, there will be no solution to change their behaviour. The only solution would be using your own controls (self written or from a 3rd party library).
Member 3889407 31-Aug-15 4:33am    
Hmm, even I thought so. Thanks for confirming that.

I am planning to use WPF controls in place of CDateTimeCtrl and CMonthCalCtrl as WPF controls are locale aware.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900