I have a question about loops...
I was attempting to make a program that took the caption of a window, and scrolled it back and forth, I got it to work using one technique (2 for loops, one for each direction), but my original idea wasn't working....I wanted to have a single while and for loop structure like so :
while(cnt<=60)
{
for(int x = 0;x < cnt%12;x++)
buf += " "; // don't worry about this, it works =)
buf += "Hello Bob";
SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)buf.c_str());
cnt++;
}
but this only goes one way (right) and jumps back to 0 and i tried it a different way and got it to work coming back, but it didn't work going forward, it just stayed put half the time, I don't have to have this way to work, but it would be nice to see how it would work since i couldn't figure it out.
To sum it up, the basic jist is that I want a formula to make a for loop oscilate from 0 to N in both directions (probably using mod) with only one statement.
Edited by - BraveZeus on 3/30/00 1:02:54 AM
Chris
OK... your code and description is a little cryptic for me, but if I understand what you''re trying to do, correct me if I''m wrong, is you want to move the text, "Hello Bob" from the left side of the window caption to the right, then back again. If this is so, then the solution could be something like this or there''s something probably a lot simpler, but oh well... this is a fairly basic problem, and I''m probably not understanding what you meant... oh well:
int inc = 1, int pos = 0, caption_width = 60;
while( true ) //REPLACE "true" WITH CONDITION
{
buffer = "";
pos += inc;
if( pos >= caption_width // pos <= 0 )
inc = -inc;
for( int i = 0; i < pos; i++ )
buffer += " ";
buffer += "Hello, Bob";
SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)buffer.c_str());
}
-Derek
int inc = 1, int pos = 0, caption_width = 60;
while( true ) //REPLACE "true" WITH CONDITION
{
buffer = "";
pos += inc;
if( pos >= caption_width // pos <= 0 )
inc = -inc;
for( int i = 0; i < pos; i++ )
buffer += " ";
buffer += "Hello, Bob";
SendMessage(hWnd,WM_SETTEXT,0,(LPARAM)buffer.c_str());
}
-Derek
That makes sense... But I think I know a more efficient way... correct me if I''m wrong!
I can''t remember the command for writing text to a window... something like PaintText(x,y,"Hello World", sizeof("Hello world")) I will use that format here just so I don''t have to go look up the actual function. You can do that
int xpos = 0,ypos = 0,inc = 1;
char Buffer[] = "Hello World";
while (true) // replace with condition
{
PaintText(xpos, ypos, Buffer, sizeof(Buffer));
xpos += inc;
if (xpos + sizeof(Buffer) * 6 >= SCREEN_WIDTH // xpos <= 0)
inc = -inc;
}
--------------------
You are not a real programmer until you end all your sentences with semicolons;
I can''t remember the command for writing text to a window... something like PaintText(x,y,"Hello World", sizeof("Hello world")) I will use that format here just so I don''t have to go look up the actual function. You can do that
int xpos = 0,ypos = 0,inc = 1;
char Buffer[] = "Hello World";
while (true) // replace with condition
{
PaintText(xpos, ypos, Buffer, sizeof(Buffer));
xpos += inc;
if (xpos + sizeof(Buffer) * 6 >= SCREEN_WIDTH // xpos <= 0)
inc = -inc;
}
--------------------
You are not a real programmer until you end all your sentences with semicolons;
--------------------
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Well, if you really want a one line fix...
change:
for(int x = 0;x < cnt%12;x++)
to:
for(int x = 0;x < (12 - abs(12 - (cnt % 24)); x++)
change:
for(int x = 0;x < cnt%12;x++)
to:
for(int x = 0;x < (12 - abs(12 - (cnt % 24)); x++)
you could also respond to the WM_NCPAINT message. that would give you more control.
Get off my lawn!
Why not use the sprintf() function with the variable width padding parameter?
Here''s a little program I put together..
See how there is an integer in sprintf (padding) which controls how much padding an empty string "" gets? I increment or decrement the padding so that more or less spaces get put infront of the "". I then put the actual string I want printed on the end.
Here''s a little program I put together..
#define MAX_PADDING 20char buffer[1024];void UpdateBuffer(char *message){ // initialize static variables; these are not reset each function entry static int padding = 0; static int incrementer = 1; // increment or decrement the padding padding += incrementer; // build the buffer sprintf(buffer,"% *s%s", padding, "", message); // switch the sign of the incrementer if // the padding has reached a maximum value if (padding == MAX_PADDING) incrementer = -1; if (padding == 0) incrementer = 1; return;}int main(int argc, char* argv[]){ int i; for (i = 0; i < 100; i++) { UpdateBuffer("hello"); printf("%s\n", buffer); } return 0;}
See how there is an integer in sprintf (padding) which controls how much padding an empty string "" gets? I increment or decrement the padding so that more or less spaces get put infront of the "". I then put the actual string I want printed on the end.
April 02, 2000 03:50 PM
Or you can get Linux and X-windows, the titles automatically scroll.
I think it''s a good idea, especially with programs that use looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong titles.
And everybody thought Gates had all the inovation . . .
I think it''s a good idea, especially with programs that use looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong titles.
And everybody thought Gates had all the inovation . . .
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement