2 Replies - 7092 Views - Last Post: 22 August 2008 - 05:46 PM Rate Topic: -----

#1 dwayne  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 02-August 08

Writing Text in Win32 GUI

Posted 22 August 2008 - 01:48 PM

I basically need to write text out to the white space in an empty window. I am attempting to use "TextOut" at the moment but get so many errors... I am new to GUI Programming, especially windows GUI design. Here is the code, for the time I am attempting to write a short string to the window though in my actual app I need to write various strings from the same variable (so I preferable should not be constant). My app was originally designed a console application though I made sure it could be easily redone into a GUI app later.

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	int nXStart;
	nXStart= 5;       // x-coordinate of starting position
	int nYStart;
	nYStart= 10;       // y-coordinate of starting position
	LPCWSTR lpString = "Random String";  // character string
	int cbString;
	cbString = 15;      // number of characters

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_about:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		TextOut(hdc, nXStart, nYStart, lpString, cbString);
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Writing Text in Win32 GUI

#2 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Writing Text in Win32 GUI

Posted 22 August 2008 - 03:55 PM

I noticed this line:

	LPCWSTR lpString = "Random String";  // character string



LPCWSTR is essentially defined to be const wchar_t* (or perhaps something else with the same result).

You could give a wchar_t* literal, by using the L prefix:
LPCWSTR lpString = L"Random String";

However, this is not the recommended way of doing it. It's usually better to do:

LPCTSTR lpString = _T("Random String");

(Make sure tchar.h is included).

This is because there's a seperate API for char* strings and for wchar_t* strings (UTF-16LE). Writing your code
like this will allow you to compile your program to use char* or wchar_t* just by defining or undefining a preprocessor symbol. If you look up the definition of TextOut on MSDN, you'll notice that it takes in a LPCTSTR parameter, so you don't need to change anything about the call to take advantage of this.

Also, your string is not 15 characters, but 13. I know that this is a test program, but you can free yourself from having to manually calculate the string length...

Instead of the above, do something more like this:

TCHAR lpString[] = _T("Random String");
// Subtracting 1 for the null terminator
int cbString = (sizeof(lpString) / sizeof(lpString[0])) - 1;



That's all for now.
Was This Post Helpful? 1
  • +
  • -

#3 dwayne  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 56
  • Joined: 02-August 08

Re: Writing Text in Win32 GUI

Posted 22 August 2008 - 05:46 PM

Thank you. Thanks to you I still have hair (i.e. I didn't pull it all out).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1