Simple touch menu on windows mobile
This application demonstrates use of touch on windows mobile by creating a simple game menu and interacting with it. Building on the directdraw features discussed in the previous two posts, we create simple game menu as shown.

We also go into full screen gaining exclusive access. The earlier app although covered the full screen, still some of the controls were getting activated behind the images. The problem there was that we were not using DDSCL_FULLSCREEN flag while creating the direct draw object. Also while experimenting with this app, there were problems using the window created by CreateWindow , instead had to use CreateWindowEx.
The menu screen is drawn on the surface “lpMenuBuffer” and is copied to “lpFrontBuffer” whenever required. We also use another dummy screen to switch between menu controls using touch input. The main idea how to deal with touch input is to handle the WM_LBUTTONDOWN message. This message is sent whenever there is a single tap on the screen. We handle this in the WndProc function. It is as shown.
The LOWORD macro retrieves the lower order WORD in the message paramenter “lParam”. This gives the x-coordinate of the click. Similarly, the HIWORD gives the y-coordinate of the click. These paramters are sent to the handle_input method is as shown below
The handle_input takes different actions based on (x,y) position and the state of the application. The (x,y) positions are matched with positions of the menu items and appropriate action taken. Here, we just switch between the GAME_MENU and a dummy screen. We move into dummy screen whenever the application is in GAME_MENU_STATE and it receives a touch/click input on any of the menu items. The dummy screen is as shown.

In all other states, whenever there is a click on the BACK button, it switches back to the GAME_MENU screen. On the GAME_MENU screen, a click on the EXIT button, quits the application.
The complete visual studio project is available for download from this link.

We also go into full screen gaining exclusive access. The earlier app although covered the full screen, still some of the controls were getting activated behind the images. The problem there was that we were not using DDSCL_FULLSCREEN flag while creating the direct draw object. Also while experimenting with this app, there were problems using the window created by CreateWindow , instead had to use CreateWindowEx.
The menu screen is drawn on the surface “lpMenuBuffer” and is copied to “lpFrontBuffer” whenever required. We also use another dummy screen to switch between menu controls using touch input. The main idea how to deal with touch input is to handle the WM_LBUTTONDOWN message. This message is sent whenever there is a single tap on the screen. We handle this in the WndProc function. It is as shown.
case WM_LBUTTONDOWN:
printf(" button down\n");
handle_input(LOWORD(lParam),HIWORD(lParam));
break;
The LOWORD macro retrieves the lower order WORD in the message paramenter “lParam”. This gives the x-coordinate of the click. Similarly, the HIWORD gives the y-coordinate of the click. These paramters are sent to the handle_input method is as shown below
void handle_input(int x,int y){
printf("x,y=%d,%d\n",x,y);
switch(game_state){
case GAME_MENU_STATE:
if(y>=40&&y<=(40+start_size.cy)){
int start_offset = (ScreenX-start_size.cx)/2;
if(x>=start_offset&&x<= (start_offset+start_size.cx)){
printf("Game running");
game_state = GAME_RUNNING_STATE;
update_frame(lpThirdBuffer);
}
}
if(y>=80&&y<=(80+continue_size.cy)){
int continue_offset = (ScreenX-continue_size.cx)/2;
if(x>=continue_offset&&x<= (continue_offset+continue_size.cx)){
printf("Game running");
game_state = GAME_RUNNING_STATE;
update_frame(lpThirdBuffer);
}
}
if(y>=120&&y<=(120+about_size.cy)){
int about_offset = (ScreenX-about_size.cx)/2;
if(x>=about_offset&&x<= (about_offset+about_size.cx)){
printf("about screen");
game_state = GAME_ABOUT_STATE;
update_frame(lpThirdBuffer);
}
}
if(y>=160&&y<=(160+exit_size.cy)){
int exit_offset = (ScreenX-exit_size.cx)/2;
if(x>=exit_offset&&x<= (exit_offset+exit_size.cx)){
printf("Game exiting");
game_state = GAME_EXIT_STATE;
handle_input(x,y);
}
}
break;
case GAME_ABOUT_STATE:
handle_back_button(x,y);
break;
case GAME_RUNNING_STATE:
handle_back_button(x,y);
break;
case GAME_EXIT_STATE:
release_resources();
SendMessage (main_window, WM_CLOSE, 0, 0);
PostQuitMessage(0);
break;
default:
PostQuitMessage(0);
}
}
The handle_input takes different actions based on (x,y) position and the state of the application. The (x,y) positions are matched with positions of the menu items and appropriate action taken. Here, we just switch between the GAME_MENU and a dummy screen. We move into dummy screen whenever the application is in GAME_MENU_STATE and it receives a touch/click input on any of the menu items. The dummy screen is as shown.

In all other states, whenever there is a click on the BACK button, it switches back to the GAME_MENU screen. On the GAME_MENU screen, a click on the EXIT button, quits the application.
The complete visual studio project is available for download from this link.
Labels: directdraw, touch, Visual C++, Windows mobile

3 Comments:
Awesome... Thanks for fulfilling my request... I was going to send you a tutorial I wrote for touch, but yours is much better...
You seem to have fullscreen on this app so I will try this solution out... On the reply you made about removing WM_Activate, that still kept the top taskbar... I also tried using SHFullscreen, but it is probably some setting in the init function/case... I am going to see and compare this project with the other one to figure out why SHFullscreen didn't work on the other one...
Thanks Once again :)...
Ran across a problem, your rect doesnt render right... For the background, I see while rect with random lines through it... Might be just because I am using the Kaiser, which is known to not have 2D acceleration because HTC decided to NOT buy drivers for qualcomm 7200... I'll try it on a touch pro soon and see if it works there...
Hey Thanks for your inputs.
I didn't come across the rect lines on my ASUS p527 but strangely it was hanging a lot unlike the other non-full screen application. Any thoughts?
Also, hey do send across your tutorials as well.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home