Click here to Skip to main content
Click here to Skip to main content

MoyStick, a Joystick Control

By , 2 Jun 2005
Rate this:
Please Sign up or sign in to vote.

Sample Image

Introduction

This control emulates an eight direction joystick. Unlike conventional joysticks, this control also provides a magnitude measurement depending on how far the end of the "stick" is from the center of the control.

Using the code

Attributes you may want to be aware of:

  • BackColor sets the color of the control's circular region.
  • CircleDiameter sets the diameter (in pixels) of the control.
  • LineColor sets the color of the control's "stick".
  • LineShapeEnd sets the shape of the end of the "stick".
  • LineWidth sets the width of the "stick" (in pixels).

Using the mouse stick with your app requires the use of the MouseStickMoved event. The eventargs provides two attributes, Magnitude and Direction.

  • Magnitude will return an integer value between 0 and 15, depending on the distance between the end of your "stick" and the center of the control.
  • Direction returns an enumeration of type cPoint, which is defined as follows:
    public enum cPoint
    {
        north,
        northEast,
        east,
        southEast,
        south,
        southWest,
        west,
        northWest
    }

For example, in my demo program, I set my circle's acceleration and direction in the form's userControl11_MouseStickMoved event handler:

private void userControl11_MouseStickMoved(object sender, 
                              MoyStick.MouseStickEventArgs e)
{
    stbMouseStick.Text = "direction: " + e.Direction.ToString() + 
                         " magnitude: " + e.Magnitude.ToString();
    switch(e.Direction)
    {
        case MoyStick.cPoint.north:
            dY = e.Magnitude * (-1);
            dX = 0;
            break;
        case MoyStick.cPoint.northEast:
            dY = e.Magnitude * (-1);
            dX = e.Magnitude;
            break;
        //(And so on......)

Points of Interest

I used a timer to invalidate to eliminate intense flickering that I observed when I tried to invalidate every time the MouseMove event occurred.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

yob25

United States United States
No Biography provided

Comments and Discussions

 
GeneralPocket PC PinmemberH.Riazi24-Nov-06 23:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 3 Jun 2005
Article Copyright 2005 by yob25
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid