Click here to Skip to main content
Email Password   helpLost your password?

Contents  

What is Mobile TouchPad?

Mobile TouchPad lets you control your PC through your touch pad phones. You can easily connect to your desktop without configuration, you only need the driver to be active (also source code included) and the standard ActiveSync enabled.  

You can use it in a presentation, or to control your media center.

Introduction

I try to explain how it is simple to write code for mobile. The project consists of two solutions, two programs are needed, because one intercepts the mouse movements on mobile and the second interacts with the desktop.

I've solved some problems about:

Info: To make the following code example easier to read, security checking and error handling are not included. 

Developer Requirement   

First of all, to deploy any WM application you need the Windows Mobile 6 Professional and Standard Software Development Kits Refresh[^].

Here you can find all the requirements for your system and you can choose the platform Pro or standard or both. You need at least one of the two. I suggest to you the 'Professional Windows Mobile 6 Professional SDK Refresh.msi', the new SDK for Pocket PC.

Please check each application's system requirements individually.

Anyway I add brief System Requirements:

Info: To deploy this project I use Visual Studio 2005 SE EU, Vista Business, Windows Mobile Device Center, and a Samsung i900. 

Additional Requirement 

For the mobile ink, I prefer to use a free Mobile Ink Library. OpenNETCF Mobile Ink is a component that provides support for WM 6 Ink here [^] and is released under the MIT X11 license [^].

I use this library, first because it's free, and Open source, and also because it's very simple to use, and I show you how do that.  

Using the Mobile Side Code 

The mobile solution is named MobileTouchPad and you can find it under the WM folder. The mobile app will be similar to the image below.

OpenNETCF Mobile Ink

As seen above, I use that because it can be cool to give some effect to the touch. At the moment, I write the touched line and I clear it after releasing the screen. It's very simple to use this component, The steps to use an ink in your project are:

Add Using and Reference

using OpenNETCF.WindowsMobile.Ink;
using OpenNETCF.WindowsMobile.Ink.Interfaces;

You can find it after installed the file downloaded OpenNETCF Mobile Ink (you can also find the source code in the installation dir).

Add a Panel

Add a panel or a picturebox to your form, it will be your touchPad, I've call it panelTouchPad.

Add the Overlay

Add an overlay to your panel:

IInkOverlay overlay;

Add the EventHandler for the Load

In the load form, attach the panel to the overlay:

overlay.hWnd = pictureBox1.Handle;
overlay.Enabled = true;

Add the Event Stroke

This enables the ink:

overlay = Ink.CreateInstance();
overlay.SetEventInterest(InkCollectorEventInterest.Stroke, true);

Adapt the Screen

In the Resize form, retrieve the screen coordinate and adapt the panelTouchPad to the screen:

Rectangle oScreen = Screen.PrimaryScreen.WorkingArea;
panelTouchPad.Location = new System.Drawing.Point(0, 0);
panelTouchPad.Size = new System.Drawing.Size
	(oScreen.Size.Width, oScreen.Size.Height - oScreen.Y);

Clear the Screen

overlay.Enabled = false;
overlay.Ink = InkDisp.CreateInstance();
overlay.Enabled = true;

Well the first step has ended, now we have a very simple Mobile paint.

Mouse Move

To intercept the mouse movements simply, I use the EventHandler of MouseMove of the panelTouchPad. Here I decided how to send data to the desktop. I save the last point every time and I send to the desktop only a step movement. The data can be (+1,0) (-1,1) (0,-1) etc.

Point pointDirection = new Point();
pointDirection.X = e.X - LastPoint.X  ;
pointDirection.Y = e.Y - LastPoint.Y  ;

SendData(pointDirection);

LastPoint.X = e.X;
LastPoint.Y = e.Y; 

Button Pressed and Double Click

To Intercept the button pressed, you need to add the KeyEventHandler for KeyDown and KeyPress and send it with no movements.

private void FormTouchPad_KeyDown(object sender, KeyEventArgs e)
{
    m_buttonState = CommonStruct.MouseButtonState.ButtonPressed;     
    SendData(new Point(0,0));
}

