I have a form and couple of labels on it. I am creating a screensaver with the said form. So far, I have been able to have the text labels appear at different intervals.
Howveer, I want to take it notch further. I would like to have a text scroll in from the top or bottom in right or left direction. Can I do this directly with C#? How?
I would appreciate a helpful answer. Thanks.
You want your text to "fly" on the form? If yes, why just not changing the label position?
Havent seen something like it... But if you know what is Expression Blend, you can make your own element styles (color, form, size, ANIMATION). You can try to realize it there...
Create a timer with a low interval and just use:
//Move right:
label1.Location = new Point(label1.Location.X + 1, label1.Location.Y);
//Move left:
label1.Location = new Point(label1.Location.X - 1, label1.Location.Y);
Maybe something like this:
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Location.X <= Width)
label1.Location = new Point(label1.Location.X + 1, label1.Location.Y);
else
label1.Location = new Point(0, label1.Location.Y);
}
You and any one else wanting to do something like that can simple use the following framework:
WinForm Animation Library [.Net3.5+]
A simple library for animating controls/values in .Net WinForm (.Net
3.5 and later). Key frame (Path) based and fully customizable.
https://falahati.github.io/WinFormAnimation/
new Animator2D(
new Path2D(new Float2D(-100, -100), lbl_label.Location.ToFloat2D(), 500))
.Play(lbl_label, Animator2D.KnownProperties.Location);
This slides the lbl_label label from -100, -100 to the location it was in first place in 500 ms.
Related
and thanks for taking a peek at my conundrum.
I am trying to write a series of strings to an image as overlays, then later be able to come back and move, or delete one of them selectively using WPF framework...
I've been looking into the FindVisualChildren function, but for the moment cant make heads or tails of how to detect the proximity to the mouse (for selectivity), or actually detect one of my created strings (Perhaps I should be making them dynamic 'Label' elements...????)
Insomnia sucks, and my brains are turning to mush.
TIA for any advice!
Okay, sorry for the lack of sample code, been a long night... well two nights actually (See earlier comment about insomnia)
public void WriteTextToImage(Point position)
{
ImageSource bitmapSource = imgFrame.Source;
var vis = new DrawingVisual();
using (DrawingContext drawingContext = vis.RenderOpen())
{
// Set the pen color... Why is this called a brush if it's for
// writing? perhaps I should overload it and call it crayon?
SolidColorBrush brush = new SolidColorBrush((Color)cpColor.SelectedColor);
drawingContext.DrawImage(bitmapSource, new Rect(0, 0, imgFrame.Source.Width, imgFrame.Source.Height));
//Write some pretty words, (actually print some general stuff)
drawingContext.DrawText(new FormattedText(tbCurrentLabel.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface("Arial"), slFontSize.Value, brush),position);
}
//Slap this puppy to the screen
var image = new DrawingImage(vis.Drawing);
//Iterate the label text to the next digit or value
IterateLabel();
}
Basically what will happen is the user will click the screen in several places to make marks on the image for printing, but I want to include support to move those marks, and delete them as needed.
I hope this explains what I am trying to accomplish a little better.
Thanks again!
Excellent! thanks NineBerry, I figured it was going to be something like that. I was originally thinking a label, but the textblock worked perfectly.
Here's the updated code showing how I am getting it done now.
public void WriteTextToImage(Point position)
{
SolidColorBrush brush = new SolidColorBrush((Color)cpColor.SelectedColor);
//Get something to write on (not an old receipt...)
TextBlock textBlock = new TextBlock();
//Say something useful... well something atleast...
textBlock.Text = tbCurrentLabel.Text;
textBlock.FontSize = slFontSize.Value;
textBlock.Foreground = brush;
Canvas.SetLeft(textBlock, position.X);
Canvas.SetTop(textBlock, position.Y);
canvas.Children.Add(textBlock);
//Need to update the canvas, was not seeing children before doing this.
canvas.UpdateLayout();
//Iterate the label text
IterateLabel();
}`
i have made a cool progressbar with text in the middle of it. Here is my code:
private void timer2_Tick(object sender, EventArgs e)
{
progressBar1.Increment(+1);
int percent = progressBar1.Value;
progressBar1
.CreateGraphics()
.DrawString(
percent.ToString() + "%",
new Font("Arial", (float)8.25, FontStyle.Regular),
Brushes.Black,
new PointF(progressBar1.Width / 2 - 10,
progressBar1.Height / 2 - 7)
);
if (progressBar1.Value >= 99)
{
timer2.Stop();
this.Close();
}
}
For some reason, the text shows up then disappears and other weird stuff. Why is that, and how do i fix it?
Try moving the drawing code into the Paint event, you are basically modifying the control's visuals, so you need to handle this instead of the default painting behaviour.
Your drawing gets erased whenever the control is repainted.
Instead, you need to set a flag indicating whether to draw the text right now, then handle the Paint event, and, if the flag is true, draw the text.
In the timer tick handler, toggle the flag and call Invalidate().
Another approach would be to create a UserControll and use a label on top of the progress bar, and use each separately, this might prove easier to do, hardly any additional code will be needed for this.
I'm developing a C# WinForms application. I can't find a solution to this probably because I'm new.
I need to create a teleprompter like text that scrolls from the bottom and goes up in a loop.
is there any simple solution or a code snippet?
An example would be nice so that I can understand how it is being done.
You could also create a Label control with text, and simply decrease its Vertical position with 1 (in pixels) every 1/20th of a second or so.
The idea is you could use the timer control, handle it Tick event
myTimer.Tick += new EventHandler(TimerEventProcessor);
Set myTimer.Interval = 1000;// event will fire every sec
private static void TimerEventProcessor(Object myObject,EventArgs myEventArgs) {
/// your logic to add new text, and change text position to give scroll effect
}
In TimerEventProcessor, put you logic to change text position that is to change it y coordinate, add new text in the bottom, this way you can create the scroll effect
In the timer.tick event handler you could do
if(label.Location.Y < 20)
label.Location = new Point(label.Location.X, this.ClientSize.Height);
else
label.Location = new Point(label.Location.X, label.Location.Y - 1);
Hope this help
I'm making a winform .NET app. It must show a graphic in bars format. I'm using a picturebox because it's the only way I know how to do it (if someone knows a better way, please tell me).
I'm adding dynamically the lines (the bars of the graphic) with this code:
int currentX = this.lineAmmount * (lineWidth + lineMargin);
pictureBox.CreateGraphics().DrawLine(new Pen(color, lineWidth), //Pen
currentX, pictureBox.Height, //Starting (x, y)
currentX, pictureBox.Height - Convert.ToInt32(value * graphicsScale)); //Ending (x, y)
this.lineAmmount++;
That works just perfect.
What I want now is the pictureBox to have an horizontal scroll bar. So what I put the pictureBox into a panel with autoscroll = true. Now what I needed its to dynamically increase the pictureBox width. So I added this code after I add each line:
pictureBox.Width = Math.Max(this.lineAmmount * (lineWidth + lineMargin), 205);
(205 is the minimum width I want).
That code also works greate. The width is increased. With the first lines Math.Max always returns 205, after a couple of lines it starts returning the orher value. From that moment on ALL THE LINES DISAPPEAR!!!
Please help!!
Thanks in advance and sorry for my bad english,
Diego
I found out the Chart control. It does all that automatically.
Where is written that code, that you posted in first box? Is it in update method of the control?
Sure, chart will be more appropriate here
I just would like to know if it is possible to dock a windows form on top of the user screen? I have been trying to do this by manually setting the position of my form to the coordinates I want. But using this method, however, allows the user to change the position of the form just by dragging it. I want to make the form docked to the upper portion of the screen since this window form will server as a menu for the project I am making.
Thanks a lot. :)
I would consider using the Control.Dock property along with one of the DockStyle enumeration values.
You might need to play with the Layout too, so that you may layout your form's controls differently depending on the DockStyle selected.
You will need, in my point of view, to consider the Control.Location property so that you get to know which DockStyle value to dock your form with.
EDIT #1
Your Windows Form has a Dock property as it inherits from Control.
Let's consider the following :
Each time your form comes closer to your right-side of the screen, for example, or of the MDI container, you want to dock right, right ? (Little word play here... =P) So, you have to subscribe to the Control.LocationChanged event.
private void myForm_LocationChanged(object sender, EventArgs e) {
if (this.Location.X > 900) then
this.Dock = DockStyle.Right;
else if (this.Location.X < 150) then
this.Dock = DockStyle.Left;
else if (this.Location.Y > 600) then
this.Dock = DockStyle.Bottom;
else if (this.Location.Y < 150) then
this.Dock = DockStyle.Top;
else
this.Dock = DockStyle.None;
}
Indeed, instead of constant values, you should use the current desktop resolution and calculate a ratio from it where you want your docking to occur.
***Disclaimer:****This code is provided as-is and has not been tested. This algorithm is hopefully enough to guide you through the docking process as you need it. Further assistance may be brought upon request.* =)
It seems the Form.DesktopLocation property is the righter tool for the job as for your main window, meaning your MDI container, for instance. As for the other windows, I would go along with something that looks like the code sample provided.
Does this help?
EDIT #2
If you want to prevent Form's overlapping, perhaps the Control.BringToFront() method could do it before or after your call to the Control.Show() method, depending on what works best for you.
So after some tweaks I finally was able to get this code working.
this.DesktopLocation = new Point((Screen.PrimaryScreen.Bounds.Width / 2 - 420), 0);
I placed that line below the InitializeComponent() and it docks my form to the center of the screen with whatever resolution values.
By setting the FormBorderStyle of your form to None, you take the drag handle away from the user so they cannot move it via the mouse.
Then you just need to place it where you want.
If you really want to take away the users options you can also set the ShowInTaskbar property to false