菜鸟之windows mobile编程

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

最近开始做windows mobile,之前我只用过一些简单的MFC,老大们都说不要用MFC,要用win32 API。搞了几天,现将心得记下来。使用win32 API编程实际上比较容易学,一般来说一个应用程序主要由几部分构成:

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

第一部分就是WinMain,它就是程序的出发点,相当于C的main

  1. int WINAPI WinMain(HINSTANCE hInstance,
  2.                    HINSTANCE hPrevInstance,
  3.                    LPTSTR    lpCmdLine,
  4.                    int       nCmdShow)
  5. {
  6.     MSG msg;

  7.     //TODO: 数据的初始化

  8.     // 窗口以及控件初始化
  9.     if (!InitInstance(hInstance, nCmdShow)) 
  10.     {
  11.         return FALSE;
  12.     }

  13.     ......

  14.     // 主消息循环:
  15.     while (GetMessage(&msg, NULL, 0, 0)) 
  16.     {
  17.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  18.         {
  19.             TranslateMessage(&msg);
  20.             DispatchMessage(&msg);
  21.         }
  22.     }
  23.     return (int) msg.wParam;
  24. }

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">


第二部分是数据的初始化,程序不同而不同。


rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

第三部分是窗口和控件的初始化以及显示:

  1. // 窗口以及控件初始化
  2. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  3. {
  4.     HWND hWnd;
  5.     TCHAR szTitle[MAX_LOADSTRING];      // 标题栏文本
  6.     TCHAR szWindowClass[MAX_LOADSTRING];    // 主窗口类名

  7.     g_hInst = hInstance; // 将实例句柄存储在全局变量中

  8.     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); 

  9.         WNDCLASS wc;
  10.     
  11.         wc.style         = CS_HREDRAW | CS_VREDRAW;
  12.         wc.lpfnWndProc   = WndProc;
  13.         wc.cbClsExtra    = 0;
  14.         wc.cbWndExtra    = 0;
  15.         wc.hInstance     = hInstance;
  16.         wc.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BASICSETTING));
  17.         wc.hCursor       = 0;
  18.         wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  19.         wc.lpszMenuName  = 0;
  20.         wc.lpszClassName = szWindowClass;

  21.     if (!RegisterClass(&wc))
  22.     {
  23.         return FALSE;
  24.     }

  25.     hWnd = CreateWindow(szWindowClass, szTitle, WS_VISIBLE,
  26.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);

  27.     if (!hWnd)
  28.     {
  29.         return FALSE;
  30.     }

  31.     ShowWindow(hWnd, nCmdShow);
  32.     UpdateWindow(hWnd);

  33.     return TRUE;
  34. }

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

使用RegisterClass来注册一个新的“窗口类”(它是概念上的一个类),之后就可以用CreateWindow来新建一个新的窗口或者控件。(它的第一个参数一定要是RegisterClass新建的类名,也是wc.lpszClassName),当然还有很多默认“窗口类”,在MSDN上是这样说(查找CreateWindow就能找到)


rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

The following predefined control classes can be specified in the lpClassName parameter.

BUTTON

Designates a small rectangular child window that represents a button the user can click to turn it on or off. Button controls can be used alone or in groups, and they can either be labeled or appear without text. Button controls typically change appearance when the user clicks them. For more information about buttons and the styles you can specify in the dwStyle parameter, see Control Styles.

EDIT

Designates a rectangular child window into which the user can type text from the keyboard. The user selects the control and gives it the keyboard focus by clicking it or moving to it by pressing the TAB key. The user can type text when the edit control displays a flashing caret; use the mouse to move the cursor, select characters to be replaced, or position the cursor for inserting characters; or use the BACKSPACE key to delete characters. For more information about edit controls and the styles you can specify in the dwStyle parameter, see Control Styles.

LISTBOX

Designates a list of character strings. Specify this control whenever an application must present a list of names, such as filenames, from which the user can choose. The user can select a string by clicking it. A selected string is highlighted, and a notification message is passed to the parent window. For more information about list boxes and the styles you can specify in the dwStyle parameter, see Control Styles.

MDICLIENT

Designates an MDI client window. This window receives messages that control the MDI application's child windows. The recommended style bits are WS_CLIPCHILDREN and WS_CHILD. Specify the WS_HSCROLL and WS_VSCROLL styles to create an MDI client window that allows the user to scroll MDI child windows into view.

RICHEDIT

Designates a Rich Edit 1.0 control. This window lets the user view and edit text with character and paragraph formatting, and can include embedded COM objects.

RICHEDIT_CLASS

Designates a Rich Edit 2.0 control. This control lets the user view and edit text with character and paragraph formatting, and can include embedded COM objects. Windows CE for Microsoft® Pocket PC does not support this control class.

SCROLLBAR

Designates a rectangle that contains a scroll box and has direction arrows at both ends. The scroll bar sends a notification message to its parent window whenever the user clicks the control. The parent window is responsible for updating the position of the scroll box, if necessary. For more information about scroll bars and the styles you can specify in the dwStyle parameter, see Control Styles.