private void FormTouchPad_KeyUp(object sender, KeyEventArgs e)
{
    m_buttonState = CommonStruct.MouseButtonState.None;
    SendData(new Point(0, 0));
}

To Intercept the double click, add an event DoubleClick to the panelTouchPad and send it with no movements.

m_buttonState = CommonStruct.MouseButtonState.DblClick;
SendData(new Point(0,0));

As you can see here, I've used a common structure CommonStruct for both the solutions.

This is the enum for the button state:

public enum MouseButtonState
{
    None = 0,
    ButtonClick,
    ButtonPressed,
    DblClick
}

This is the struct for the button action:

public struct TouchPro
{
    public Point point;
    public MouseButtonState buttonState;
}

This common struct is in Common.cs.

Desktop IP

Info: To communicate with the desktop you need to sync the Device or the emulator. To do that, you need the ActiveSync 4.5 installed as System Requirements for Windows XP, and the Windows Mobile Device Center for Windows Vista.

After syncing it, you only need set up your connection as DMA, with this option you are able to retrieve the desktop IP dynamically.

To retrieve the desktop IP, I've used some registry key enabled by the ActiveSync. This key are stored in [HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts\ppp_peer] or if not exist [HKEY_LOCAL_MACHINE\Comm\Tcpip\Hosts\dtpt_peer]. If you have problems, I'll suggest to you this blog [^].

Using the Emulator

You can use the emulator to test the project, but obviously the mouse interaction can be a bit difficult.

Attention: If you use the emulator, you must activate the cradle.

In Visual Studio 2005, open Tools menu and choose Device emulator manager. In Device emulator manager, select your current emulator and select cradle.

After ActiveSync starts, you can may notice a small tray icon like networking icon, with a plus sign turning left and right.

Serialize/Deserialize

Using socket, I need to convert my class to byte[]. Normally on desktop, I use BinaryFormatter and MemoryStream , but on WM that reference is missing... Anyway I found another way using Marshaling. This is the CommonConvertion I use in my two projects (you can find it in Common.cs).

class CommonConvertion
{
    public static byte[] StructureToByteArray(object obj)
    {
        int Length = Marshal.SizeOf(obj);
        byte[] bytearray = new byte[Length];
        IntPtr ptr = Marshal.AllocHGlobal(Length);
        Marshal.StructureToPtr(obj, ptr, false);
        Marshal.Copy(ptr, bytearray, 0, Length);
        Marshal.FreeHGlobal(ptr);
        return bytearray;
    }
    public static void ByteArrayToStructure(byte[] bytearray, ref object obj)
    {
        int Length = Marshal.SizeOf(obj);
        IntPtr ptr = Marshal.AllocHGlobal(Length);
        Marshal.Copy(bytearray, 0, ptr, Length);
        obj = Marshal.PtrToStructure(ptr, obj.GetType());
        Marshal.FreeHGlobal(ptr);
    }
}

Connect and Send Data to Desktop

To send data to desktop, I prefer to use TCP/IP instead of Web service, Rapi and so on, because it's more simple (I think).

TCP Usage

Define a TcpClient and a NetworkStream used all the same during the app life:

private TcpClient m_client;
private NetworkStream m_stream;

In the constructor, add the Connection to the server and open the stream to it:

m_client = new TcpClient(server, port);
m_stream = m_client.GetStream();

If it fails (the desktop server is closed), the app tries to connect to the server every time you touch the screen and gives a message box to you... you can continue without the connection or you can go on drawing on it....

Finally we send data as you saw before in the MouseMove. Here in the code, you can see a new method SendData. This is a method where I manage the data and send it to the desktop.

private void SendData(Point pointDirection)
{
    ...
    CommonStruct.TouchPro AddMove = new CommonStruct.TouchPro();
    AddMove.point = pointDirection;
    AddMove.buttonState = m_buttonState;
    object objTmp = (object)AddMove;
    Byte[] data = CommonConvertion.StructureToByteArray(AddMove);
	
    m_networkstream.Write(data, 0, data.Length);
    ...
}

Using the Desktop Side Code

The solution is named DriverMobileTouchPad and you can find it under x86 folder. This is a screenshot to show you its interface. Here you must activate the server to wait for the mobile connection.

