I wanted a label like control where the text would scroll across the control. I figured surely there was a control like this out there, but all I could find were User Controls that contained an inner label control that had its position updated every few ticks. So after an hour of looking I decided to just write my own, which is the topic of this article. First, lets go over the requirements of the control. In essence, I wanted to create a label control that scrolls the text across the width of the control. I want the text to be able to scroll from left to right once the text scrolls of the control on the right side, it starts again from the left , from right to left, or to bounce back and forth between the two sides.
I also want the text to be able to be vertically aligned to the top, middle or bottom of the control. The user of the control should be able to programmatically control the speed of the text scrolling as well. I also like the look of some of the custom brushes that.
The last requirement, and most important, is I want the scrolling text to act like a hyperlink. When the user moves the mouse over the moving text, the cursor should change to a hand, and if the user clicks on the text, then an event needs to be fired. First step is to decide which class you need to derive inherit your new control from.
You have 4 basic choices for this. First you could use the UserControl class. The UserControl derives from the ScrollableControl class, which in turn derives from the Control class. If you create UserControl, Visual Studio will give you a designer so that you can drag and drop other controls onto it. So if you need to make something that is a combination of several controls this is the way to go. Next, you could inherit from an existing control, such as the Label control in this case, and override some of its methods to change its behavior.
The major difference is that UserControl gives you a design time designer to drag and drop other controls into it. Since I want to handle all painting myself, I created a new class called ScrollingText and derived it from the base Control class. Whenever I decide to create a new control, my solution always has two projects in it. The first project is a ClassLibrary project that contains my control.
The second one is a WinForms project that I use to test my custom control.
C# Application Programming
Once you have both of these projects created, open the designer for Form1 of your WinForms project. As shown below. Name and Control. The next thing to think about is notifying the control to paint itself. Timer class. This delegate will get called every time the timer interval expires.
- Inchoate Thoughts – Custom Drawing Controls in C# – Manual Double Buffering.
- Applications of Supercritical Fluids in Industrial Analysis;
- 24 packages returned for Tags:"GDI+";
- Economic Development of Latin America: Historical Background and Contemporary Problems?
- Un site utilisant unblog.fr?
This is shown below. Next we need to create a Tick method that the timer delegate will call. This method is pretty simple. All it does is call the Control. Invalidate method. There is a problem with this though. To handle this, you can also call the Control. Update method. One other thing to remember about calling the base.
One note, you might be tempted to use either the System. Timer class or System. These timer classes are specifically designed for use in a multithreaded environment, and both use threads from the ThreadPool to run their respective delegates. Invoke method to make the call thread safe, and its more of a pain in the butt than its worth and not especially fast. Especially since the System. Timer class is specifically designed to update the UI of a Form. But I will go over some of the basics and specifically what is needed for this control.
This article was filed under:
This is a great development feature, since all you just compile your code and you get instant visual feedback. This feature of control design caused me great trouble a few months back. I was creating a custom control, which did all its own painting, and I found that after working in Visual Studio for 5 minutes, my OS would just slow down and eventually freeze up.
I hade to use notepad in order to figure out where I was missing a Dispose and correct it. Then I paint the background of the control. If the user has set a custom brush via the BackgroundBrush property, I use that to paint the background control. The Graphics class has a FillRectangle method that I use to do this. Otherwise, the code as it is, just writes to the panel, which I could already do I thought the base.
OnPaint e is called only if you are overriding a paint event method, but in the custom control context, I'm in a private event method, so I don't think there is any "base" to call, my code crashes if I put Base. OnPaint e. Maybe I'm misunderstanding something, should I be overriding the control paint method with a paint event for the panel, and in that panel paint event call the base. OnPaint e?
Suddenly, you've confused me You have a custom control. Within that custom control is a panel. Yes, I would like to do that, that is correct. Okay, good! I understand the problem again!
OnPaint e ; You're crashing? Oh shit. Are you extending "Control" or "UserControl"? Without the line, it doesn't crash. You can see the line in the code snippet. I don't think I'm extending it, I basically created a control and dragged and dropped a panel into it in design mode. Here is the full code for the full user custom control: using System; using System.
OK I tried that just now, when I run the program, I get a blank form nothing drawn on it. Here's my current code below Now, the custom control has a paint method The custom control to your way of thinking has some "sub controls" on it. You want to call the paint method of the panel, yes?
First things, first OnPaint e ; then GridCus tomControl. OK, so far so good. The snippet of code has what I have so far. It compiles and runs. But the square is drawn on the Form, not in the panel.
What should we do next? Now, we've got a paint loop working for the panel Learn More! I'm not sure what this does, but this line was added to my InitializeComponent method of the custom control: this. Paint EventHandl er this. This is in the portion of the code you can't change, yes?
The Basics of GDI+
No game programming CreateGraphics ; Console. Dispose ; dc. Panel ; this. Point 0, 0 ; this.