Kingdz changed to cainiao, one C # Small instance per day-controlling beautiful pictures

Source: Internet
Author: User

Remember my blog domain www.cnblogs.com/hihell

Why is this? I want to call the hi + Hello result. I accidentally typed it into hihell without hitting O.

Well, from today on, I write a few small instances every day. Of course, they are all about C #. Sometimes I may write ASP. net, but it's time. Of course, I am.ArticleMany of them may be very simple, so the old bird flew over.

 

Special statement: this series of articles is intended for: Understanding C # BASIC language, or having learned C # For more than half a year.

Haha, I want to change kingdz back to that green dish again.

To put it bluntly, in my article below, whether you are a master or a dish. I will learn some valuable things. If you don't want to write anything about him, you can entertain yourself. Haha.

Well, let's just talk a little bit about it. Let's start our first example today. We use a keyboard to control beautiful pictures and drive them in disorder. Haha, I wrote it in JS, and I wrote it in HTML5, I have also used C # To write, others, and others. I have used Silverlight to write ....

Let's get started. Step 1: Prepare the image.

Then we start to load our Avatar ----- I am using the dynamic loading method, because one advantage is that it is convenient for you to modify it later ----- Of course we can adopt some design patterns.

StringImgpath =String. Empty; imgpath =Directory. Getcurrentdirectory (). substring (0,Directory. Getcurrentdirectory (). Length-10) +@ "/Image/1.jpg"; Pic. Load (imgpath); pic. sizemode = pictureboxsizemode. Zoom;

Here we will talk about a little bit of technical content. --------------- get the winform path. Haha, it's actually not easy, that isDirectory

It is a class used to obtain the software running directory. In the future, we may encounter other applications in our instances. The specific usage also requires your understanding.

Now, what we need to do is move the image. What do we need to move the image? First: direction. Of course, we adopt four directions this time. Second, speed.

Now, let's get started. Let's add a move class first, which is ourProgramNow, let's get familiar with the coordinates of the form.

First, find the keydown event of the form. There is no doubt that it is a keyboard-based event. Of course, if you can use the delegate to write the event on your own, you can leave it alone.

MessageBox. Show (E. keycode. tostring () +":"+ E. keyValue );

Haha, okay. Let's first find the values corresponding to the keys we need.

OK, see it. It's easy to test the button we need. We can also use the one before the colon and the other to check your own.

Then proceed. The following figure shows the length, width, and coordinates of the image.

Add

 
Lblwidth. TEXT = pic. width. tostring (); lblheight. TEXT = pic. height. tostring (); lblx. TEXT = pic. left. tostring (); lbly. TEXT = pic. top. tostring ();

Don't think it's meaningless. The above is very simple, but do you know that sometimes, the most useful is the simple small tests.

What we do below is nothing more than that, when you press the keyboard and then the X coordinate of the image minus the speed and the length of the image itself, a simple example is, the moving speed of a fake image is 5, the image length is 10, and the X coordinate of the download is 100. Well, by pressing "Rotate", the X coordinate is 95, the image length is a judgment to prevent the image from moving out of the form. OK. The left and right movements are implemented below.

Improve the move class

EnumDirection {left, right, up, down}

Declare an enumeration to record the direction in which it is pressed.

 Class  Move { Int Speed = 5; Public int Getmove (direction ){ Int Movenum = 0; Switch (Direction ){ Case Direction. Right: Case Direction. Up: movenum = + speed; Break ;Case Direction. left: Case Direction. Down: movenum =-speed; Break ; Default : Movenum = 0; Break ;} Return Movenum ;}}

This is a good understanding of the speed of moving. Then we can return the corresponding value. Note that we press forward → to accelerate, and the corresponding one is to slow down. Well, I can't really understand that either. Let's get started with it,

Now, the implementation of left-right movement is complete.

 Private void Form1_keydown ( Object Sender, Keyeventargs E ){ Move M = New  Move (); Switch (E. keycode. tostring ()){ Case  "Left" : PIC. Left + = pic. Left> 0? M. getmove (direction. Left): 0; Break ; Case  "Right" : PIC. Left + = pic. Left < This . Size. Width-pic. width? M. getmove (direction. Right ): This . Size. Width-pic. width; Break ; Default : Break ;} Lblwidth. TEXT = pic. width. tostring (); lblheight. TEXT = pic. height. tostring (); lblx. TEXT = pic. left. tostring (); lbly. TEXT = pic. top. tostring ();}

It should be noted that the form coordinates and the coordinates of our images are used to prevent the image from being moved to a part other than the form. size. width indicates the width of the form. Others are some simple logic.

Move up and down.

It's time to move a simple form. Of course, you can move the form by yourself.

This is our time_tick event. Haha, the principle of the snake is so implemented. You can try it on your own. A simple example may contain many basic things.

This article is over now. Let's take a look at what we can get,

Experts fly over. I would like to dedicate this series of articles to the beginner cainiao.

Code download http://files.cnblogs.com/hihell/MovePic.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.