Operating system experiment 1: clock interrupt program design

Posted by Kenny Pollock on Wed, 13 Oct 2021 02:22:47 +0200

1. Test requirements:

Draw a car in the specified window area of the screen (or load a car picture and remove the background), and let the car move at a uniform speed from the upper left corner to the lower right corner of the window.

Specific requirements are as follows:

(1) The trolley must be from the upper left corner to the lower right corner;

(2) The movement is basically uniform, and its speed can be adjusted through the button, and the speed setting is at least 2 kinds;

(3) After clicking the close button, the program will be executed in the background, and the corresponding icon of the program will appear in the tray area of the taskbar;

(4) Right click the corresponding icon of the program in the tray area, pop up the menu, click "restore", the program will be switched to the foreground again, click "exit", the program will exit the memory and stop running;

(5) When the program runs in the background, the trolley is still moving. It's just invisible.

           Select the language to implement: C#

2. Result display:

 

 

In this place, we click to restore the operation interface of the trolley, and it will be displayed on the desktop again.

3. Code implementation

3.1. Core component: Timer component

 

This component can realize the movement of our trolley (by changing the position of the trolley and then refreshing the interface regularly)

timer binding event

The following is the code bound to timer (used to control the movement of trolley)

private void timer_rePaint(object sender, EventArgs e)
        {
            //Control the movement of the vehicle
            int move_x = panel1.Size.Width / 100;
            int move_y = panel1.Size.Height / 100;
            int car_x = pictureBox1.Location.X + move_x;
            int car_y = pictureBox1.Location.Y + move_y;
            //Judge whether the car exceeds the window area. If it exceeds the window area, return to the upper left corner again
            if (car_x > this.Width - pictureBox1.Size.Width || car_y > this.Height - pictureBox1.Size.Height)
            {
                car_x = 0;
                car_y = 0;
            }
            //Reset the position of the trolley
            pictureBox1.Location = new Point(car_x, car_y);

        }

3.2. Function realization of five buttons:

3.2.1. Start button:

Bind click event to this button:

Click the event code as follows:

 private void button_start_click(object sender, EventArgs e)
        {
            //Start the timer to start the movement of the trolley
            timer1.Start();
        }

3.2.2. Pause button:

 

Click the event code as follows:

private void button_stop_click(object sender, EventArgs e)
        {
            //Pause timer, pause the movement of the trolley
            timer1.Stop();
        }

  3.2.3. Acceleration button:

 

  Code of acceleration event:

private void button_speedUp_click(object sender, EventArgs e)
        {   
            //When the refresh interval is greater than 10ms
            if(timer1.Interval > 20)
            {
                timer1.Interval -= 10;
            }
            //When the refresh interval is greater than 5ms and less than 10ms
            else if(timer1.Interval > 5)
            {
                timer1.Interval -= 1;
            }
            else
            {
                timer1.Interval = 5;
            }
        }

  3.2.4. Deceleration button:

Deceleration event codes are as follows:

private void button_decelerate_click(object sender, EventArgs e)
        {
            //When the refresh interval is less than 10ms
            if (timer1.Interval < 300)
            {
                timer1.Interval += 50;
            }
            else
            {
                timer1.Interval = 350;
            }
        }

   3.2.4. Terminate program button:

 

private void button_terminate_click(object sender, EventArgs e)
        {
            //Exit program
            System.Environment.Exit(0);
        }

3.3 trolley picture and related configuration

3.4.notifyIcon component is used to realize the program. It will be displayed in the lower right corner after clicking "X"

 

  The component is bound with a double-click event, that is, double-click the program to redisplay the relevant interface

private void notifyIcon_mouseDoubleClick(object sender, MouseEventArgs e)
        {
            //Display this Winform program
            this.Show();
        }

This is the property setting of notyfyIcon. We need to bind it to a ContextMenuStrip component. The following are the relevant settings of the ContextMenuStrip component

 

