text drawn in a window

closed account (zwA4jE8b)
Some of you have seen this code in a previous post.

I am wondering is there a way to draw the text as a separate object? Bad wording I know. What I want is the text (line 165-167) displayed but not scrolled.

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//Michael Ervin - Windows Sine Wave

#include <Windows.h>
#include <math.h>
#include <string>
#include <sstream>

#define screen_width 800
#define screen_height 600
#define pi 3.14159

struct _points
{
	int _x;
	int _y;
};

struct _ycoords
{
	_points _one;
	_points _two;
} _yc;

COLORREF blue = RGB(0, 0, 255);
HDC hDC;
HWND hWnd;
std::string _amplitude, _frequency, _speed;

double _freq = 50;
int _sTime = 10, _amp = 100;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void calcwave();
void drawwave();
std::string convstr(const int& t);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	WNDCLASSEXA wc;

	ZeroMemory(&wc, sizeof(WNDCLASSEX));
	ZeroMemory(&_yc, sizeof(_yc));
	_yc._one._x = 600;
	_yc._two._x = 599;
	_yc._two._y = 300;

	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WindowProc;
	wc.hInstance = hInstance;
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hIcon = (HICON)LoadImageA(NULL, "C:\\Visual Studio 2010\\Icons\\Smiley.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
	wc.lpszClassName = "SineWave";

	RegisterClassExA(&wc);

	hWnd = CreateWindowExA(NULL,
                          "SineWave", "SineWave",
                          WS_SYSMENU,
                          0, 45,
                          screen_width, screen_height,
                          NULL, NULL,
                          hInstance,
                          NULL);

	ShowWindow(hWnd, nCmdShow);
	hDC = GetDC(hWnd);

	HPEN _Pen = CreatePen(PS_SOLID, 1, blue);
	SelectObject(hDC, _Pen);

	MSG msg;

	while(TRUE)
	{
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{DispatchMessage(&msg);}

		if(msg.message == WM_QUIT)
			break;

		calcwave();
	}
	return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		} break;
		case WM_KEYDOWN:
		{
			switch (wParam)
			{
			case VK_NUMPAD4:
				_freq--;
				if(_freq < 1.0)
					_freq = 1.0;
				break;
			case VK_NUMPAD6:
				_freq++;
				break;
			case VK_NUMPAD8:
				_amp++;
				if(_amp > 200)
					_amp = 200;
				break;
			case VK_NUMPAD2:
				_amp--;
				if(_amp < 0)
					_amp = 0;
				break;
			case VK_SUBTRACT:
				_sTime++;
				if(_sTime > 50)
					_sTime = 50;
				break;
			case VK_ADD:
				_sTime--;
				if(_sTime < 10)
					_sTime = 10;
				break;
			case 0x48:
				MessageBoxA(NULL, "NUMPAD + : Increase speed\n"
					 "NUMPAD - : Decrease speed\n"
					 "NUMPAD 8 : Increase amplitude\n"
					 "NUMPAD 2 : Decrease amplitude\n"
					 "NUMPAD 4 : Increase frequency\n"
					 "NUMPAD 6 : Decrease frequency\n"
					 "\nChanges are small, hold the button",
					 "Help", MB_OK);
				break;
			}
			return 0;
		} break;
     }
     return DefWindowProc(hWnd, message, wParam, lParam);
 }

void calcwave()
{
	static const double _full = 2 * pi;
	static double _x = 0;

	_yc._one._y = (sin(_x)*_amp) + 300;

	_x += 1.0 / _freq;
	if (_x >= _full)
		_x -= _full;

	_amplitude = "Amplitude : " + convstr(_amp * 2) + " pixels";
	_frequency = "Frequency : " + convstr(_full * _freq) + " pixels";
	_speed = "Speed : " + convstr(_sTime);
	
	drawwave();
	
	TextOutA(hDC, 0, 0, _amplitude.c_str(), _amplitude.size());
	TextOutA(hDC, 0, 17, _frequency.c_str(), _frequency.size());
	TextOutA(hDC, 0, 34, _speed.c_str(), _speed.size());

	_yc._two._y = _yc._one._y;
}

void drawwave()
{
	MoveToEx(hDC, _yc._two._x, _yc._two._y, NULL);
	LineTo(hDC, _yc._one._x, _yc._one._y);
	ScrollWindow(hWnd, -1, 0, NULL, NULL);
	Sleep(_sTime);
}

std::string convstr(const int& t)
{
	std::stringstream itoa;
	itoa << t;
	return itoa.str();
}
hint:
Read up on the ScrollWindow function - (you already use it in your code) - especially read about those last two parameters that you are currently setting to NULL .
closed account (zwA4jE8b)
thank you sir.

I am having a little trouble still,

I defined this rectangle area and added it to scroll window. The text no longer gets distorted because it is out of the rectangular area but now the drawn sine wave is scrolled as a solid chunk. Could you please give me an example of what I need to do here? I do not quite understand what I need for the lpClipRect parameter. The lpRect parameter seems pretty straightforward and I believe my current Rect structure is correct for that.

1
2
3
const RECT rect = {0, 50, 600, 800};
.......
ScrollWindow(hWnd, -1, 0, &rect, NULL);
Last edited on
Oh, I see what you mean - that is interesting!
I'm going to investigate..
closed account (zwA4jE8b)
thank you for your time and effort, let me know what you discover. I will also try to figure it out in the meantime
OK - i'm a bit stuck getting the clip to work.
So I've taken the sensible way out.
I've gone back to scrolling the whole window,BUT move the display of the text from where it
was and into a seperate function.

So how does the new code below perform on your system??

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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//Michael Ervin - Windows Sine Wave

