C# Animation with timer flickers the background - c#

I'm trying to make an animation for my C# program. There's a space rocket that ascends 8 pixels vertically with a timer interval of 25ms. I've managed to make the animation but since the picturebox's background (I've used for the rocket) is set to transparent it flickers the form's background image everytime it moves. What can I do to prevent it?
The code I've used for timer tick :
pictureBox1.Top -= 8;
P.S: I've tried to change the picturebox with panel, slow downed the rocket and timer but nothing seemed to change.

Well i haven't tried it myself now.
There needs to be a render event which you can hook to and make manipulations to your UI which would render smoothly.
Control.Paint
Try something like these :
private void Form1_Load(object sender, System.EventArgs e)
{
pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);
}
private void pictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
pictureBox1.Top -= 8;
}
Again this isn't tested and i haven't developed any thing in winforms for ages.
But that's the direction you should go for to render things smoothly.
That double buffered thing mentioned above is also a factor in some cases.
but this is mainly the way to do it.

Related

How to determine the keyboard offset

I had this issue yesterday and it seems a lot of people have had similar issues in the past, so I figured I would pose my question & the solution I ended up coming up with. Microsoft has cleaner solutions to this in the 8.1 SDK, but the vast majority of WP app users are on 8.0 and below, so I imagine this will still be helpful.
When you open the virtual keyboard in a Windows Phone 7/8 Silverlight app, and the text box that caused the keyboard to open is on the lower half of the screen (that would be covered by the keyboard), it scrolls the entire page up. How can you determine how much it has scrolled, in case there was content at the top that you need displayed?
It's a little clunky, but you can get the amount the page was scrolled up by looking at the offset of the root frame.
Since this is animated into position, the question becomes "when". What I found that works is, when a text box's GotFocused event is fired, subscribe to the LayoutUpdated event, and when LayoutUpdated is fired, grab the offset from there. If you weren't already subscribed to that event, you can unsubscribe in the LostFocus event. That way as it moves, you'll get the change.
double lastOffset = 0;
private void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
LayoutUpdated += MyControl_LayoutUpdated;
}
private void MyControl_LayoutUpdated(object sender, EventArgs e)
{
// Grab the offset out of the root frame's RenderTransform object
PhoneApplicationFrame root = App.Current.RootVisual as PhoneApplicationFrame;
TransformGroup transform = root.RenderTransform as TransformGroup;
double offset = transform.Value.OffsetY;
if (offset != lastOffset)
{
// Do your logic here if the offset has changed
lastOffset = offset;
}
}
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
// Unsubcribe to updates and reset the offset to 0
LayoutUpdated -= MyControl_LayoutUpdated;
lastOffset = 0;
}
After you have this offset, you can alter your controls as needed. You can either shrink the height of a control by that amount, or if you have something small at the top, like a header, you can apply a TranslateTransform by the inverse of the offset to just move it downward.

Transferring GridView is not smooth on Windows Phone 8.1

I'm trying to move a GridView with the ManipulationDelta event, similar to dragging.
My ManipulationDelta event looks like this:
private void GridView_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
{
var tt = (TranslateTransform)(sender as GridView).RenderTransform;
tt.X += e.Delta.Translation.X;
UpdateLayout();
}
The problem with this approach is that it's not smooth at all. Is there any better way to do this?
Remove the update layout call.
Changing the render transform is fast since it applies purely on the render thread and doesn't have to synchronize with the code thread or do a layout pass. UpdateLayout force everything to re-layout which is slow and unnecessary.

How is resize handled by DWM? C#

