Thank you for your help.
With C#, you can create a simple animation by drawing the owner of the ListBox you placed ListBox.
I'm trying to get him to do it.I want you to animate only in the cell you are currently selecting.
I'm doing it, but ListBox flickers every time I refresh it with Timer.
Of course, it's natural, but after that, try setting DoubleBuffered to True
I tried to override the OnPaintBackground empty.
I can't help but flicker.
Is there a way to make the animation work without flickering?
Below is the code.
public class AnimeList:ListBox
{
private Timer timer = new Timer();
private intiXpos = 0;
public TunesList()
{
This.ScrollAlwaysVisible=true;
this.Font = new Font ("Mario", 8, FontStyle.Regular);
This.ItemHeight=20;
this.DrawMode=DrawMode.OwnerDrawFixed;
This.DrawItem+=newDrawItemEventHandler(this.listBox1_DrawItem);
This.DoubleBuffered=true;
This.timer.Interval=100;
This.timer.Tick+=new System.EventHandler(this.timer1_Tick);
This.timer.Enabled = true;
}
/// <summary>
/// cell drawing
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void listBox1_DrawItem(object sender,DrawItemEventArgse)
{
if(this.DesignMode)
{
return;
}
// e.Index becomes -1 when ListBox is selected when ListBox is empty
if(e.Index<0)
{
return;
}
// Get string to draw
strings=((ListBox) sender).Items[e.Index].ToString();
e.DrawBackground();
Brushb = null;
// Unselected Cells
if(e.State&DrawItemState.Selected)!=DrawItemState.Selected)
{
e.Graphics.FillRectangle (Brushes.Black, e.Bounds);
b=new SolidBrush (Color.White);
// Drawing a String
e.Graphics.DrawString(s, this.Font, b, e.Bounds.X+4, e.Bounds.Y);
}
// cell being selected
else
{
e.Graphics.FillRectangle (Brushes.White, e.Bounds);
b=new SolidBrush (Color.Black);
// Drawing a String
e.Graphics.DrawString(s, this.Font, b, e.Bounds.X+4 + this.iXpos, e.Bounds.Y);
}
// Clean up afterwards
b.Dispose();
// Draw a square showing focus
e.DrawFocusRectangle();
}
private void timer1_Tick(object sender, EventArgse)
{
This.iXpos -=2;
if (this.iXpos<-128)
{
This.iXpos = 0;
}
This.Refresh();
}
protected override void OnPaintBackground (PaintEventArgs event)
{
// do nothing
}
}
Thank you for your cooperation.
c# winforms
For the time being, to reduce flickering in accordance with the original policy, keep down the update range and number of drawings.
Use OnPaintBackground
for background drawing.
OnPaintBackground
overridee.Graphics.FillRectangle (Brushes.Black, e.Bounds);
Specify the update range.
Use Invalidate(Rectangle)
instead of this.Refresh()
to specify the update range.
private int[]_previousSelection={};
private void timer1_Tick(object sender, EventArgse)
{
This.iXpos -=2;
if (this.iXpos<-128)
{
This.iXpos = 0;
}
var selection = SelectedIndices.Cast<int>().ToArray();
// Redraw the selected row and the previously selected line
foreach(vari in selection.Union(_previousSelection))
{
Invalidate (new Rectangle(0, i*ItemHeight, Width, ItemHeight);
}
_previousSelection=selection;
}
However, the above correction is to "limit the flickering range." If you plan to draw it in Graphics
of ListBox
, you will get some flickering.
In order to solve the problem fundamentally, it would be better to abandon ListBox
and perform WPF interoperation, or combine Windows form controls to create a similar design and change Location
to create a similar animation.
© 2024 OneMinuteCode. All rights reserved.