I wrote a tutorial years ago about creating a Windows screen saver using the C# language. I usually let my GUI students do it as an extra credit assignment.
The tutorial was really showing signs of age, and last week I was finally able to update it. You can find it here. If you'd prefer to just jump straight to the finished product, you can download the project here. I used Visual Studio 2010 to write the demo code.
One more thing to cross off my summer to-do list.
Hello, recently in learning C#, saw your article, and made a screen saver, the basic functions have been achieved. However, I found that the screen saver in the process of operation, the text will "jitter", just like a position will stay in one place for a moment, suddenly moved to another location, I looked up all the code, and did not see the place that caused the change in this location, I do not know if you know what the reason is?
ReplyDeleteThank you
Without looking at your code, I have no idea why that would happen.
DeleteMy code is the same as yours. I ran your compiled program and found that the result is the same as that of my code. I tried it in Windows 7 32 & 64 bits. It feels like instability. The text appears in one location and jumps to another instantly. I tried to monitor it in the middle of Time_Ticks (possibly) and found that the position adjustment was only done once, but I saw it twice in actual operation.
DeleteThe text is supposed to move from one location to the next after a brief pause. You can change the moveTimer properties if you want it to stay in one location longer.
DeleteI just downloaded the zip file, ran it in Visual Studio 2017 on Win10, and it worked fine.
I think I've found the reason for the problem. It's more obvious on slower computers. Maybe it won't happen on higher-performance computers.
DeleteThe problem is in:
//Move text to new location
textLabel.Left = rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
textLabel.Top = rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));
When changing the coordinates, it really does two movements.
The solution is very simple. Calculate the coordinates of the movement well in advance and make one move. I use the following code to solve the problem:
int newX = rand.Next(Math.Max(1, Bounds.Width - textLabel.Width));
int newY = rand.Next(Math.Max(1, Bounds.Height - textLabel.Height));
textLabel.SetBounds(newX, newY,textLabel.Width, textLabel.Height);
Thank you and a nice day!
Interesting... I'm glad you figured out a solution.
DeleteHello, recently in learning C#, saw your article, and made a screen saver, the basic functions have been achieved. However, I found that the screen saver in the process of operation, the text will "jitter", just like a position will stay in one place for a moment, suddenly moved to another location, I looked up all the code, and did not see the place that caused the change in this location, I do not know if you know what the reason is?
ReplyDeleteThank you