Mobile devices have several limitations, one above all is the small screen resolution. To avoid this problem, I've added the possibility to change the cursor velocity. 

Connect and Receive Data from Mobile 

TCP Usage

Into the form, I've created a server waiting connection on a port:

 m_tcpserver = new TcpListener(IPAddress.Any, 5000);
 m_tcpserver.Start();
 m_tcpserver.BeginAcceptTcpClient
	( new AsyncCallback(DoAcceptTcpClientCallback), m_tcpserver); 

Warning: Remember to open the port 5000 on your firewall.

When it's connected to a client, it begins to read stream.Read inside the DoAcceptTcpClientCallback from the opened stream and converts it to my common struct. After reading data, deserialize it with the CommonConvertion, as shown here Serialize/deserialize.  

CommonStruct.TouchPro AddMove = new CommonStruct.TouchPro();
object objTmp = new object();
objTmp = (object)AddMove;
CommonConvertion.ByteArrayToStructure(bytes, iBufflen, ref objTmp);
AddMove = (CommonStruct.TouchPro)objTmp;

Mouse Interaction

For the interaction, I've created a class MouseMov. Here I use the P/Invoke to send click (pressed and release) and double click.

 [DllImport("user32.dll")]
 private static extern void mouse_event
	(UInt32 dwFlags, UInt32 dx, UInt32 dy, UInt32 dwData, IntPtr dwExtraInfo);
 private const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
 private const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;
 internal static void SendUp()
 {
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, new System.IntPtr());
 }
 internal static void MoveMouse(Point p)
 {
    Cursor.Position = new Point(Cursor.Position.X + p.X, Cursor.Position.Y + p.Y);
 }

The MoveMouse is a simple cursor movement.

internal static void SendDown()
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, new System.IntPtr());
}

Well finally we have finished. Try to run the two solutions I've added and you can see the code in action. Let me know your problems and what you think about it. Have fun.  

Links  

I've found these links very useful:

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionProblem in deploying!!!!
gg05
12:15 6 Nov '09  
Hello Sir,
i m Govind from indore i want to use this technique in my project Bur the problem with its source code is that when we open wm side project it gives the error "error retrieving information from user datastore.Platform not found"
Is it due to the use of devie emulator 2003?
plz reply me soon.
thnx with regards
Govind Gupta
e-mail :- govind.gupta05@ymail.com
AnswerRe: Problem in deploying!!!!
Dr.Luiji
4:26 7 Nov '09  
Hi Govind,

to run this project you must have Microsoft Visual Studio 2005 or above and Windows Mobile 6 Professional and Standard Software Development Kits Refresh[^].
As you wrote if you have only the development for 2003 can be a problem. Before begin take care about the Developer Requirement of the article.
Let me know, I'll wait you replay.

Cheers

Dr.Luiji

Dr.Luiji

Trust and you'll be trusted.

My Blog : The Code Is Art

QuestionRe: Problem in deploying!!!!
gg05
2:33 8 Nov '09  
sir, thanks for your reply n good news is that the problem has been resolved
but the new problem arises that when i deployed it,i can only see that form not the desktop.still i cant watch my desktop from that program.it only shows x and y coordinate but not the desktop item.please reply me soon

with regards
Govind Gupta
AnswerRe: Problem in deploying!!!!
Dr.Luiji
7:14 8 Nov '09  
Hi again,
This project lets you to control your PC through your touch phone.
Your phone becomes a touchpad, as a touchpad it does not show the desktop.
At least the phone will show you the last movements.

Let me know
Cheers

Dr.Luiji

Trust and you'll be trusted.

My Blog : The Code Is Art

QuestionRe: Problem in deploying!!!! [modified]
gg05
5:34 9 Nov '09  
Will u plz help me in building such a system through which we can access our whole computer system via internet connectivity in a more simple and basic way.Actually i m very new to this technology & i m a studnt of BE(final year).Will you plz give me some of your valuable time pesonally, i want to discuss about my project which i selected as my major project.Plz tell me when you are online free(plz give the Indian Time).Thanx for ur response.

with regards,
Govind Gupta

