[wxWidgets] Add an icon from .ico file

Posted on Dec 21, 2014

YourApp.rc

mondrian ICON "mondrian.ico" // identifier ICON filename

#include "wx/msw/wx.rc"

YourApp.cpp

// In the constructor,
MyDialog::MyDialog(wxWindow* parent)
{
	...

	SetIcon(wxICON(mondrian)); // the same identifier that you put in the rc file
}

[wxWidgets] Add “About…” to the system menu

Posted on Dec 21, 2014

Original post: https://forums.wxwidgets.org/viewtopic.php?f=20&t=13921

MyDialog.h

class MyDialog : public wxDialog
{
	...

	// Override MSWTranslateMessage
	bool MSWTranslateMessage(WXMSG* pMsg);
};

MyDialog.cpp

// Include aboutdlg.h
#include <wx/aboutdlg.h>

MyDialog::MyDialog(wxWindow* parent)
{
	...

	// Add the menu item, "About...", to the system menu
	HMENU hSystemMenu = ::GetSystemMenu((HWND__*)GetHWND(), FALSE);
	::AppendMenu(hSystemMenu, MF_SEPARATOR, 0, wxT(""));
	::AppendMenu(hSystemMenu, MF_STRING, wxID_ABOUT, wxT("About..."));
	::DrawMenuBar((HWND__*)GetHWND());
}

// implement the message handler
bool MyDialog::MSWTranslateMessage(WXMSG* pMsg)
{
	if (WM_SYSCOMMAND==pMsg->message && wxID_ABOUT==pMsg->wParam)
	{
		wxAboutDialogInfo di;
		di.SetName(wxT("My App"));
		di.SetVersion(wxT("1.00"));
		di.SetDescription(wxT("This app does cool trix!"));

		wxAboutBox(di);

		return true; // Message processed
	}

	return wxDialog::MSWTranslateMessage(pMsg);
}

Conversion between UTF-16 and UTF-8 in C++

Posted on Jun 5, 2014

#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>

// Unicode representation in MS Windows uses the 2-byte wchar_t type.
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> utfconv;

// string conversion
std::wstring wide = L"Hello, World! 안녕하세요?";  // wide string with utf-16 encoding
std::string narrow = utfconv.to_bytes(wide);     // conversion from utf-16 to utf-8
wide = utfconv.from_bytes(narrow);               // back from utf-8 to utf-16

// conversion during file I/O
std::wofstream fout;                             // wide output stream
fout.open("test.txt", fout.out);
fout.imbue(std::locale(fout.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
fout << wide << std::endl;                       // this stream is stored as utf-8
fout << utfconv.from_bytes(narrow) << std::endl; // the same as the above line
fout.close();

std::wifstream fin;
fin.open("test.txt", fin.in);
fin.imbue(std::locale(fin.getloc(), new std::codecvt_utf8_utf16<wchar_t>));
std::wstring hello, world, anyoung, tline;
fin >> hello >> world >> anyoung;      // utf-8 stream is converted to utf-16 string
std::getline(fin, tline);              // read out the end of the line
std::getline(fin, tline);              // read the next line
fin.close();

Printing Unicode strings in Windows console

Posted on May 12, 2014

Win32 Console Applications do not display Unicode strings properly. There is a simple solution of using _setmode(). You need to include two headers.

#include <io.h>
#include <fcntl.h>

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	_setmode(_fileno(stdout), _O_U16TEXT);

	wcout << L"안녕하세요?" << endl;
	// or
	// wprintf(L"%s\n", L"안녕하세요?");

	return 0;
}