I have tried to create a custom border less form for my application. I decided that it had to be resizable, so I put 8 panels on its sides (4 corners, 4 sides actually) and created code which will resize my form when called. Like, for example:
private void E_MouseDown(object sender, MouseEventArgs e)
{
Active = true;
}
private void East_MouseMove(object sender, MouseEventArgs e)
{
if (Active)
{
this.Size = new Size(this.Width + e.Location.X, this.Height);
this.Refresh();
}
}
private void E_MouseUp(object sender, MouseEventArgs e)
{
Active = false;
}
Unfortunately, the resizing is slow, overloads the CPU and performs visual glitches even with double buffering for the form turned on. I have quite a bit of controls on my form. But what I have noticed is the fact that when I switch to a standard border (like Sizable), and DWM handles the window chrome, resizing is perfect. No glitches, stuttering and flicker. So I came to wonder, how does Windows manage to do? Is there a way to simulate what it is doing via the 8 panels? I hate when it flickers and it halts my project for no reason. The fact is that I worked all day long to mimic the Visual Studio 2013 window chrome and nearly suceeded, there is just this annoying problem... Can you help, please?
Thanks.

Gif image looping only once

I was trying to figure out how to make sure that a GIF image what I put to my WindowsForm project through picturebox going to loop only once and still without finding an answer even after going through as many sites with this problem as I could find.
I know that with using of picturebox programmer lossing control of looping (in case that you set looping only once when creating the gif animation in editor) and I somehow have to get a code which will basically say when gif animation gets to some certain frame...in my case to last one which is number 7 and gets there in 430 milliseconds (190,40,40,40,40,40,40) the gif animation going to be Disposed.
So the whole idea was to make a simple game with balloons which will have an animation with flying and animation with exploding after pressing of the mouse on the ballon_grey picture box.
It would look like that:
private void timer1_Tick(object sender, EventArgs e)
{
//makes balloon going up
balloon_grey.Location = new Point(balloon_grey.Location.X, balloon_grey.Location.Y - 3);
}
private void balloon_grey_Click(object sender, EventArgs e)
{
//forces balloon_grey picturebox to change animation to balloon_grey_explosion.gif
balloon_grey.Image = Image.FromFile("balloon_grey_explosion.gif");
//gets the amount of frames from the gif animation...don't know if this is needed to reach the goal
int frames = Properties.Resources.balloon_grey_explosion.GetFrameCount(FrameDimension.Time);
}
Thank you for your time and any advice about the problem.

On Idle Hide Mouse System-Wide, Show on Movement, from a C# Application

I'm trying to develop an application that, when running, will hide the mouse cursor wherever it happens to be on the screen after 5 seconds of inactivity, even if my app isn't in the foreground. Then, once the mouse moves again, the mouse should reappear instantly.
I've been reading a lot about low-level mouse hooks, but can't seem to wrap my head around the delegates and callbacks that, to me, make the code a lot harder to read.
Here are some of the things I've been looking at in terms of solutions:
Hide mouse cursor after an idle time
http://blogs.msdn.com/b/toub/archive/2006/05/03/589468.aspx
http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C
And I seemed to get closest with this one:
http://weblogs.asp.net/jdanforth/archive/2011/03/19/detecting-idle-time-with-global-mouse-and-keyboard-hooks-in-wpf.aspx
My problem seems to be that if I set up a timer on my main form for 5 seconds, then yes the mouse disappears after those 5 seconds if it's inactive, but then it also takes 5 seconds for the mouse to reappear! Would the solution simply be to have two separate timers and separate events?
Thanks so much for the help!
Add a handler for the MouseMove event on your form then call Cursor.Show() in the MouseMove handler.
You dont have to create 2 different timers. I would use the same timer, but have a private member variable that keeps track of whether the cursor is hidden. Once you hide it, you can reset the timer to be a lot shorter (say 500 ms) and continue to track it to turn it back on.
You can try this:
private Timer t;
void OnLoad(object sender, EventArgs e)
{
t = new Timer();
t.Interval = 5000;
t.Tick += new EventHandler(t_Tick);
}
private bool _hidden = false;
void t_Tick(object sender, EventArgs e)
{
if(!_hidden)
{
Cursor.Hide();
t.Interval = 500;
}
else
{
if(--some parameter---)
Cursor.Show();
}
}

Categories