modified on Tuesday, November 10, 2009 3:17 AM

AnswerRe: Problem in deploying!!!!
Dr.Luiji
6:04 9 Nov '09  
Currently I'm involved into too many projects and I can't finish one, I have 2 little kids and my free time is very poor. Anyhow I'm interested in your project and I can give you a hand.
I have already sent you an email, reply me soon.
Cheers

Dr.Luiji

Trust and you'll be trusted.

My Blog : The Code Is Art

General@ Good Job
PavanPareta
0:55 17 Aug '09  
Hi Dr.Luiji,

excellent work Thumbs Up

My Vote is 5

Pavan Pareta

GeneralRe: @ Good Job
Dr.Luiji
7:30 17 Aug '09  
You're welcome!
Thanks for the vote Big Grin !

Dr.Luiji

Trust and you'll be trusted.

My Blog : The Code Is Art

Questionplz help me about run listener app!?
barbod_blue
19:43 2 May '09  
hi dr. luigi , so tanx for cool sample...
but i have a different question , plz help me :
how can i write a sample on wm5+ that , this program
is a listener , for example , run this program after wm5+ load
and run in the hidden process or run at the taskbar , and , when
user have an incomming call, this program , automatic reject it and
send sms to the destination and ...
or when user have incomming sms, this program can check the body
of sms , and if contain some character , answer to it automatically...
please help me , really tanx Smile
AnswerRe: plz help me about run listener app!?
Dr.Luiji
2:31 4 May '09  
Hi barbod_blue,
Probably this isn't the best place to insert this question... next time, try here[^].
Anyway the app you describe is really simple to deploy.
Here on CP you have a lot of useful samples that can help you:
- Automatically Starting Your Application on Windows Mobile[^]
- Windows Mobile Call Silencer[^]
- 0wn your phone - Taking back control of your mobile phone[^]
- Sending SMS on Pocket PC[^]

Hope this help you solve your problems.
Have a nice day.

Dr.Luiji

Trust and you'll be trusted.

Try iPhone UI [^] a new fresh face for your Windows Mobile, here on Code Project.

Question2 way communication????
anusho
7:44 21 Apr '09  
hi Dr.Luiji,

My name is Anil am a student working on a project which basically deals with desktop ,mobile devices and communications.. First of all let me tell all that plz know the value this article before giving any comments.. Such an excellent stuff really appreciable..

My rating is definitely 5..

Sir, i have few questions to you,i have worked on this code.during that i could noticed that the communication was from device to desktop but was not vice versa(sorry if there is any neglectance)how can we achieve desktop to device communication and desktop to desktop communication? and i could see that it basically consists of mouse pointer interaction module which is amazing.. i would love to know whether can we extend the same for more functionalities like text and other picture messages? Sir if u please don't mind please give me few references where i can explore lots.. awaiting an early positive response..
Thanks a lot.

Cheers,
Anil Smile Thumbs Up
AnswerRe: 2 way communication????
Dr.Luiji
12:53 22 Apr '09  
Hi Anil,
Thanks for the kindly words.
anusho wrote:
i have worked on this code.during that i could noticed that the communication was from device to desktop but was not vice versa(sorry if there is any neglectance)how can we achieve desktop to device communication and desktop to desktop communication?

In this project I only need to send some data from the phone to PC, I didn't need the opposite. The conection is a classic TCP/IP with socket, the TPC connection are in both directions. You can invert or add a new the logic with no problem. It's really simple:
- To send data to device you only need use the stream.Write (like the SendData used for the device )
- To receive data you must use stream.Read (like the DoAcceptTcpClientCallback used for the desktop)
anusho wrote:
i would love to know whether can we extend the same for more functionalities like text and other picture messages?

You need need to change both sides the convert methods.
- Before the stream.Write you need to use the CommonConvertion.StructureToByteArray with your Image yourImage . Something like this:
Byte[] data = CommonConvertion.StructureToByteArray(yourImage);
- After the stream.Read you need to use the CommonConvertion.ByteArrayToStructure with your picture byets. Something like this:
Image myImage = new Image();
object objTmp = new object();
objTmp = (object)AddMove;
CommonConvertion.ByteArrayToStructure(bytes, iBufflen, ref objTmp);
myImage = (Image)objTmp;
I've not compiled the hereover code but I think can help you a lot. If not so, Ask me again I'll try to explain it in a better way.
anusho wrote:
Sir if u please don't mind please give me few references where i can explore lots..

