I don't understand this. Please help.

Pages: 12
Got it to work. Moved definition of HWND hWnd to global from WinMain. Make sense since it set in WinMain and used in WriteText. Lesson learned. Thanks to everybody for their idea's and help.

Final code:


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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <windows.h>

// Make global

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

static char gszClassName[]  = "darkblue";
static HINSTANCE ghInstance = NULL;

#define ID_5SECONDS 101
UINT TimerID = 0;
BOOL bFlag   = TRUE;
HWND hWnd;

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

        ghInstance = hInstance;

        WndClass.cbSize        = sizeof(WNDCLASSEX);
        WndClass.style         = NULL;
        WndClass.lpfnWndProc   = WndProc;
        WndClass.cbClsExtra    = 0;
        WndClass.cbWndExtra    = 0;
        WndClass.hInstance     = ghInstance;
        WndClass.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        WndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
        WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        WndClass.lpszMenuName  = NULL;
        WndClass.lpszClassName = gszClassName;
        WndClass.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

        if(!RegisterClassEx(&WndClass)) {
                MessageBox(0, "Window Registration Failed!", "Error!", MB_ICONSTOP | MB_OK);
                return 0;
        }

        hWnd = CreateWindowEx(
                WS_EX_STATICEDGE,
                gszClassName,
                " Test Program",
                WS_OVERLAPPEDWINDOW,
                CW_USEDEFAULT, CW_USEDEFAULT,
                320, 240,
                NULL, NULL,
                ghInstance,
                NULL);

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

        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);

    	// set up our timer
        TimerID = SetTimer( hWnd , ID_5SECONDS , 1 *1000 , NULL);

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

class WriteText
{
public:

    HDC hdc;
    PAINTSTRUCT ppaint;

    WriteText(int iXpos, int iYpos)
    {
        LPSTR szMessage_1 = "Text string #1!";
        LPSTR szMessage_2 = "Text string #2!";

        hdc = BeginPaint(hWnd, &ppaint);
        if (bFlag == TRUE) {
            TextOut(hdc, iXpos, iYpos, szMessage_1, strlen(szMessage_1));
        } else {
            TextOut(hdc, iXpos, iYpos, szMessage_2, strlen(szMessage_2));
        }
        EndPaint(hWnd, &ppaint);
    }
};

    LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {

        switch(Message) {
                case WM_PAINT:
                        WriteText(70,50);
                        break;
        		case WM_TIMER:
                       // Flip between TRUE AN FALSE every 1 soconds
                        bFlag = !bFlag;
                       // Call WM_PAINT when free
                        InvalidateRect(hWnd, NULL, NULL);
                        break;
                case WM_CLOSE:
                        DestroyWindow(hWnd);
                        break;
                case WM_DESTROY:
                        PostQuitMessage(0);
                        break;
                default:
                        return DefWindowProc(hWnd, Message, wParam, lParam);
        }
        return 0;
}
Topic archived. No new replies allowed.
Pages: 12