Win32 Text Output Example

Note: I am not asking for any help, I am just posting an example for those who want to learn this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// How to print text in Win32 C/C++
// Auther: -LeetGamer-

#include <Windows.h>

HINSTANCE hInstance;

// Function I made to get the size of the text
int GetTextSize (LPSTR a0)
{
    for (int iLoopCounter = 0; ;iLoopCounter++)
    {
        if (a0 [iLoopCounter] == '\0')
            return iLoopCounter;
    }
}

LPSTR TextArray [] = {
    "Hello World"
};

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;
        
        case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint (hwnd, &ps);
            TextOut (hdc,
                     // Location of the text
                     10,
                     10,
                     // Text to print
                     TextArray [0],
                     // Size of the text, my function gets this for us
                     GetTextSize (TextArray [0]));
            EndPaint (hwnd, &ps);
        }
        break;
    }
    return DefWindowProc (hwnd, msg, wParam, lParam);
}

int WINAPI WinMain (HINSTANCE hInstanace, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX WindowClass;
    WindowClass.cbClsExtra = 0;
    WindowClass.cbWndExtra = 0;
    WindowClass.cbSize = sizeof (WNDCLASSEX);
    WindowClass.lpszClassName = "1";
    WindowClass.lpszMenuName = NULL;
    WindowClass.lpfnWndProc = WndProc;
    WindowClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    WindowClass.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    WindowClass.hCursor = LoadCursor (NULL, IDC_ARROW);
    WindowClass.style = 0;
    WindowClass.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
    RegisterClassEx (&WindowClass);

    HWND hwnd = CreateWindowEx (WS_EX_CLIENTEDGE,
                                "1",
                                "Printing Text in Win32 C/C++",
                                WS_OVERLAPPEDWINDOW,
                                315, 115,
                                640, 480,
                                NULL,
                                NULL,
                                hInstance,
                                NULL);

    ShowWindow (hwnd, SW_SHOWNORMAL);

    MSG msg;

    while (GetMessage (&msg, NULL, 0, 0) > 0)
    {
        TranslateMessage (&msg);
        DispatchMessage (&msg);
        if (VK_ESCAPE == msg.wParam)
            break;
    }
    return 0;
}


If you have any questions let me know.
#1 - Why not use strlen?
#2 - Why have an array of strings when you're outputting one?
#3 - static and const are awesome keywords.
#4 - You should be handling the escape key within WinProc, not in your GetMessage loop.
Last edited on
1) Never heard of it :P edit: nvm lol I know that, but I learned it a long time ago and never thought to use it, plus I like making my own functions if I can
2) Because I usually have a lot of strings that I put in an array so I just used an array, but in this case I only had one
3) ok....
4) I have been doing that for a wile because of when people are editing some window text and they press esc it doesn't close so I just put it there, there really isn't a difference anyway...
Last edited on
 
4) I have been doing that for a wile because of when people are editing some window text and they press esc it doesn't close so I just put it there, there really isn't a difference anyway...


Its wrong.
What do you mean "Its wrong." ? =/
If you have a program with multiple Windows you will have problems. The message loop services all messages for all windows in your primary thread. If you have a program that opens a main program window, which then - due perhaps to some user activity - opens another window, hitting the escape key will not only close the 2nd window, but rather the whole program. I don't use switch constructs to map my message handlers to windows messages, but here is what you should have....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <windows.h>

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch(message)
 {
   case WM_CHAR:
     {
        if(wParam==VK_ESCAPE)
           SendMessage(hwnd,WM_CLOSE,0,0);
        return 0;
     }
   case WM_PAINT:
     {
        PAINTSTRUCT ps;
        HDC hDC;
        char szBuffer[]="Hello, World!";
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,10,10,szBuffer,strlen(szBuffer));
        EndPaint(hwnd,&ps);
        return 0;
     }
   case WM_DESTROY:
     {
        PostQuitMessage(0);
        return 0;
     }
 }

 return DefWindowProc (hwnd, message, wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hIns, HINSTANCE hPrev, LPSTR lpszArgument, int nCmdShow)
{
 char szClassName[]="Form6";
 WNDCLASSEX wc;
 MSG messages;
 HWND hwnd;

 wc.hInstance=hIns;
 wc.lpszClassName=szClassName,                         wc.lpfnWndProc = WndProc;
 wc.style = CS_DBLCLKS,                                wc.cbSize = sizeof (WNDCLASSEX);
 wc.hIcon = LoadIcon (NULL, IDI_APPLICATION),          wc.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
 wc.hCursor = LoadCursor (NULL, IDC_ARROW),            wc.lpszMenuName = NULL;
 wc.cbClsExtra = 0,                                    wc.cbWndExtra = 0;
 wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
 RegisterClassEx(&wc);
 hwnd=CreateWindow(szClassName,szClassName,WS_OVERLAPPEDWINDOW,200,200,444,375,HWND_DESKTOP,NULL,hIns,NULL);
 ShowWindow(hwnd, nCmdShow);
 while(GetMessage(&messages, NULL, 0, 0))
 {
    TranslateMessage(&messages);
    DispatchMessage(&messages);
 }

 return messages.wParam;
}
Heres an example of how to draw text in the center of your window, and set the font/colour/transparent background

1
2
3
4
5
6
7
8
9
	RECT rc;
	GetClientRect(hwnd, &rc);

	SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
	SetBkMode(hdc, TRANSPARENT);
	SetTextColor(hdc, RGB(255, 0, 0));

	std::string strOut = "Hello World!"; // or wstring if you have unicode set
	DrawText(hdc, strOut.c_str(), strOut.length(), &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);


if you want your custom font then check out the CreateFont() api, but remember to use DeleteObject on the HFONT when you finish.
Last edited on
Topic archived. No new replies allowed.