-Wince Program Design (3rd edition)-1.7 helloce

Source: Internet
Author: User
Tags drawtext

Helloce
In Windows programming, the typical SDK style is criticized because a huge switch statement is always used during the window process. The switch statement analyzes the messages sent to the window process, so that each message can be processed independently. One of the advantages of such standard results is to force a similar structure to be added to almost all Windows applications, which makes it easier for a programmer to understand the code of another person. The disadvantage is that all the variables in the entire window process usually appear at the beginning of the process in disorder.

Over the years, I have explored a different style for my windows program. The main idea is to break down the winmain and winproc processes into manageable units that are easier to understand and to be converted to other Windows programs. Winmain is divided into several processes, including application initialization, instance initialization, and instance termination. As the core of all Windows programs, the message loop is also in winmain.

The window process is divided into several independent processes, each processing a specific message. The window process itself is a code framework. It only looks for the incoming message and checks whether there is a process to process the message. If yes, this process is called. If no, messages are sent to the default window process.

Divides the processing of messages into independent blocks, making the structure easier to understand. In addition, the Code segment for processing a message is greatly isolated from the other, which makes it easier for you to convert the code for processing a specific message from one program to another. Many years ago, I first saw a description of this structure from Ray Duncan's "programming power" column in PC Magazine. Ray is one of the legends in MS-DOS and OS/2 programming. Although I made some changes to this design to suit my needs, it should be attributed to Ray.

Code
The source code of helloce is shown in Listing 1-4:
Listing 1-4 helloce programs
Helloce. h
// ================================================ ======================================
// Header file
//
// Written for the book programming Windows CE
// Copyright (c) 2003 Douglas boling
// ================================================ ======================================
// Returns number of elements
# Define dim (x) (sizeof (X)/sizeof (X [0])

//----------------------------------------------------------------------
// Generic defines and Data Types
//
Struct decodeuint {// structure Associates
Uint code; // messages
// With a function.
Lresult (* FXN) (hwnd, uint, wparam, lparam );
};
Struct decodecmd {// structure Associates
Uint code; // menu IDs with
Lresult (* FXN) (hwnd, word, hwnd, word); // Function
};

//----------------------------------------------------------------------
// Function prototypes
//
Hwnd initinstance (hinstance, lpwstr, INT );
Int terminstance (hinstance, INT );

// Window procedures
Lresult callback mainwndproc (hwnd, uint, wparam, lparam );

// Message handlers
Lresult dopaintmain (hwnd, uint, wparam, lparam );
Lresult dodestroymain (hwnd, uint, wparam, lparam );
// ================================================ ======================================
// Helloce-a simple application for Windows CE
//
// Written for the book programming Windows CE
// Copyright (c) 2003 Douglas boling
// ================================================ ======================================
# Include <windows. h> // For all that Windows stuff
# Include "helloce. H" // Program-specific stuff

//----------------------------------------------------------------------
// Global data
//
Const tchar szappname [] = text ("helloce ");
Hinstance hinst; // program instance handle

// Message dispatch table for mainwindowproc
Const struct decodeuint mainmessages [] = {
Wm_paint, dopaintmain,
Wm_destroy, dodestroymain,
};

// ================================================ ======================================
// Program entry point
//
Int winapi winmain (hinstance, hinstance hprevinstance,
Lpwstr lpcmdline, int ncmdshow ){
MSG;
Int rc = 0;
Hwnd hwndmain;

// Initialize this instance.
Hwndmain = initinstance (hinstance, lpcmdline, ncmdshow );
If (hwndmain = 0) return 0x10;

// Application message loop
While (getmessage (& MSG, null, 0, 0 )){
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
// Instance cleanup
Return terminstance (hinstance, MSG. wparam );
}
//----------------------------------------------------------------------
// Initinstance-instance Initialization
//
Hwnd initinstance (hinstance, lpwstr lpcmdline, int ncmdshow ){
Wndclass WC;
Hwnd;

// Save program instance handle in global variable.
Hinst = hinstance;

# If defined (win32_platform_pspc)
// If Pocket PC, only allow one instance of the application
Hwnd = findwindow (szappname, null );
If (hwnd ){
Setforegroundwindow (hwnd) (DWORD) hwnd) | 0x01 ));
Return 0;
}
# Endif

