Record of Windows Development Notepad Program (ii) logic Chapter 1_ Other synthesis

Source: Internet
Author: User

1. Main content

Start with this section to introduce the logical implementation section of Windows Development Implementation Notepad program. The main contents of this section are the following 3 points:

1. main window Definition--Main Introduction Notepad main interface window corresponding window class and realization plan

2. Selection and initialization of RichEdit control--selection and use of editing controls in Notepad program

3. The choice of the whole program icon. --Program icon settings

2. Actual development

2.1 Main Window implementation

The implementation of the previous introduction interface only gives the effect of running the interface, but the interface program was not able to respond to any Windows messages at the time, because the window at the moment was created with the corresponding process function of the window set to null. Now we need to add the corresponding process handler function so that this Notepad application can respond to a class of operation instructions that we issued. Therefore, in the development of this paper, a separate class Cmainwnd is designed to save the main interface window. This class defines the process handler function for the entire window MAIN_PORC. In Main_proc, any incoming messages can be processed (including initializing window messages, messages from other controls in the window, closing window messages, and so on). Take Windows with Notepad as an example, as shown in Figure 1

Figure 1 Windows main window message effect area

As shown in the figure above, in the Windows Notepad main interface, you need to respond to the various messages of the menu control in the red rectangular area, the related messages in response to the system buttons in the yellow rectangular area, and the messages in the corresponding edit control edit. For messages from various controls in the main window, Windows transmits with a WM_COMMAND message, which is also the core processing area of the entire program. The message that the System button is off is wm_close. Window initialization message Wm_initdialog is the initialization message that is emitted before the dialog window is constructed. In order to be able to respond to all of the above messages, you need to add a response function to these types of messages in Cmainwnd, so the basic implementation form for the entire cmainwnd is as follows:

Header File declaration:

