Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

phil05

Win32 API - Need Static Text Help

This topic is 5281 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I just learned how to create a blank window which took me all day. I''m trying to add text to it like in the old console applications "hello, world!" stuff. How do I do that? Here''s the source code...
#include <windows.h>

const char g_szClassName[] = "myWindowClass";

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_CLOSE: 	DestroyWindow(hwnd);
					   break;
		case WM_DESTROY: PostQuitMessage(0);
					     break;
		default: return DefWindowProc(hwnd, msg, wParam, lParam);
	}

	return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	WNDCLASSEX wc;
	HWND hwnd;
	MSG Msg;

	// Registering Window Class
	wc.cbClsExtra		= 0;
	wc.cbSize			= sizeof(WNDCLASSEX);
	wc.cbWndExtra		= 0;
	wc.hbrBackground	= (HBRUSH) (COLOR_WINDOW + 1);
	wc.hCursor			= LoadCursor(NULL, IDC_ARROW);
	wc.hIcon			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm			= LoadIcon(NULL, IDI_APPLICATION);
	wc.hInstance		= hInstance;
	wc.lpfnWndProc		= WndProc;
	wc.lpszClassName	= g_szClassName;
	wc.lpszMenuName		= NULL;
	wc.style			= 0;

	if (!RegisterClassEx(&wc))
	{
		MessageBox(NULL, "Registering Class Failed!", "Error:", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "World of Dragons", WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);

	if (hwnd == NULL)
	{
		MessageBox(NULL, "Window Creation Failed!", "Error:", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	ShowWindow(hwnd, nCmdShow);
	UpdateWindow(hwnd);

	while (GetMessage(&Msg, NULL, 0, 0) > 0)
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}
 

Share this post


Link to post
Share on other sites
Advertisement
Here is a website with what you want: Game Tutorials First tutorial will have what you want to accomplish.

Use the TextOut function:"TextOut(hdc, x, y, ""Hello World", length);"

BOOL TextOut(
HDC hdc, // handle of device context
int nXStart, // x-coordinate of starting position
int nYStart, // y-coordinate of starting position
LPCTSTR lpString, // address of string
int cbString // number of characters in string
);


So you could have something like this:

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
char text[]="Hello World";
static int length=0;
PAINTSTRUCT paintStruct;
HDC hDC=NULL;
switch (iMsg)
{
case WM_CREATE:
{
hdc = GetWindowDC(hwnd);
length = strlen(text);
ReleaseDC(hwnd, hdc);
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hwnd, &paintStruct);
TextOut(hdc, 0, 0, text, length);
EndPaint(hwnd, &paintStruct);
}
break;
//More stuff...

}
}


I think I have everything you need to write text, sorry I haven't written something in a window this way in a long time. I think GameTutorials and theForger will be good references; have fun.

Edit: Wrong variable name

[edited by - Xiachunyi on March 6, 2004 11:11:26 AM]
Edit: tutorial added

[edited by - Xiachunyi on March 6, 2004 11:28:55 AM]

Share this post


Link to post
Share on other sites
If you want to use an actual static text control (called a label in some other GUI apps, like Visual Basic) you''ll need to create a second window with the class name "STATIC"

Read the CreateWindow docs

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!