// Register Application main window class.
WC. Style = 0; // window style
WC. lpfnwndproc = mainwndproc; // callback function
WC. cbclsextra = 0; // extra class data
WC. cbwndextra = 0; // extra window data
WC. hinstance = hinstance; // owner handle
WC. hicon = NULL, // application icon
WC. hcursor = loadcursor (null, idc_arrow); // default cursor
WC. hbrbackground = (hbrush) getstockobject (white_brush );
WC. lpszmenuname = NULL; // menu name
WC. lpszclassname = szappname; // window class name

If (registerclass (& WC) = 0) return 0;

// Create main window.
Hwnd = createwindow (szappname, // window class
Text ("helloce"), // window title
// Style flags
Ws_visible | ws_caption | ws_sysmenu,
Cw_usedefault, // X position
Cw_usedefault, // y position
Cw_usedefault, // initial width
Cw_usedefault, // initial height
Null, // parent
Null, // menu, must be null
Hinstance, // application instance
Null); // pointer to create
// Parameters
If (! Iswindow (hwnd) return 0; // fail code if not created.

// Standard show and update CILS
Showwindow (hwnd, ncmdshow );
Updatewindow (hwnd );
Return hwnd;
}
//----------------------------------------------------------------------
// Terminstance-program cleanup
//
Int terminstance (hinstance, int ndefrc ){
Return ndefrc;
}
// ================================================ ======================================
// Message handling procedures for Main Window
//
//----------------------------------------------------------------------
// Mainwndproc-callback function for application window
//
Lresult callback mainwndproc (hwnd, uint wmsg, wparam,
Lparam ){
Int I;
//
// Search message list to see if we need to handle this
// Message. If in list, call procedure.
//
For (I = 0; I <dim (mainmessages); I ++ ){
If (wmsg = mainmessages [I]. Code)
Return (* mainmessages [I]. FXN) (hwnd, wmsg, wparam, lparam );
}
Return defwindowproc (hwnd, wmsg, wparam, lparam );
}
//----------------------------------------------------------------------
// Dopaintmain-process wm_paint message for window.
//
Lresult dopaintmain (hwnd, uint wmsg, wparam,
Lparam ){
Paintstruct pS;
Rect;
HDC;

// Get the size of the client rectangle
Getclientrect (hwnd, & rect );

HDC = beginpaint (hwnd, & PS );
Drawtext (HDC, text ("Hello Windows CE! "),-1, & rect,
Dt_center | dt_vcenter | dt_singleline );

Endpaint (hwnd, & PS );
Return 0;
}
//----------------------------------------------------------------------
// Dodestroymain-process wm_destroy message for window.
//
Lresult dodestroymain (hwnd, uint wmsg, wparam,
Lparam ){
Postquitmessage (0 );
Return 0;
}

If you browse the source code of helloce, you will see the standard templates used by all programs in this book. The contained header file and macro are some global variables. I know there are a lot of good suggestions to persuade global variables not to appear in the program, but I use them to simplify the examples in this book and make them clearer. Each program defines a unicode string szappname, which is used in many places of the program. Hinst is also used in many places, and I will mention it when detailing the initinstance process. The global structure at the end is a message list that contains the process for processing related messages. The Window Process uses this structure to associate the message and the process of processing the message.

In helloce, winmain has two basic functions: Call initinstance (with application initialization code), process messages in the message loop, and call terminateinstance when the message loop exits. In this program template, winmain becomes a routine that does not need to be modified. In general, the only change in winmain is the modification of the message loop processing process, involving the processing of the keyboard accelerator key and monitoring of messages in the modeless dialog box and other tasks.

Instance Initialization
The main task of initinstance is to register the window class of the Main Window, create the main window of the application, and display the main window in the format specified by the ncmdshow parameter passed to winmain. For Pocket PC compilation, some Conditional compilation code is provided to prevent multiple instances of a program from running at the same time.

The first task of initinstance is to store the hinstance of the program instance handle in the global variable hinst. The instance handle of the program is useful in many Windows applications. I am saving this value here because I already know the instance handle and it is a convenient place to save the instance handle in the program.

When running on the Pocket PC, helloce uses findwindow to check whether its own copy is running. This function is searched in the top-level window to check whether the class name or window title match or both match. If a match is found, use the setfroegroundwindow function to put the window at the top. Then, the routine exits and the return code is 0. This causes winmain to exit and terminate the application. I will spend more time discussing Pocket PC-related code in chapter 17.