3.5. The above two added components belong to ToolStripMenuItem

  Related configuration of recovery

Bind a click event  

The event code is as follows:

private void toolStripMenuItem_restore(object sender, EventArgs e)
        {
            //Display this WinForm program
            this.Show();
        }

Exit related configuration:

  The event code is as follows:

private void toolStripMenuItem_exit(object sender, EventArgs e)
        {
            //Exit program
            System.Environment.Exit(0);
        }

3.6. Related configuration of overall Form

The FormClosing event is as follows (used to make the program run in the background instead of exiting the program after clicking the "X" key in the upper right corner):

private void form_formClosing(object sender, FormClosingEventArgs e)
        {
            //Set that clicking "X" will not exit the program
            e.Cancel = true;
            //Hide this WinForm program
            this.Hide();
        }

  The FormLoad event code is as follows:

private void form_load(object sender, EventArgs e)
        {
            pictureBox1.Location = new Point(0, 0);//Set the initial position to the top left corner
        }

The FormMouseClick event is as follows:

private void form_mouseClick(object sender, MouseEventArgs e)
        {
            //Display this WinForm program
            this.Show();
        }

4. The overall Form code is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CarTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer_rePaint(object sender, EventArgs e)
        {
            //Control the movement of the vehicle
            int move_x = panel1.Size.Width / 100;
            int move_y = panel1.Size.Height / 100;
            int car_x = pictureBox1.Location.X + move_x;
            int car_y = pictureBox1.Location.Y + move_y;
            //Judge whether the car exceeds the window area. If it exceeds the window area, return to the upper left corner again
            if (car_x > this.Width - pictureBox1.Size.Width || car_y > this.Height - pictureBox1.Size.Height)
            {
                car_x = 0;
                car_y = 0;
            }
            //Reset the position of the trolley
            pictureBox1.Location = new Point(car_x, car_y);

        }

        private void button_start_click(object sender, EventArgs e)
        {
            //Start the timer to start the movement of the trolley
            timer1.Start();
        }

        private void button_stop_click(object sender, EventArgs e)
        {
            //Pause timer, pause the movement of the trolley
            timer1.Stop();
        }

        private void button_speedUp_click(object sender, EventArgs e)
        {   
            //When the refresh interval is greater than 10ms
            if(timer1.Interval > 20)
            {
                timer1.Interval -= 10;
            }
            //When the refresh interval is greater than 5ms and less than 10ms
            else if(timer1.Interval > 5)
            {
                timer1.Interval -= 1;
            }
            else
            {
                timer1.Interval = 5;
            }
        }

        private void button_decelerate_click(object sender, EventArgs e)
        {
            //When the refresh interval is less than 10ms
            if (timer1.Interval < 300)
            {
                timer1.Interval += 50;
            }
            else
            {
                timer1.Interval = 350;
            }
        }

        private void button_terminate_click(object sender, EventArgs e)
        {
            //Exit program
            System.Environment.Exit(0);
        }

        private void notifyIcon_mouseDoubleClick(object sender, MouseEventArgs e)
        {
            //Display this Winform program
            this.Show();
        }

        private void form_mouseClick(object sender, MouseEventArgs e)
        {
            //Display this WinForm program
            this.Show();
        }

        private void form_load(object sender, EventArgs e)
        {
            pictureBox1.Location = new Point(0, 0);//Set the initial position to the top left corner
        }

        private void form_formClosing(object sender, FormClosingEventArgs e)
        {
            //Set that clicking "X" will not exit the program
            e.Cancel = true;
            //Hide this WinForm program
            this.Hide();
        }

        private void toolStripMenuItem_restore(object sender, EventArgs e)
        {
            //Display this WinForm program
            this.Show();
        }

        private void toolStripMenuItem_exit(object sender, EventArgs e)
        {
            //Exit program
            System.Environment.Exit(0);
        }
    }
}

5. All source codes:

Click to view the source code

Topics: C stm32