Friday, April 22, 2011

Windows CE Application - Create Buttons

Hi,
The content shown below will help you create a window, then create a button on top of it and add functionality to that button (i.e. do something if the button is pushed).
Here are the steps to to do so on Windows Embedded CE.

1. First of all lets create a window. Here is the code:

    g_hwnd1 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0,
         rcWorkArea.right, rcWorkArea.bottom, 
                                                         NULL, NULL, g_hInst, (LPVOID)NULL);

The c_szWndClass has to be a registered class, and the rcWorkArea.right and rcWorkArea.bottom parameters are the width and height of the screen respectively.

2. Now we will create a button on this window. Here is the code:

    g_hwndbtn1 = CreateWindow(L"button", L"Push Button", 
(WS_CHILD | WS_VISIBLE),
(rcWorkArea.right/2), 
(rcWorkArea.bottom), 
ButtonWidth, ButtonHeight, 
g_hwnd1, 
(HMENU)ID_BUTTON1, 
hInst, 
NULL);

    The ID_BUTTON1, ButtonWidth and ButtonHeight has to be defined prior to this code.
    hInst is the application instance which is passed into WinMain.

3. Now, lets add some functionality to this button, i.e. if this button is pushed then another window should open. Here we will use a callback function to achieve:


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    int wmId, wmEvent; 
    switch (message) 
    {
    case WM_COMMAND:
   wmId    = LOWORD(wParam); 
   wmEvent = HIWORD(wParam);
   switch (wmId)
   {
    case ID_BUTTON1:
SendMessage(hWnd,WM_LBUTTONDOWN,LOWORD(wParam),lParam);
     g_hwnd2 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0, rcWorkArea.right rcWorkArea.bottom, NULL, NULL, g_hInst, (LPVOID)NULL);
SendMessage(hWnd,WM_LBUTTONUP,wParam,lParam);
break;
          }
    }
return 0;
}


The other cases of the Callback function are not shown here, and they have to be added properly. Scroll down for the entire code:


#define ID_BUTTON1 0x8801
HWND g_hwnd1, g_hwnd2, g_hwndBtn1;
TCHAR const c_szWndClass[] = TEXT("Window");
HINSTANCE g_hInst;



int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{
MSG msg;
    HACCEL hAccelTable;
BOOL mess;
//Store the application instance in a global.
g_hInst = hInstance;
MyRegisterClass(hInstance);
// Perform application initialization:
    InitInstance (hInstance, nCmdShow);

    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)180);
    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return msg.wParam;    
}


ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASS wc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC) WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 4;
    wc.hInstance = hInstance;
    wc.hIcon = NULL;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND+1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = c_szWndClass;
    return RegisterClass(&wc);
}

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
RECT rcWorkArea;
SystemParametersInfo(SPI_GETWORKAREA, 0, (void*)&rcWorkArea, 0);
  // Create a window
 g_hwnd1 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0,  rcWorkArea.right, rcWorkArea.bottom, NULL, NULL, g_hInst, (LPVOID)NULL);
 ShowWindow(g_hwnd1 , nCmdShow);
 UpdateWindow(g_hwnd1 );
 // Create a button.

g_hwndbtn1 = CreateWindow(L"button", L"Push Button", (WS_CHILD | WS_VISIBLE),  (rcWorkArea.right/2), (rcWorkArea.bottom), ButtonWidth, ButtonHeight, g_hwnd1,  (HMENU)ID_BUTTON1, hInst, NULL);


  return TRUE;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
 switch (message) 
 {
case WM_COMMAND:
wmId    = LOWORD(wParam); 
wmEvent = HIWORD(wParam);
switch (wmId)
{                 
      case ID_BUTTON1:
          SendMessage(hWnd,WM_LBUTTONDOWN,LOWORD(wParam),lParam);
            g_hwnd2 = CreateWindow(c_szWndClass, TEXT(""), WS_VISIBLE, 0, 0,
                      rcWorkArea.right, rcWorkArea.bottom, NULL, NULL, g_hInst, (LPVOID) NULL);
          SendMessage(hWnd,WM_LBUTTONUP,wParam,lParam);
break;
      }
      default:
            return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}