The Pocket PC-related code is included in the # If and # endif line. These rows tell the compiler to include them only when the # If statement conditions are true. In this example, if the constant win32_platform_pspc is defined, the condition is true. This constant is defined in [Project Settings. Go to the C/C ++ page in [Project Settings] to view the complete pre-processor-defined area. In this area, one of the definitions is $ (ceplatform-CE platform), and $ registers a value placeholder. In key [HKEY_LOCAL_MACHINE]/software/Microsoft/Windows CE tools/platform manager, you can find a series of registration keys, each representing a target platform installed in EVC ++. The ceplatform (CE platform) value varies depending on the target project. For Pocket PC and vintage palm-Size PC projects, ceplatform is defined as win32_platform_pspc.

The registration of window classes and the creation of the main window are similar to those in hello3. The only difference is that the global string szappname used as the main window class name is used. Every time I use this template, I can change szappname to the program name. This makes it easier for findwindow to work with different application window class names.

By now, the initinstance is complete, and the application main window has been created and updated, because a message is sent to the window before entering the message loop. Now it's time to look at this part of the program.

Window Process
When you write a Windows program, most of the programming time is spent on the window process. The Window Process is the core of the program and the place where the personality of the program is created by window behavior.

Compared with Windows programs that do not use libraries such as MFC, my programming style is obviously different in the window process. For almost all of my programs, the window process is the same as in helloce. Before proceeding further, I reiterate that this program structure is not particularly used in Windows CE. All my Windows applications use this style, whether it is Windows 3.1, me, XP, or CE.

This style simplifies the window structure into a simple table search function. The main idea is to find the Message Value in the mainmessages table defined in the C file earlier. If a message is found, the associated process is called and the original parameters are passed to the process for processing the message. If not found, call the default process defwindowproc. Defwindowproc is a Windows function that provides default behavior for all messages in the system, so that Windows programs do not have to process every message passed to the window.

The message table associates the message value with the process of processing the message. The table is as follows:
// Message dispatch table for mainwindowproc
Const struct decodeuint mainmessages [] = {
Wm_paint, dopaintmain,
Wm_destroy, dodestroymain,
};
This table is defined as a constant, which is not only a good programming habit, but also helps to save memory. Because the Windows CE program can be executed in ROM, constant data should be marked as constants. This allows the Windows CE program loader to store constant data in the Rom, instead of loading a copy into RAM, thus saving valuable Ram.

The table itself is a simple structure array, which contains two elements. The first is the message value, and the second is the pointer to the function that processes the message. Since the function can have any name, I used a consistent structure throughout the book to help you understand them. The name is prefixed by do (in line with object-oriented experience), followed by a message name and a suffix used to indicate window classes related to the table. For example, dopaintmain is the function name that processes the wm_paint message for the main window of the program.

Dopaintmain and dodestroymain
In helloce, the two message processing routines are paintmain and dodestroymain. They are similar to the case clause in hello3. The benefit of independent routines is that the Code is isolated from local variables. In the hello3 Window Process, the local variables for the painting code are concentrated at the top of the window process. Code encapsulation makes it easy to copy the code to your next application.

Run helloce
After inputting and compiling the program in EVC ++, you can choose [Build]-> [execute] helloce.exe in VC ++ or press Ctrl + F5 to remotely execute the program. The program displays a line of "Hello Windows CE" in the middle of the blank window. Figure 1-3 and figure 1-4 show how helloce runs on pocketpc. Click the [close] button on the title bar to send the wm_close message to the window by windwos ce. Although helloce does not explicitly process the wm_close message, the defwindowproc process uses the destroy main window as the default processing method. Because the window is being destroyed, the wm_destory message will be sent, which in turn leads to the call to postquitmessage.
Figure 1-3 (omitted)
The helloce window running on the embedded Windows CE system.

As I said, helloce is a very basic Windows CE program, but it shows you a basic framework of the application. You can add more content on it. If you use a browser to view the helloce.exe file, you will find that the program uses a common icon. When helloce is running, there is no icon next to the text in the buttons corresponding to helloce on the task bar. I will explain how to add custom icons to a program and how the drawtext function works in the following chapters.

Figure 1-4 (omitted)
Helloce window running on Pocket PC

Figure 1-4 shows how helloce runs on a Pocket PC. The helloce window stretched to the bottom of the screen. It is related to the specific switching mode between programs. The SIP (Input Soft Keyboard) button may be displayed on the helloce window. Applications designed for the Pocket PC will create a menu bar at the bottom of the screen, and buttons used to display soft keyboards and other things will be included.

You must manually adjust the window size to avoid overwriting the menu bar or overwriting the menu bar. Later, we will discuss how to design applications for the user interface of the Pocket PC. It is certain that the first chapter about Windows CE applies to Pocket PC devices and other Windows CE systems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.