Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,259 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,186 people online right now. Registration is fast and FREE... Join Now!




Writing Text in Win32 GUI

 
Reply to this topicStart new topic

Writing Text in Win32 GUI

dwayne
post 22 Aug, 2008 - 12:48 PM
Post #1


New D.I.C Head

*
Joined: 2 Aug, 2008
Posts: 39


My Contributions


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.

C++
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;
}
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 22 Aug, 2008 - 02:55 PM
Post #2


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


I noticed this line:

cpp

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:

cpp

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


That's all for now.
User is online!Profile CardPM

Go to the top of the page

dwayne
post 22 Aug, 2008 - 04:46 PM
Post #3


New D.I.C Head

*
Joined: 2 Aug, 2008
Posts: 39


My Contributions


Thank you. Thanks to you I still have hair (i.e. I didn't pull it all out).
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 11:06PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month