Touch operations of touchpanel in Windows Phone 7

Source: Internet
Author: User

Windows Phone 7 has four ways of user interaction:

  1. Buttons
  2. Touch
  3. Gesture
  4. Peripherals (such as gravity sensing)

Generally, Windows Phone 7 supports only the back button (Return key) for controllable operations. The other two are uncontrolled.

In xNa, such code is automatically generated, which indicates the Event Response to the back button.

if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)                this.Exit();

This means that the game is exited when the back button is pressed. This is generally relatively simple.

I. Touch

The simplest method of touch is to touch the screen directly. Windows Phone 7 supports up to four touch points.

11 In xNa, you must first obtain the status of the touch device before obtaining the touch point information (such as the Touch ID and coordinate information ).

The method for obtaining touch information is controlled by touchpanel. The touchcollection set can be obtained by using the getstate () of the touchpanel (because touch at least 4 points is supported, the collection is obtained), and the structure of the touchlocation is stored in the collection, there are three members:

ID: indicates the touch code. Each touch has a unique ID.

Position: Touch position

State: Touch status

Three States are available:

① Pressed. That is, the current user's touch status is pressed (the finger is pressed on the screen ).

② Moved. That is, the current user's touch status is moving (the finger is pressed and moved on the screen)

③ Release. When your finger leaves the touch screen

The following example shows three states.

First, declare a global variable touchmessage of the string type.

string TouchMessage="";

Add a spritefont file to display text on the screen.

Add a global variable of the spritefont type to load the spritefont resource to the content pipeline for the program to use.

SpriteFont spritefont;

Load spritefont Resources

spritefont = Content.Load<SpriteFont>(@"Fonts\SpriteFont");

Then we need to add the code to capture the touchpanel status in update, which is implemented through the getstate () method of touchpanel.

Protected override void Update (gametime) {// allows the game to exit if (gamepad. getstate (playerindex. one ). buttons. back = buttonstate. pressed) This. exit (); // todo: add your update logic here touchmessage = "; touchcollection Tc = touchpanel. getstate (); // because getstate obtains a set of states, you need to use foreach to retrieve each of them. // Pass TC. count attribute to obtain the number of sets foreach (VAR location in TC) {touchmessage + = string. format ("ID: {0} \ n location: {1} \ n position: {2} \ n TC. count: {3} \ r \ n ", location. ID, location. state, location. position, TC. count);} base. update (gametime );}

Then spritefont is drawn in the draw method.

protected override void Draw(GameTime gameTime)        {            GraphicsDevice.Clear(Color.CornflowerBlue);            spriteBatch.Begin();            // TODO: Add your drawing code here            spriteBatch.DrawString(spritefont, TouchMessage, Vector2.Zero, Color.Black);            spriteBatch.End();            base.Draw(gameTime);        }

When the touch is reached, the first status is pressed, but it will soon become moved. When the touch is opened, the released status will appear, and no signal will be displayed, that is, the touchmessage content is empty, because we click on the simulator with the mouse, there is only one point, when you open the mouse, there will be no status, so the touchmessage content is empty.

Related Article

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.