PDA

Click to See Complete Forum and Search --> : Tutorial: Displaying Text in win32


slx47
May 23rd, 2002, 07:57 PM
#include <windows.h>

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)



{

WNDCLASS WndClass;
WndClass.style = 0;
WndClass.cbClsExtra = 0;
WndClass.cbWndExtra = 0;
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = hInstance;
WndClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
WndClass.hCursor = 0;
WndClass.hIcon = 0;
WndClass.lpszMenuName = 0;
WndClass.lpszClassName = "WinProg";

RegisterClass (&WndClass);

HWND hWindow;

hWindow = CreateWindow ("WinProg","Window",WS_OVERLAPPEDWINDOW,0,0,400,400,NULL,NULL,hInstance,NULL);

ShowWindow(hWindow, nCmdShow);

UpdateWindow (hWindow);

MSG Message;

while (GetMessage(&Message,NULL,0,0))
{
DispatchMessage(&Message);
}
return(Message.wParam);
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
{
switch(uiMessage)
{

case WM_DESTROY:

PostQuitMessage(0);
return 0;
break;

default:
return DefWindowProc(hWnd, uiMessage, wParam, lParam);

case WM_PAINT:

HDC hdc;
PAINTSTRUCT ps;
hdc = BeginPaint (hWnd, &ps);
hFont = (HFONT) GetStockObject (SYSTEM_FONT);
SelectObject (hdc,hFont);
SetTextColor(hdc,RGB(255,0,255));
//if you want a background then remove "//" on next line
//SetBkColor(hdc, RGB(255,0,0));
SetTextAlign (hdc, TA_LEFT);
Char *string1;
string1 = new char[12];
lstrcpy(string1,"SYSTEM_FONT");
TextOut(hdc,10,10,string1, lstrlen(string1));
}
}


Description:
HDC hdc;

This is the handle to the device context object.

PAINTSTRUCT ps;

structuce:

typedef struct PAINTSTRUCT

{

HDC hdc;
BOOL fErase;
RECT rcPaint;
BOOL fRestore;
BOOL fIncUpdate;
BOOL rgbReserved[32];
} PAINTSTRUCT;

hdc = ^ look above for description

fErase = is NULL if the area of the invalid box was redrawn using hbcBackground from the WNDCLASS class.

rcPaint = it contains info for the invalid box.

Rest = not important, used by system.

Begin Paint = setup up the area for painting.

hFont = the font to use.

SelectObject = get the current preferences for use.

SetTextColor = just as it is written, it changes the text color.

SetTextAlign = Alignment of the text in a area.

Char *string1;

declares a string.

string1 = new char[12];

Allocates some memory for this pointer string.

lstrcpy function simply sets string1 up with a value.

textout simply displays the text on the window.

Format:

TextOut(HDC,x,y,string,string length);