Search for handles in MFC

Source: Internet
Author: User
Tags textout

A handle is the identifier of a window resource. It identifies the memory block occupied by the resource in the system. The application operates the window through the window handle. In addition to the window handle, any kind of resource has its own handle, such as the cursor handle and bitmap handle. The window ID is the unique identifier of the window in the application. You can obtain the window handle through the window ID.

Bytes -------------------------------------------------------------------------------------------------

What is the concept of control ID in VC ++? What is a handle?

Both concepts are the control identifier. These two concepts can be considered from both internal and external aspects ",
"" Refers to the identification of users (programmers). In the VC editing system, IDs are used by programmers to identify various controls; the "internal" id refers to the handle, that is, the handle is the identifier recognized by the internal operating system.
In addition, during the running period, the entire process does not have an ID, but only a handle. In the Editor (that is, during the non-running period of the Program), the ID exists, there is no handle, but for the same control, they refer to the same thing. The key is to see when it is used.

Bytes ------------------------------------------------------------------------------------------------

Mutual conversion between ID--HANDLE--HWND

ID-> handle, and hwnd =: getdlgitem (hparentwnd, ID );

ID-> pointer, and cwnd: getdlgitem ();

Handle-> ID, and Id = getwindowlong (hwnd, gwl_id );

Handle-> pointer, and cwnd * pwnd = cwnd: fromhandle (hwnd );

Pointer-> ID, and Id = getwindowlong (pwnd-> getsafehwnd, gwl_id );

Getdlgctrlid ();

Pointer-> handle, and hwnd = cwnd. getsafehandle () or mywnd-> m_hwnd;

//////////////////////////////////////// //////////////////////////////////////// ////////////////

First, use the: findwindow () function to obtain the handle of the Main Dialog Box.
The original function is as follows:
Hwnd findwindow (
Maid, // Class Name of the Main Window
Lptstr lpwindowname // window title
);
The return value of this function is the handle of the main window.
Then, the sub-window handle is obtained through the: findjavaswex () function.
The original function is as follows:
Hwnd find1_wex (
Hwnd hwndparent, // handle of the parent window
Hwnd hwndchildafter, // obtain the sub-window handle
Lpctstr lpszclass, // subwindow Class Name
Lpctstr lpszwindow // subwindow title
);
The above function obtains the sub-window handle through the parent window handle. You can choose one of the following two parameters.
Set the other parameter to null!

//////////////////////////////////////// //////////////////////////////////////// ////////////////

In the VC window class, there is a member variable m_hwnd, which represents the handle of the window. Therefore, you can obtain the desired handle by using some functions in VC to obtain the window pointer and then accessing its member variables.
For example, use this function to get the window pointer and then access its m_hwnd.
Afxgetmainwnd ();

//////////////////////////////////////// //////////////////////////////////////// //////////////

VC: code for obtaining program processes and window handles based on file names:

 

The code for finding the program process is as follows based on the matching of the module name and running file name in the system process (because the window class and window name are changing, this is the only option ).

 

// Create a system process Snapshot

Snapshot = createconlhelp32snapshot (th32cs_snapprocess, 0 );

// Find the first process

F = process32first (snapshot, & processliststr );

While (f)

{

Char * T1 = "3smeeting.exe"; // This is the running file name

If (* processliststr. szexefile = * T1 ){

Sprintf (szhello, "processid: % x EXE: % s", processliststr. th32processid, processliststr. szexefile );

Textout (HDC, Rt. Left, Rt. Top, szhello, strlen (szhello ));

Break;

}

F = process32next (snapshot, & processliststr); // continue searching

}

Closehandle (snapshot );

There are many ways to obtain other window handles in VC, but you need to find the window Handle Based on the window class and window title, such as findwindow. The window class and the window title are constantly changing, the following code is obtained through practice.

Obtain the window handle based on a part of the window name. The Code is as follows:

 

Int I;

// Use the desktop as the parent window to find the first main window

Hwndprevious = getwindow (get1_topwindow (), gw_child );

Lptstr m_pszexename;

While (iswindow (hwndprevious ))

{

Int I = getwindowtextlength (hwndprevious );

Getwindowtext (hwndprevious, szhello, I); // get the window title

// Only the following words remain unchanged in my window

If (strstr (szhello, "current user :")){

Sprintf (szhello1, "hwnd: % x title: % s", hwndprevious, szhello );

Textout (HDC, Rt. Left, Rt. Top, szhello1, strlen (szhello1 ));

Break;

// Match. At this time, hwndprevious is the handle of the window to be found.

}

Hwndprevious = getwindow (hwndprevious, gw_hwndnext );

}

Thank you for your correction.

Bytes ------------------------------------------------------------------------------------------------

Windows uses two character sets (ANSI and Unicode). The former is the common single-byte method. However, this method is inconvenient to process double-byte characters such as Chinese characters and is prone to the situation of half a Chinese character. The latter is in dubyte mode to facilitate the processing of dubyte characters. All character-related functions of Windows NT provide two versions, while Windows 9x only supports ANSI. _ T is generally related to a constant, for example, _ T ("hello "). If you compile a program in ANSI mode, _ t does not actually play any role. If a program is compiled in Unicode mode, the compiler saves the "hello" string in Unicode mode. The difference between _ T and _ L is that _ L is saved in Unicode no matter how you compile it.

Bytes ------------------------------------------------------------------------------------------------

Derivation and inheritance

1. Concept of a derived class

From the perspective of object-oriented programming, inheritance expresses the relationship between object classes. This relationship allows an object to inherit the features and capabilities of another object. If a class object inherits the features and capabilities of another class object, this class is called the derived class of the inherited class.

1.1 The general format of declaring a derived class is:

Class derived class name: the derived method (public or private) base class name {

// Newly added or modified data and member functions of the derived class };

1.2 execution sequence of the constructor and destructor of the derived class

When the derived class does not contain object members

● When creating a derived class object, the execution sequence of the constructor is: base class constructor → derived class constructor;

● When revoking a derived class object, the execution sequence of the Destructor is: constructor of the derived class → constructor of the base class.

When a derived class contains object members

● When defining a derived class object, the execution sequence of the constructor is: base class constructor → object member constructor → derived class constructor;

● When revoking a derived class object, the execution sequence of the Destructor is: constructor of the derived class → constructor of the object member → constructor of the base class.
.
1.3 constructor and destructor construction rules of derived classes

When the base class does not explicitly define a constructor or has a function but no parameter, the derived class can not pass parameters to the base class, or even do not define a constructor;
When the base class contains constructors and parameters, the derived class must define constructors to pass parameters to the base class constructors.

(1) The general format of the derived class constructor is:

Derived class: derived class constructor name (parameter table): base class constructor name (parameter table ){
// Add a member to a derived class
}

(2) When a derived class contains object members, the constructor generally takes the following form:
Derived class: derived class constructor name (parameter table): base class constructor name (parameter table), object member name (parameter table ),...... Object member name N (parameter table)

{// New data initialization (excluding object members)
}

2. Multi-Inheritance

The derived classes we introduced earlier have only one base class, which is called single-base Derivation or single inheritance. In practical use, we often need a derived class to have multiple base classes at the same time. This method is called multi-base Derivation or multi-inheritance.

2.1 Statement of multiple inheritance:

In C ++, the form of a derived class with more than two base classes is similar to that of a single base class. You only need to separate multiple base classes to be inherited with commas.

In multi-inheritance, public and private derivation have the same accessibility for the base class members in the derived class as the rule for single inheritance.
In addition, access to base class members must be unambiguous. If two base classes have data members or member functions with the same name, use the member name limitation to eliminate ambiguity, if the new member or member function in the derived class has the same name as the base class member or member function, the derived class will overwrite the member with the same name as the outer class, And the scope identifier must also be used.

2.2 multi-inheritance constructor and destructor:

The definitions of Multi-inheritance constructor are similar to those of single-inheritance constructor. Only constructors of N base classes are separated by commas.

The execution sequence of the Multi-inheritance constructor is the same as that of the single-inheritance constructor. The constructor of the object member is also executed after the base class constructor is executed, finally, execute the principles of the derived class constructor. Between multiple base classes, the sequence is arranged from left to right according to the declaration of the derived class. The execution sequence of the Destructor is opposite to that of the constructor.

2.3 virtual base class:

If some or all of the direct base classes of a derived class are derived from another common base class, the members inherited from the base class at the upper level will have the same name, when this derived class accesses the members of this common base class, it may produce ambiguity. In this case, you can define a virtual base class. This requires that, in the definition of its direct base class, the common base class should be defined as the virtual base class using the keyword Virtual. The syntax format is as follows:

Class derived class name: base class in Virtual Mode

The initialization of the virtual base class is syntactically the same as that of the general multi-inheritance class, but the calling sequence of the constructor is different. The Calling sequence of the virtual base class constructor is as follows:

1) at the same level, first call the constructor of the virtual base class, and then call the constructor of the non-virtual base class, the constructor of the object member, and the constructor of the derived class.

2) If the same level contains multiple virtual base classes, the constructor of these virtual base classes calls them in the order they are described

3) if the virtual base class is derived from a non-virtual base class, the base class constructor is still called before the derived class constructor is called.

3. Note

1. The base class provides its behavior and structure to the derived class. The derived class is responsible for correctly initializing the base class object.

2. Call the constructor of the base class with the correct parameters as part of the constructor of each derived class (discussed)

3. Common member functions cannot use this syntax to call basic class methods.

4. A class is only responsible for the construction of its direct base class. However, there are different virtual base classes.

5. The parameters of the derived class constructor should include the parameters you need to use and the basic class.

6. The derived class inherits the behavior and structure of the Base class, but does not inherit the constructor and destructor.

7. You need to call the copy constructor of the base class in the copy constructor of the derived class.

8. You need to call the assignment operator of the base class in the assignment operator of the derived class.

9. The derived class destructor do not explicitly call the basic class destructor.

10. The used virtual base class is initialized by the constructor of the final derived class. When an object is created, calls to the virtual base class constructor within the sub-object constructor are ignored.

11. public inheritance is the main mode of inheritance. Private inheritance is only used in special cases (for example, stack classes can be inherited from List classes, but it is not a list, re-export the members of the private base class .) Private inheritance has no polymorphism.

12. Do not use multiple inheritance when a single inheritance can achieve the goal

13. inherited advantages: code reuse, adding new classes and new features (such as satellite and patient monitoring), dynamic Association, and Polymorphism to running programs.

Bytes ------------------------------------------------------------------------------------------------

An event is triggered by an application, and a message is an application event that the operating system perceives. The event is converted into a Message Queue sent to the application. Messages can be generated by the operating system or by another message. The message processing function is a window process function.

Messages are implemented through the MSG structure. The structure is defined as follows:

Typedef struct {

Hwnd; // The Window handle for receiving messages. If it is a thread message, it is null.

Uint message; // Message ID

Wparam; // the exact meaning of the attachment function depends on the value of the message member.

Lparam; // the exact meaning of the attachment function depends on the value of the message member.

DWORD time; // Message Delivery Time

Point pt; // The Position of the message delivery time mark on the screen

} MSG, * PMSG;

Bytes -------------------------------------------------------------------------------------------------

1. Realize that reference pointing to null values cannot be used under any circumstances. However, the pointer can assign null values to the pointer.

2. In C ++, the reference should be initialized. However, pointers can be Uninitialized pointers (but null pointers are valid but dangerous ).

3. The fact that there is no reference pointing to a null value means that the code to be referenced is more efficient than the pointer. Because you do not need to test its validity before using the reference. Instead, pointers should always be tested to prevent being empty.

4. Another important difference between a pointer and a reference is that a pointer can be re-assigned to point to another object. However, the reference always points to the specified object during initialization and cannot be changed later.

In general, you should use pointers in the following situations. First, you should consider the possibility of not pointing to any object (in this case, you can set the pointer to null ), second, you need to be able to point to different objects at different times (in this case, you can change the pointer ). If you always point to an object and it does not change its direction once it points to an object, you should use references.

Another case is that when you reload an operator, you should use references. The most common example is the operator []. A typical use of this operator is to return a target object, which can be assigned a value.

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.