Click here to Skip to main content
15,880,905 members
Articles / Desktop Programming / Windows Forms
Article

MoyStick, a Joystick Control

Rate me:
Please Sign up or sign in to vote.
2.82/5 (4 votes)
2 Jun 20051 min read 35.9K   1.7K   18   1
An eight direction joystick control.

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:
    C#
    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:

C#
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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPocket PC Pin
HRiazi24-Nov-06 23:41
HRiazi24-Nov-06 23:41 

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

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