You can explore the Help on MDSN for the socket usage, a good point with the examples can be Here[^]. This[^] can be another great point if you need a GPRS connecion. See my link added at the end of this article to close the circle.
Let me know.

Regards.

Dr.Luiji

Trust and you'll be trusted.

Try iPhone UI [^] a new fresh face for your Windows Mobile, here on Code Project.

GeneralRe: 2 way communication????
anusho
21:56 22 Apr '09  
Hi sir,
Thanks a lot for all suggestions,i will try out with the possibilities which you have provided and will catch you back very soon with few more queries... Once again thanks for allowing me to have my queries and for your valuable references and directions..

Cheers,
Anil Big Grin Thumbs Up
GeneralDOESNT WORK
dizzy59
18:53 9 Apr '09  
I GOT AN ERROR AT
m_tcpclient.Connect("PPP_PEER",5000);
IT DOESNT CONNECT TO DESKTOP
GeneralRe: DOESNT WORK
Dr.Luiji
11:13 10 Apr '09  
PPP_PEER is a registry entry stored in HKLM\Comm\Tcpip\Hosts that only exists when an ActiveSync connection is active. First of all enable your ActiveSync and check this entry.

Dr.Luiji

Trust and you'll be trusted.

Try iPhone UI [^] a new fresh face for your Windows Mobile, here on Code Project.

GeneralVery cool
Sacha Barber
22:49 6 Dec '08  
Have a 5

Sacha Barber
  • Microsoft Visual C# MVP 2008
  • Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralRe: Very cool
Dr.Luiji
5:16 7 Dec '08  
Thanks a lot man!

Dr.Luiji

Trust and you'll be trusted.

GeneralRe: Very cool
Sacha Barber
8:32 7 Dec '08  
u r welcome

Sacha Barber
  • Microsoft Visual C# MVP 2008
  • Codeproject MVP 2008
Your best friend is you.
I'm my best friend too. We share the same views, and hardly ever argue

My Blog : sachabarber.net

GeneralMy vote of 1
khansameer
23:55 5 Dec '08  
1
GeneralRe: My vote of 1 [modified]
Dr.Luiji
5:32 7 Dec '08  
khansameer wrote:
1


LOL

Thanks for the useless comment....
I apologize myself for writing this comment because it is also useless...

At least, Could you say to us why?

Dr.Luiji

Trust and you'll be trusted.

modified on Tuesday, December 16, 2008 10:10 AM

GeneralRe: My vote of 1
Wes Aday
7:45 28 Dec '09  
Dr.Luiji wrote:
Could you say to us why


Well I can guess.....

Of the 9 whole comments that he has made in the past 3 years, 6 of them are titled "My vote of 1". He's just voting you a "1" just becuase he can. Don't worry about it.

Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns

Help humanity, join the CodeProject grid computing team here

GeneralRe: My vote of 1
Dr.Luiji
22:47 1 Jan '10  
Thanks man

Dr.Luiji

Trust and you'll be trusted.

My Blog : The Code Is Art

GeneralAwesome
Quartz.
21:44 25 Nov '08  
Thats a great work, Dr. Luigi.


Omit Needless Words - Strunk, William, Jr.


Vista Gadget Book: Creating Vista Gadgets using HTML, CSS, & JavaScript. Sample chapter here Selling Your Gadget

GeneralRe: Awesome
Dr.Luiji
2:07 26 Nov '08  
Thanks for your message Rajesh!
I'm glad you liked it, let me know if you come across any suggestions to improve this.
Best Regards.

Dr.Luiji

Trust and you'll be trusted.

GeneralAlready done
thomy
5:02 13 Nov '08  
Nice article. There is another app at http://forum.xda-developers.com/showthread.php?t=427097[^] which does the same and much more. It is freeware.


Last Updated 21 Nov 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010