Hello, fainting lizard! Any friend who has studied programming will not be unfamiliar with Hello, but this time is not much in the past, it is a special procedure, not functional, is in thought. This is a very classic program, this is not wanting to list this program, but this is a typical framework for WinCe programming, which writes it to make us a very intuitive understanding of WinCe. Another important reason is that this example program gives a new code style. It is different from the previous Win32 program. I appreciate this code or the program structure, so I will write this program again. . But I did some small changes. Main purposes: * Understand the structure and principle of the WinCe program. * Appreciate and learn a very beautiful code style. * Learn about the method of writing Wince program using EVC (introduction IDE).
Open Embedded VC Select New.
How is the interface and VC6 is almost. We select the WCE Pocket PC 2002 Application, enter Hello at the project name, check whether the CPUS is selected from the ARM and X86. This should be different according to your development environment. As far as I said, first should debug on the PC, then compile the application that suits me, my PPC is ARM CPU so I need WCE X86 and WCE ARM, as for other, for example, MIPS or SH I have not considered. (If you want to develop commercial software or want more people to use your program, you should consider more types of CPUs.) Good proceedings:
We choose an empty project. Click Finish. Ok, we see that EVC has established an application framework for us, of course, in this example, it is empty. However, we can also find several files shown in the picture below in the directory. Well, add a header file helloce.h and a hellocE.c file to our project. They are listed below: You can copy directly, but you need to pay attention to compilation errors caused by some annoying formats (such as spaces). However, the best way is to manually input, they are not long. / / =========================================================================================================================================================================================== ====================== //Header file: helloce.h // ============================================================================================================================================================================================= ============================================================================================================================================================================================================= == // Return to the number of elements, primarily for search message list #define Dim (x) (sizeof (x) / sizeof (x [0])) // ------------- -------------------------------------------------- ----- // Data Type Definition // Struct DecodeUint {// The association structure of the message and message function uint code; lresult (* fxn) (HWND, UINT, WPARAM, LPARAM); // This functions Pointer}; struct decodeCmd {// uint code; lresult (* fxn) (HWND, Word, Hwnd, Word); // here is used herein}; // ------ -------------------------------------------------- -------------- # Define IDC_cmdbar 1 // Command Bar ID
/ / -------------------------------------------------------------------------------------------- ---------------------- // Function prototype // int initApp (hinstance); // Initialization application function prototype HWnd InitInstance (Hinstance, LPWSTR, INT); // Initialization Example Function Prototype INT Terminstance (Hinstance, Int); // Instance Termination Function Prototype // Window Processing Function Prototype LRESULT CALLBACK MainWndProc (HWND, UINT, WPARAM, LPARAM); // Message Handle LRESULT DOCREATEMAIN (hwnd, uint, WPARAM, LPARAM); LRESULT DoPaintMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoHibernateMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoActivateMain (HWND, UINT, WPARAM, LPARAM); LRESULT DoDestroyMain (HWND, UINT, WPARAM, Lparam;
/ / =========================================================================================================================================================================================== ===================================================================================================================================================================================== ============================================================================================================================================================================================================= = # include
/ / =========================================================================================================================================================================================== ===============// The entry of the program int WinStance Hinstance, Hinstance Hprevinstance, LPWSTR LPCMDLINE, INT NCMDSHOW) {msg msg; int RC = 0; hWnd hwndmain; // Initial application RC = initApp (hinstance); if (rc) return rc; // Initialization instance hWndmain = initInstance (Hinstance, LPCMDLINE, NCMDSHOW); if (hwndmain == 0) Return 0x10; // Application Message Cyclic While (GetMessage (& MSG, NULL, 0, 0)) {TranslateMessage (& MSG); DispatchMessage (& MSG);} // Instance Clear Return Terminstance (Hinstance, Msg.wParam);} // ---- -------------------------------------------------- ---------------- // Application initialization function // int initApp (hinstance hinstance) {WNDCLASS WC; // Registered main window wc.style = 0; / / Window style wc.lpfnwndproc = mainwndproc; // callback function wc.cbclsextra = 0; // extended class data wc.cbWndextra = 0; // extended window data wc.hinstance = hinst ance; // instance handle wc.hIcon = NULL, // icon wc.hCursor = NULL; // mouse wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH); wc.lpszMenuName = NULL; // menu wc.lpszClassName = szAppName ; // Name IF of the window class (RegisterClass (& WC) == 0) Return 1;
Return 0;} // -------------------------------------------- -------------------------- // Initialization Example // hwndinstance (Hinstance Hinstance, LPWSTR LPCMDLINE, INT NCMDSHOW) {hwnd hwnd; //// Storage program unsuccess to global variable hinst = hinstance; // Establish a main window hWnd = createWindow (Szappname, // window TEXT ("Hello Lizard"), // Window Title WS_Visible, // Style CW_USEDEFAULT, / / X coordinate CW_USEDEFAULT, / / Y Coordinate CW_USEDEFAULT, / / Initial Width CW_USEDEFAULT, / / Initial Height NULL, // Parent Window Null, // Menu, must be NULL, WINCE window does not support menu. Hinstance, // instance null; // A pointer to the parameter is created for the WM_CRATE message. / / If you cannot establish a main window to return failures (! Iswindow (hwnd)) return 0; // Display and update window functions ShowWindow (hwnd, ncmdshow); UpdateWindow (hwnd); return hwnd;} // ----- -------------------------------------------------- --------------- // Terminstance - Program Clear / Int Terminstance (Hinstance Hinstance, Int Ndefrc) {
Return ndefrc;} // ============================================ ===============================// The following is the message processing function of the main window //// ---------- -------------------------------------------------- ---------- // MainwndProc - Main Process Function, this is a callback function // LRESULT CALLBACK MainWndProc (HWND HWND, UINT WMSG, WPARAM WPARAM, LPARAM LPARAM) {INT i; // Search Message list If the corresponding function is written to handle this message, call this function for (i = 0; I Beginpaint (hwnd, & ps); DrawText ("Hello Dizom!"), -1, & rece, // is changed to Chinese DT_Center | DT_VCenter | DT_SINGLINE); EndPaint (HWND, & PS); Return 0;} // -------------------------------------------- -------------------------- // dohibernateMain - Processing Window Suspended Message (WM_Hibernate) function, this is the unique message of Wince, purpose // is the amount of memory usage to the smallest .//lResult dohib, wparam wparam, lparam lparam {// If the window is not an active, the command bar is canceled, release memory IF (getActiveWindow () ! = hWnd) Commandbar_Destroy (getdlgitem (hwnd, idc_cmdbar); return 0;} // -------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------- // doactivatemain - Treatment Window Activation (WM_ACTIVATE) Message function // LRESULT DOACTIVATEMAIN (HWND HWND, UINT WMSG, WPARAM WPARAM, LPARAM LPARAM) {hwnd hwndcb; // If the window is active and there is no command bar ((loword (wparam)! = Wa_inactive ) && (gitdlgitem (iDC_cmdbar) == 0)) {// Establish a command bar hwndcb = commandbar_create (hinst, hwnd, idc_cmdbar); // Add an exit button to the command bar commandbar_addadornments (hwndcb, 0, 0);} return 0;} // ------------------------ ------------------------------------------ // dodeStroymain - Processing Window Destroy (WM_DESTROY) message function .//lresult doDestroymain (HWND HWND, UINT WMSG, WPARAM WPARAM, LPARAM LPARAM) {PostquitMessage (0); Return 0;} I need some job before the compiler, of course, this is not a full must, it is necessary to work according to your development environment, my environment is, I need to compile Pocket PC 2002, and Chinese The environment because I installed the Chinese Image of Pocket PC 2002 SDK. So first, I need to change Download Directory, because Chinese's default directory is no longer / windows / start menu, I have changed to my documents, of course, you can also set up another directory you wish, but you should pay attention to not use / Windows / Start Menu (Default Directory) You need to run it through the Explorer. Another place that needs to be modified is to modify the resource tab of the Project Setings dialog, and set the language option to Chinese. As shown in the figure: Good compilation, we have completed the first Wince program. When I put it on my 568, after the operation was successful, my mood and the first programming predecessor were passed. But the real gain is no longer, although this is a very simple program, but we can learn some good programming ideas. The following is true start, let's "anatomy" this simple program. Harvest 1: All program code is in accordance with Hungarian nomenclas, we can know the role of the code almost no comments. Harvest 2: Change the way we have been using the window process function to list the window messages with a large number of Switch statements. With the decomposition of the window process into a single process, each process processes a specific message, the remaining window procedure is a code snippet, which is used to check whether the corresponding process is written to process the message. If there is, call the process No, pass the message to the default window process. The advantage of this structure is to divide the message processing into a separate block. First, the variables of all window processes are mixed in the top of the process; the second is to understand, we can understand the code that handles specific messages. The most important thing is that such structures are easy to transplant, each independent message processing function can be used to copy the past. I can't remember that if the function of a piece of code may be repeated more than three times, it should be packaged. The function may be the smallest form of the package code. Harvest three: The message mapping of the message handle, structural definition, and program files from the header file, the message mapping table of the main window process function We can see and a simple framework, perhaps through these we have a message mapping mechanism for MFC help. Ok, this time we saw a very simple, but I think it was a very beautiful WinCE application code (good code read and enjoyed). And master the general steps of writing applications with Embedded VC . Very harvest, but we still have a lot of things to learn, don't make it, continue to work hard, you and me are.