#include <Windows.h>
#include <math.h>
#include <string>
#include <sstream>

#define screen_width 800
#define screen_height 600
#define pi 3.14159

struct _points
{
    int _x;
    int _y;
};

struct _ycoords
{
    _points _one;
    _points _two;
} _yc;

COLORREF blue = RGB(0, 0, 255);
HDC hDC;
HWND hWnd;
std::string _amplitude, _frequency, _speed;

double _freq = 50;
int _sTime = 30, _amp = 100;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void calcwave();
void drawwave();
void drawtext();//<<< **************************************
std::string convstr(const int& t);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEXA wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));
    ZeroMemory(&_yc, sizeof(_yc));
    _yc._one._x = 600;
    _yc._two._x = 599;
    _yc._two._y = 300;

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = (HICON)LoadImageA(NULL, "C:\\Visual Studio 2010\\Icons\\Smiley.ico", IMAGE_ICON, 0, 0, LR_LOADFROMFILE);
    wc.lpszClassName = "SineWave";

    RegisterClassExA(&wc);

    hWnd = CreateWindowExA(NULL,
                          "SineWave", "SineWave",
                          WS_SYSMENU,
                          0, 45,
                          screen_width, screen_height,
                          NULL, NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);
    hDC = GetDC(hWnd);

    HPEN _Pen = CreatePen(PS_SOLID, 1, blue);
    SelectObject(hDC, _Pen);

    MSG msg;

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {DispatchMessage(&msg);}

        if(msg.message == WM_QUIT)
            break;

        calcwave();
    }
    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } break;
        case WM_KEYDOWN:
        {
            switch (wParam)
            {
            case VK_NUMPAD4:
                _freq--;
                if(_freq < 1.0)
                    _freq = 1.0;
                break;
            case VK_NUMPAD6:
                _freq++;
                break;
            case VK_NUMPAD8:
                _amp++;
                if(_amp > 200)
                    _amp = 200;
                break;
            case VK_NUMPAD2:
                _amp--;
                if(_amp < 0)
                    _amp = 0;
                break;
            case VK_SUBTRACT:
                _sTime++;
                if(_sTime > 50)
                    _sTime = 50;
                break;
            case VK_ADD:
                _sTime--;
                if(_sTime < 10)
                    _sTime = 10;
                break;
            case 0x48:
                MessageBoxA(NULL, "NUMPAD + : Increase speed\n"
                     "NUMPAD - : Decrease speed\n"
                     "NUMPAD 8 : Increase amplitude\n"
                     "NUMPAD 2 : Decrease amplitude\n"
                     "NUMPAD 4 : Increase frequency\n"
                     "NUMPAD 6 : Decrease frequency\n"
                     "\nChanges are small, hold the button",
                     "Help", MB_OK);
                break;
            }
            return 0;
        } break;
     }
     return DefWindowProc(hWnd, message, wParam, lParam);
 }



void calcwave()
{
    static const double _full = 2 * pi;
    static double _x = 0;

    _yc._one._y = (sin(_x)*_amp) + 300;

    _x += 1.0 / _freq;
    if (_x >= _full)
        _x -= _full;

    _amplitude = "Amplitude : " + convstr(_amp * 2) + " pixels";
    _frequency = "Frequency : " + convstr(_full * _freq) + " pixels";
    _speed = "Speed : " + convstr(_sTime);
    
    drawwave();
    
    
    //*******commented out this
    //TextOutA(hDC, 0, 0, _amplitude.c_str(), _amplitude.size());
    //TextOutA(hDC, 0, 17, _frequency.c_str(), _frequency.size());
    //TextOutA(hDC, 0, 34, _speed.c_str(), _speed.size());
    
    //We use a seperate function for drawing the text
    drawtext(); //call the drawtext function

    _yc._two._y = _yc._one._y;
}

void drawwave()
{
    const RECT rect = {0, 50, 600, 800};
    
    
    MoveToEx(hDC, _yc._two._x, _yc._two._y, NULL);
    LineTo(hDC, _yc._one._x, _yc._one._y);    
    
    ScrollWindow(hWnd, -1, 0, NULL,NULL );
    Sleep(_sTime);
}


//***********************************************
//DrawText function
//***********************************************
void drawtext()
{

    TextOutA(hDC, 0, 0, _amplitude.c_str(), _amplitude.size());
    TextOutA(hDC, 0, 17, _frequency.c_str(), _frequency.size());
    TextOutA(hDC, 0, 34, _speed.c_str(), _speed.size());
}

std::string convstr(const int& t)
{
    std::stringstream itoa;
    itoa << t;
    return itoa.str();
}
Last edited on
closed account (zwA4jE8b)
it works, but the same as before, if the text is not lined up at x-coordinate = 0, it also scrolls.
Increase 600 here:

1
2
3
const RECT rect = {0, 50, 600, 800};
//...
ScrollWindow(hWnd, -1, 0, &rect, NULL);

Last edited on
closed account (zwA4jE8b)
@m4ster r0shi
Pardon me, what do you mean exactly? your post looks identical to mine, so I am unclear as to what you are saying.
You should increase 600. Try making it 800.

In fact, it should be -> const RECT rect = {0, 50, screen_width, screen_height};

See why here -> http://msdn.microsoft.com/en-us/library/dd162897(v=vs.85).aspx

Last edited on
closed account (zwA4jE8b)
oh wow, that is embarrassing. I confused the x and y values of the bottom right corner.
Thank you for correcting me.
closed account (zwA4jE8b)
thank you, works perfectly now.

Just for the sake of conversation.... is there a better method to use than Sleep?

I am using Sleep to keep the wave from drawing too fast. I know of timers but have not used them. Would a timer be better?
Topic archived. No new replies allowed.