C + + migration from C + + to C #
Release Date: 1/24/2005 | Renew Date: 1/24/2005
John Kennedy
Microsoft Corporation
Download Road07102002-code.exe.
Content of this page
Card games
Graphics and SDE
Sound effects
As with many things in life, sometimes the only way to learn some knowledge is to try it yourself. There are, of course, some notable exceptions. I don't recommend doing surgery in this way (haha), but it's definitely a good way to learn about Smart Device Extensions (SDE) and to write programs that apply to Pocket PCs in C #.
Admittedly, we are getting more and more exposed to the Microsoft. NET Compact Framework, no doubt in this column or in the actual development segment. It is even more undeniable that C # is the most widely used language in the. NET Compact Framework. After I developed some programs in C #, I was more and more impressed by its ease of use, flexibility, and elegance. Of course, the current release of the beta version in some places there is a so-called "poor" situation, but this will soon be resolved. I firmly believe that once you continue to understand some new concepts, you will really enjoy using C # and the. NET Compact Framework.
Card games
We've discussed XML Web services in this column before, but some readers want me to be able to use smart Device extensions to create other, less connected programming projects. Not every software needs to be connected to the Internet, so some of the most powerful features in this new platform are often overlooked.
So in this month's column, we'll drill down to how to develop a more traditional application for the Pocket PC--a game that is written in C # and uses the. NET Compact Framework. There are some obvious differences between developing and using C # for development and eMbedded Visual C + +, and I would like to share with you some of the obvious questions that have been encountered in writing this program.
The game to be discussed is very simple. An older version of the card matching game-pelmanism. However, it's complicated to clarify some important differences and methods, so even if you think it's easy, try it. If you want to load and demo, you can download the source code.
The design of the game is very simple, as you can see in the screenshot below. (These photos are all members of my team, the Visual C + + team.) You might think that they would be more cautious now when I asked them for a photo. Of course, I prefer to use Drew Barrymore's photos, but there are strict procedures ... )。 There are 16 cards on the screen, and players "flip them" by clicking on the pointer. If the cards match, they will disappear from the screen. I'll leave it to the reader to practice-add code to count the number of flips, save results, and so on. Yes, it means I'm slacking off. But you know, I have been busy for a day! The first person asked me how much time I spent playing Halo on the Xbox with a very scornful look.
Figure 1. You know, I was forced by the Xbox team to discuss this picture's license. Of course, it's actually a lie.
Back to the top of the page
Graphics and SDE
C # programs are very different from the way eMbedded Visual C + + works, and it inherits the concept of forms from visual Basic. From a technical point of view, the change is not big, but I hope to be able to attract your attention. As you may know, a form is actually a page where you can add controls, display text, receive screen-clicking messages from, and so on. When we create a default project in Visual Studio. NET, the system automatically provides us with a default form.
Figure 2. A blank form, ready to let us show our imagination and talent.
You can see that the left side of the form is a set of controls that we can drag and drop onto the form, that is, drag and drop into our program. As opposed to using the eMbedded Visual C + + toolbox, you will find that the visual appearance of the controls in the editor is more closely related to the source code that is created. Try some controls and observe the code. Visual Studio keeps track of the way it impresses people.
A word of warning there are some places in the source code that do not make changes manually. For some reason I ignored this caveat, and when I was adjusting the form in the designer I lost the code, so keep in mind.
One control that can be added to a form is PictureBox, which is a control that displays an image. I intend to use a PictureBox for every card shown in the game. It differs from the way the program was originally written in C + +. If created in C + +, I would need to create a window (now what we call a form) and get HDC (show context handles) and then use BITBLT to turn a bunch of graphic data into actual graphics. This time, I'm going to use 16 PictureBox controls and use their methods to define what images they display. As you will see, this approach is quite appropriate.
Of course, knowing that we're going to display 16 cards, you might try to drag 16 PictureBox onto the form, and then resize them and arrange them neatly. This is a good idea, but programmatically dealing with each control becomes a trivial matter soon. A better approach would be to create a set of PictureBox and then use the index to process them, and you'll see, that's what I did. So, while the designer is useful, we don't necessarily use it to add PictureBox controls.
Let's simply look at some source code. C # programs are much like C + + programs in most places. such as functions, if () statements, do/while, and large numbers of parentheses. However, there are some subtle (not very significant) differences.
Give some basic examples, such as good and old #define statements. How can I explain it to you? It's out of date. Of course, it's not completely outdated, it's just a bit of a change. In a C # program, #define can only be used to create identifiers. You cannot use it to create constants, such as #define X_WIDTH 100.
With this in mind, we'll look at some array declarations. Do you need a two-dimensional array of integers? Do not think of int grid[4][4], but should be int[,] Grid = new int[4,4];.
Once you know this information, you should be able to understand what the code at the beginning of the game is useful for:
Figure 3. Creating controls programmatically is easy, and they show all the methods in the editor to prompt you what to do next.
This code declares some arrays: one for storing 9 different images used to play solitaire, one for storing the card type in 16 places where solitaire may exist, and one for storing the PictureBox control itself.
We then handle all of these PictureBox objects in the same way, create them, add them to the form, and resize and position them. Note how methods and properties that apply to each object, such as Location, are separated from the object by a period. The Visual Studio integrated Development Environment (IDE) is smart, and once you type a period, it pops up a list of all the correct actions you can perform on this object. This is just one aspect, which shows that C # makes it a pleasure to write code.
If you're the same as me, you'll like to add MessageBox calls to your program to keep abreast of what's going on. You can also have the ability to use Smart Device Extensions: Just use the following syntax:
MessageBox.Show ("Hello");
Of course there are more options for the complete list, see the online Help.
Consider images
Want to know how to load when we play solitaire to display some images? I also want to know, because the. NET Compact framework lacks some of the features of the. NET Framework, not even my preferred method. But it's still very simple, and if you look at it with a little attention when you read through the sample source code, you'll find that it has something to do with the Image object.
Here's another piece of code. This is a function that, by calling it, loads the image from the disk (actually memory, as long as you understand what I mean), and assigns the elements in the image array previously declared to it.
Figure 4. Loading images from memory is easy.
A. bmp file is a collection of images that I created in a doodle program and is scaled in advance to fit the size of the PictureBox control. I'm used to the. bmp format, but the control also supports other common file formats.
I believe you are already familiar with Try/catch exception handling, which is an optional way, but it is recommended that you develop the habit of using it. If the image fails to load, the code in catch () {} is executed. This obviously makes the code more robust and reliable, but it's even more useful if you can do more (rather than just warn the user that the program is about to terminate).
If you try to run this sample program with a small Pocket pc emulator that comes with Smart Device extensions, you may not know how to move a. bmp image file from a development computer to an emulated Pocket pc. Let me tell you, because this process has changed a little bit compared to the early Pocket PC development tools-you'll know I have a deep impression of it by guessing.
Copying files to an emulator
1.
Start the emulator (if it is not already started).
2.
From the Start menu, select Settings and then select the System tab.
3.
Click About, and then click the DeviceID tab.
4.
Change the device name from POCKET_PC to another name.
Figure 5. Change the device ID or an error occurs when the emulator communicates with the desktop computer.
5.
Now start the file browser program on the Pocket PC.
6.
Click on the network share icon. The icon is at the far right of the bottom of the screen.
7.
Enter the details you need to access your computer over the LAN.
8.
Browse to the shared folder on your desktop computer, and then select the files you want to copy.
9.
Click on the file on the Pocket PC and hold it down.
Note The emulator has a bug and you may need to hold down the mouse button for 30 seconds to see the red dots and subsequent pop-up menus.
10.
Copy the file and return to the Pocket PC's own directory and paste it.
Figure 6. Browse to a shared folder on your desktop computer, and then copy the file and paste it into your (emulated) Pocket PC.
That's all the steps.
Solemnly announce that this is the entire content of this small program. Of course, I also added some menus, using the form designer, how simple it would be. One thing I've been thinking about for a long time is how to handle user-directed pen clicks.
My theoretical analysis of the Visual C + + program is this: Occupy the entire screen and accept the mouse down the message, solve the X and Y coordinates, and then calculate the user clicks the card. In this case, each card is a control, and each control responds individually to the mouse-pressed message. I can't include the "click on the screen and get the message" code that has a global character.
However, it is easy to specify a handler for each control. This is the line in the first code block of the source code:
This.picturebox[x,y]. Click + + new System.EventHandler (this. Usertapsscreen);
As you can see, I added the same function to each control (Usertapsscreen). The obvious question is, "in this function, how do I know which control the message is emitted from?" "Here's the source code to do this:
private void Usertapsscreen (object sender, System.EventArgs e)
{
int CARDX =-1;
int cardy =-1;
Find the "card" send the message
for (int x=0; x<4; x + +)
for (int y=0; y<4; y++)
if (sender. Equals (Picturebox[x,y])
{
CARDX = x;
Cardy = y;
y=4;x=4;
}...
Note that this code uses an attribute of the sender parameter passed to the Usertapsscreen function, along with its Equals method.
Back to the top of the page
Sound effects
A game that has no sound effect is worthless. This is what we are going to introduce next month. At the same time, download the source code and try the demo. If you have any questions, please feel free to send me an email. However, if you are a company that specializes in spamming, and you intend to send me 12 emails a day to provide me with some clothing, electronic and pornographic information, that's another story!
John Kennedy was a technical writer/programmer for the Visual C + + team during the day, while at night he enjoyed the secret life of Pocket PC developers.
Larry Roof founded Larryroof.com, a company specializing in mobile project consulting and eMbedded Visual Basic, Smart Device Extensions, and SQL Server CE training.
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.