/************************************************************************/
/* file  : CMainWnd.h 
 * Author:huagang Li
 * Date  : 2014-8-30 15:29:42
 * blogs:http://www.cnblogs.com/lhglihuagang/
 * Tips
   : The main Window implementation class, the realization of the window process function, message response function, etc.
 * *
/********************************************************************* /
#ifndef _main_wnd_h
#define _MAIN_WND_H

#include <Windows.h>
/////////////////////
//Cmainwnd main window class, providing

class Cmainwnd
{
Public:
  Static BOOL WINAPI Main_proc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM);
  Static BOOL Main_oninitdialog (HWND hwnd, HWND Hwndfocus, LPARAM LPARAM);
  static void Main_oncommand (HWND hwnd, int ID, hwnd hwndctl, LPARAM LPARAM);
  static void Main_onclose (HWND hwnd);

Private:
  static HWND Hmainwnd;  main window Handle
};


#endif

Cmainwnd Specific definition:


#include "MainWnd.h" include <WindowsX.h>//////////////////////////////////////////////////////////////////
Static data members HWND Cmainwnd::hmainwnd = NULL; static function members//main window process functions, Handling various messages based on message type BOOL WINAPI Cmainwnd::main_proc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM) {switch (umsg) {HA
 Ndle_msg (HWnd, Wm_initdialog, Main_oninitdialog);
 Handle_msg (HWnd, WM_COMMAND, Main_oncommand);
 Handle_msg (HWnd, Wm_close, main_onclose);
</span><span style= "color: #0000ff" >return</span><span style= "color: #000000" > FALSE; BOOL Cmainwnd::main_oninitdialog (HWND hwnd, HWND Hwndfocus, LPARAM LPARAM) {return TRUE;}//ID number for specific space, available in RESOURC
e defines a meaningful control ID, such as "Open file" can be set//for Id_file_open void Cmainwnd::main_oncommand (HWND hwnd, int ID, hwnd hwndctl, LPARAM LPARAM) {switch (ID) {//}} void Cmainwnd::main_onclose (HWND hwnd) {:: EndDialog (hwnd, NULL); }

After defining the Cmainwnd, add the process handler function of the main window at the DialogBox at the main function


::D Ialogbox (hinstance, Makeintresource (Idd_main), NULL, Cmainwnd::main_proc);

After you have completed the steps above, you can see that the startup main interface can respond to the system button "off" on the window, but the message for the menu control, because the message responds to this or the number in the function, does nothing, so there is no processing.

Selection of 2.2 RichEdit control

For a Notepad program, the core area of the main interface is still the editing area. However, no editing controls are selected in the current Notepad program. By looking at the list of existing Windows controls, you can see that the edit control is appropriate for editing controls, as well as rich Edit2.0. For both editing controls, edit control is simpler, but the response is less powerful. Rich Edit2.0 Control is more complex to implement, but it also has a lot of features (for example, font color, font size, and so on) can be changed. This article hopes to implement a strong Notepad, so select Rich Edit2.0 control for follow-up development. After you insert rich Edit2.0 control, the resource view for the main interface window is shown in Figure 2:


Figure 2 Insert Rich Edit2.0 control in Idd_main

After the above steps run, I thought you can see the Notepad with the editing interface, but in fact, the program has no effect after running, even the main interface can not start normally. Baidu found that for RichEdit startup failure method is for MFC program, need to add initialization function AfxInitRichEdit2. But now using the Windows API development, and not AfxInitRichEdit2 this function, can only find another way. Finally, in a blog post http://blog.csdn.net/dijkstar/article/details/7953816 mentioned that the above initialization function is mainly loaded with RichEdit dependent DLL, then the whole problem is enlightened, We just need to load this DLL manually before the main window starts. Therefore, the following actions were added to load the DLL before the dialogbox of the main function:

:: LoadLibrary (T ("Riched20.dll"));

Properties in main:

When you run the program at this point, you can start Notepad normally, and you can edit it in RichEdit, as shown in Figure 3:


Figure 3 The main interface window appears after loading Riched20.dll manually

After you start the main interface, you can edit it normally. It looks like the control is working properly. But in the actual test, the following problems were found:

1. After the interface operation RichEdit border edges and corners too clear

Processing method: RichEdit control's properties-> boarder–> flase

2. Enter cannot wrap (always edit on the same line when manually entered)

Processing method: RichEdit control's properties-> multiline–> True

RichEdit control's Properties-> Want return–> True

3. No scroll bars (horizontal and vertical)

This is on the main interface attribute, IDD

Processing method: Idd_main-> Properties-> Horizontal scrollbar–> True

idd_main–> Properties-> Vertical scrollbar–> True

4. Cannot scale with window size

The size of the RichEdit control remains the same size when the window is scaled, as shown in Figure 4:


Figure 4 the size of the RichEdit control is unchanged when the main interface size changes

The problem is well understood, because Windows will send a WM_SIZE message notification window when the Flex Main interface window is in place. This process is similar to the way Windows says to the main interface window "Hi, your size has changed and you are changing the size of the change." Now our main window process function does not specifically deal with the wm_size message for the RichEdit, so the RichEdit below the main interface keeps its original size before it appears. The specific solution is to add the RichEdit size Adaptive feature to the InitDialog, and to add main_onsize functions for wm_size messages to handle this stand-alone control. The specific code implementation is as follows:

void Cmainwnd::main_onsize (HWND hwnd, UINT State, int cx, int cy)
{
 RECT strect;
 :: GetClientRect (hwnd, & Strect); Get window client area size
</span><span style= "color: #008000" >//</span><span style= "color: #008000" > Resize RichEdit to Customer area size </span>
 : MoveWindow (:: GetDlgItem (HWnd, Idc_richedit), Strect.left, Strect.top
 , Strect.right-strect.left, Strect.bottom-strect.top, TRUE);

Here just added the message response function for wm_size in Cmainwnd, and to get RichEdit to respond to this message, you also need to add the appropriate procedure to the Main_proc

Handle_msg (HWnd, wm_size, main_onsize);

In this way, RichEdit can also scale freely with the main window size. In the above process processing function, three basic API interfaces are mainly called.

1. GetClientRect, this API is used to get the client area size, the size of the RichEdit scale is this size value

2. GetDlgItem gets the handle to one of the controls under the window, such as GetDlgItem (HWnd, Idc_richedit), to get the handle to the RichEdit control under the main window.

3. MoveWindow. Its first parameter is the window handle that needs to be changed to the size of the position. Here we pass the RichEdit handle in 2, followed by the left point of the rectangular region, the top point, the width value, and the height value. The last parameter is used to indicate that the window should not be redrawn when the size changes. Note that true is selected here. If you choose False, the following situation occurs: When the window is smaller, the RichEdit is not immediately adapted to the larger area or to the smaller area. The effect is as shown in Figure 5:

Figure 5 Potential problems with the parameter repaint set to False in Movewindows

Based on this, when we use MoveWindow to change the window size, it is best to make repaint true and ensure real-time change.

2.3 Main program Icon design

In the above screenshot can be seen, the main interface of the upper left corner icon has always been windows with the icon. To be similar to Windows with Notepad, go directly to the Internet to find a similar JPG icon to ICO, and then set the program icon. Specific icon setting method please refer to http://www.cnblogs.com/lhglihuagang/p/3927283.html

After the icon is set, you can run the program to see the latest effects, as shown in Figure 6


Figure 6 The result of setting the program icon

Finally, the dialog window Title dialog really seems to be somewhat different, here according to the title of Windows with Notepad "Untitled-Notepad", the value of the corresponding changes, specifically for idd_main-> properties-> caption– > Untitled-Notepad last, dialog box.

3. Operating Results

After adding the Cmainwnd and RichEdit, the running effect of the entire program is shown in Figure 7 below:


Figure 7 Effect of changes in this section of the program

4. Conclusion

1. When using the RichEdit control, you need to load the Riched20.dll manually, otherwise the program does not have any interface effects after running

2. RichEdit line, scroll bar, border can be set through the corresponding fields in properties

3. Need to add Wm_size message response function in Cmainwnd to ensure richedit free scaling.

5. Reference links

[1] http://blog.csdn.net/dijkstar/article/details/7953816

[2] Http://www.cnblogs.com/lhglihuagang/p/3927283.html

[3] http://msdn.microsoft.com/en-us/library/ms633534 (vs.85). aspx

6. Notes

This will be a series of posts, followed by the steps to complement the development of the logical features. Hope to be able to communicate with more Bo friends. If you think this article can also, please point to praise, haha ~ ~
Statement: No explanation, then the article for the original cake. NOTE: Reprint must retain the full text, if need to modify, please contact the author.

Related Article

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.