STATIC

Designates a simple text field, box, or rectangle used to label, box, or separate other controls. Static controls take no input and provide no output. For more information about static controls and the styles you can specify in the dwStyle parameter, see Creating Controls.


rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

接着要把窗口和控件显示:


  1.     ShowWindow(hWnd, nCmdShow);
  2.     UpdateWindow(hWnd);




第四部分是窗口消息处理,窗口消息处理函数让程序活了起来。对于自定义的“窗口类”,这个处理函数是由用户在调用RegisterClass时指定的


  1.         WNDCLASS wc;
  2.     
  3.         wc.style         = CS_HREDRAW | CS_VREDRAW;
  4.         wc.lpfnWndProc   = WndProc;

消息循环是程序的主题:


  1.     // 主消息循环:
  2.     while (GetMessage(&msg, NULL, 0, 0)) 
  3.     {
  4.         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
  5.         {
  6.             TranslateMessage(&msg);
  7.             DispatchMessage(&msg);
  8.         }
  9.     }


rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

一个窗口消息处理函数看起来是这样的:





  1. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  2. {
  3.     int wmId, wmEvent;
  4.     PAINTSTRUCT ps;
  5.     HDC hdc;

  6.     static SHACTIVATEINFO s_sai;
  7.     
  8.     switch (message) 
  9.     {
  10.         case WM_COMMAND:
  11.             wmId    = LOWORD(wParam); 
  12.             wmEvent = HIWORD(wParam); 
  13.             // 分析菜单选择:
  14.             switch (wmId)
  15.             {
  16.                 case IDM_HELP_ABOUT:
  17.                     DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
  18.                     break;
  19.                 case IDM_OK:
  20.                     SendMessage (hWnd, WM_CLOSE, 0, 0);
  21.                     //保存并且释放资源
  22.                     BasicSettingSavenExit();
  23.                     break;
  24.                 default:
  25.                     return DefWindowProc(hWnd, message, wParam, lParam);
  26.             }
  27.             break;
  28.         case WM_CREATE:
  29.             SHMENUBARINFO mbi;

  30.             memset(&mbi, 0, sizeof(SHMENUBARINFO));
  31.             mbi.cbSize     = sizeof(SHMENUBARINFO);
  32.             mbi.hwndParent = hWnd;
  33.             mbi.nToolBarId = IDR_MENU;
  34.             mbi.hInstRes   = g_hInst;

  35.             if (!SHCreateMenuBar(&mbi)) 
  36.             {
  37.                 g_hWndMenuBar = NULL;
  38.             }
  39.             else
  40.             {
  41.                 g_hWndMenuBar = mbi.hwndMB;
  42.             }

  43.             break;
  44.         case WM_PAINT:
  45.             hdc = BeginPaint(hWnd, &ps);
  46.             
  47.             //将图片画出来
  48.                         DrawWindow(hWnd,hdc);
  49.             
  50.             EndPaint(hWnd, &ps);
  51.             break;
  52.         case WM_DESTROY:
  53.             CommandBar_Destroy(g_hWndMenuBar);
  54.             PostQuitMessage(0);
  55.             break;
  56.         case WM_ACTIVATE:
  57.             // 向外壳程序通知我们的激活消息
  58.             SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
  59.             break;
  60.         case WM_SETTINGCHANGE:
  61.             SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
  62.             break;
  63.         case WM_LBUTTONDOWN:
  64.             DoLBUTTONDOWN(hWnd,message,wParam,lParam);
  65.             break;
  66.         case WM_LBUTTONUP:
  67.             DoLBUTTONUP(hWnd,message,wParam,lParam);
  68.             wmId = wmId;
  69.             break;
  70.         case WM_MOUSEMOVE:
  71.             DoMouseMove(hWnd,message,wParam,lParam);
  72.             break;
  73.         case WM_CLOSE:
  74.             return DefWindowProc(hWnd, message, wParam, lParam);
  75.         case WM_QUIT:
  76.             return DefWindowProc(hWnd, message, wParam, lParam);
  77.         default:
  78.             return DefWindowProc(hWnd, message, wParam, lParam);
  79.     }
  80.     return 0;
  81. }



rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

说了一大堆,有没有现成的工程呢?有。我的机子上装了MS2005和它的MSDN,以及windows mobile开发工具包,代码和文档都在这些工具里。看看这张图片:




rel="File-List" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_filelist.xml"> rel="themeData" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_themedata.thmx"> rel="colorSchemeMapping" href="file:///C:%5CDOCUME%7E1%5CADMINI%7E1%5CLOCALS%7E1%5CTemp%5Cmsohtmlclip1%5C01%5Cclip_colorschememapping.xml">

也就是说代码就放在这个目录C:/Program Files/Windows CE Tools/wce500/Windows Mobile 5.0 Pocket PC SDK/Samples/CPP/Win32或者类似的目录。




评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值