Jia xubin, School of optoelectronic engineering, Chongqing University, describes the "handle" and "Pointer" as follows (afxgetmainwnd getsafehwnd () afxgetappname () afxgetthread)

Source: Internet
Author: User
Http://www1.ustc.edu.cn/personal/csli/vc_note/frame.htm

Sentence handle

Source: Fang Tang's description in 2003.9.22, which was previously cited by Mr. Jia xubin, because he wrote a wonderful article!
= Jia xubin, School of optoelectronic engineering, Chongqing University, describes the handle as follows:

The handle concept is an important concept in Windows Programming and plays an important role in many places. However, the resulting handling concepts are similar. For example, the concept of handling in <Microsoft Windows 3 developer's workshop> (Microsoft press, by Richard Wilton) is: in Windows, handles are used to identify projects, including:
*. Module)
*. Task)
*. Instance)
*. File)
*. Memory block (Block of memory)
*. Menu)
*. Control)
*. Font)
*. Resource, including icons, cursor, and string
*. GDI object, including bitmap, brush, Metafile, palette, pen, region ), and device context ).

WindowsProgramDoes not use a physical address to identify a memory block, file, task, or dynamic Mount module. On the contrary, Windows API assigns a fixed handle to these projects, return the handle to the application, and then use the handle to perform the operation.

In the book <windows programming short and fast> (Nanjing University Press), the handle is the unique integer that wondows uses to identify objects created or used by applications, windows uses various handle identifiers, such as application instances, windows, controls, bitmaps, and GDI objects. A Windows handle is a bit like a file handle in C.

From the two definitions above, we can see that a handle is an identifier used to identify an object or project. It is like our name, and each person has a name, different people have different names, but there may be people with the same name as you. In terms of data type, it is only a 16-bit unsigned integer. Applications almost always obtain a handle by calling a Windows function. Then other Windows functions can use this handle to reference the corresponding objects. In Windows programming, a large number of handles are used, such as hinstance, hbitmap, HDC, and hicon, there areA common handleIs handle, such as the following statement:

Hinstance;

You can change it:

HandleHinstance;

The preceding two statements are correct.

A Windows application can obtain the handle of a specific item in different ways. Many API functions, such as createwindow, globalalloc, and openfile, return a handle value. In addition, Windows can also transmit a handle to the application as a parameter through the app's function extraction. Once the application obtains a specific handle, you can operate the handle anywhere in windows. In fact, the extensive use of handles has affected every Windows program design.

The handle is meaningful only when a project is uniquely identified. The handle corresponds to one of the project tables, and only Windows itself can directly access this table. Applications can only process different handles through API functions. For example! For example, we can apply for a memory block for our application and return a handle value by calling the API function globalalloc:

Hmem = globalalloc (......);

In fact, the hmem value is only an index value, not a physical address, and the application cannot directly access this memory. There is another question here, that is, during programming, the memory allocated to the application can be moved or discarded, in this way, we can make full use of limited memory resources. Therefore, the address of the memory we allocate at a certain time is uncertain because it can be moved, so the memory block must be locked first. Here, the application needs to call the globallock function of the API function to lock the handle. As follows:

Lpmem = globallock (hmem );

In this way, the application can access this memory.

I think now you can understand the handle concept.ArticleIt can help you. In fact, if you have learned SDK programming, you will have a better and deeper understanding of the handle concept. If you are directly learning vc6's MFC programming, it is recommended that you take a look at SDK programming, which will be of great benefit to you.

=== Common functions related to handles or pointers

1. How can I obtain the instance handle of an application?AfxGetInstanceHandle ()
The application instance handle is stored in cwinappim_hinstance. You can call afxgetinstancdhandle to obtain the handle.
Example: handle hinstance = AfxGetInstanceHandle ();

2. How to passCodeGet the pointer to the main application window?Afxgetmainwnd getsafehwnd () afxgetappname () afxgetthread
The pointer of the main window is saved in cwinthread: m_pmainwnd, And the afxgetmainwnd implementation is called.

[Example] afxgetmainwnd ()-> showwindow (sw_showmaxmized); // maximize the program.

[Example] In this example, the main window is a dialog box. The following code is to implement related functions in another cfiletreectrl class (sub-window) in the Main Dialog Box (Main Window) in the following static text box (sub-window:
Cwnd * m_pcwnd = afxgetmainwnd (); // Get the main window pointer and access other sub-window resources through the main window pointer
// Method 1
M_pcwnd-> setdlgitemtext (idc_static_path, "cwnd *" + m_scurpath); // display the string in the subwindow (ID: idc_static_path) in the main window
M_pcwnd-> setdlgitemtext (idc_static_who, "Path Display completed by filetreectrl class :");
// Method 2
M_pcwnd-> sendmessage (stn_clicked); // send a message to the main window. The display task is completed in the main window.
// The. cpp in the main window contains the on_message (stn_clicked, onstaticpath3) description.

// some functions must be accessed through the window handle. We can use method 3 below
// cwnd :: getsafehwnd
// returns the window handle for a window
// hwnd getsafehwnd () const;
hwnd m_hwnd_tree = getsafehwnd (); // [note] only the current window (filetree class) handle
hwnd m_hwnd = m_pcwnd-> getsafehwnd (); // here is the handle of the main window (the handle of the main window is obtained by the main window pointer)

// Bool setwindowtext (hwnd, lpctstr lpstring)
: Setwindowtext (m_hwnd, "ok2222"); // modify the title of the main window.
: Setdlgitemtext (m_hwnd, idc_static_path2, "hwnd:" + m_scurpath );

[Other]Afxgetthread
Cwinthread * afxgetthread ();
Return Value: pointer to the currently executing thread.

3. How to Get icons of other programs in the program?AfxGetInstanceHandle ()

Hinstance AfxGetInstanceHandle ( );
Return Value
AnHinstanceTo the current instance of the application. If called from within a DLL linked with the usrdll version of MFC,HinstanceTo the DLL is returned.
Remarks
This function allows you to retrieve the instance handle of the current application.AfxGetInstanceHandleAlways ReturnsHinstanceOf your executable file (. EXE) unless it is called from within a DLL linked with the usrdll version of MFC. In this case, it returnsHinstanceTo the DLL.

Two methods:
(1) the SDK function shgetfileinfo or use extracticon to obtain the handle (handle) of the icon resource ),
(2) the SDK function shgetfileinfo obtains a lot of information about the file, such as the size icon, attribute, and type.

Example (1): the notepad icon is displayed in the upper left corner of the program window.
Void csampleview: ondraw (CDC * PDC)
{
If (: shgetfileinfo (_ T ("C: \ pwin95 \ notepad.exe"), 0,
& Stfileinfo, sizeof (stfileinfo), shgfi_icon ))
{
PDC-> drawicon (10, 10, stfileinfo. hicon );
}
}


Example (2): Same function, use extracticon Function
Void csampleview: ondraw (CDC * PDC)
{
Hicon =: extracticon (AfxGetInstanceHandle () , _ T
("Notepad.exe"), 0 );

If (hicon & hicon! = (Hicon)-1)
PDC-> drawicon (10, 10, hicon );
}
[Note] the paths for obtaining system files, such as win. ini system32.ini, are different in different systems. For example:
In general, the getwindowsdirectory function is used to obtain the notepad.exe path;
If you are calling the paint brush under Win95, You Should access the registry to obtain its path;
To make a more sophisticated program, we should consider it comprehensively.

[Other]
Hinstance Afxgetresourcehandle ( );
Return Value:
AnHinstanceHandle where the default resources of the application are loaded.

4. Obtain the desktop handleGetasktopwindow ()

MsdnExample:

// The static function cwnd: getdomaintopwindow returns the pointer of the desktop window. The following example illustrates the MFC
Void cframewnd: beginmodalstate ()
{
// First count all windows that need to be disabled
Uint ncount = 0;
Hwnd =: getwindow (: get1_topwindow (), gw_child );
While (hwnd! = NULL)
{
If (: isw.wenabled (hwnd )&&
Cwnd: fromhandlepermanent (hwnd )! = NULL &&
Afxisdescendant (pparent-> m_hwnd, hwnd )&&
: Sendmessage (hwnd, wm_disablemodal, 0, 0) = 0)
{
++ Ncount;
}
Hwnd =: getwindow (hwnd, gw_hwndnext );
}
}

// User problem: the following program does not take the same program handle, but the result returned by getmodulefilename is the same.

Hwnd chwnd; // subwindow handle
Hwnd hwdesktop =: getdesktopwindow (); // obtain the desktop handle
Chwnd =: getwindow (hwdesktop, gw_child); // obtain the desktop sub-Handle
Cstring cstitle, csclass, CSTM, mlookstring;
Char szbuffer [255];
While (chwnd! = NULL) // cyclically fetch the same level handle of the sub-Handle
{
If (: iswindowvisible (chwnd) // determines whether the window is displayed.
{
: Getwindowtext (chwnd, cstitle. getbuffer (255), 254 );
: Getclassname (chwnd, csclass. getbuffer (255), 254 );
Cstitle. releasebuffer (); // Title
Csclass. releasebuffer (); // Class Name
CSTM. Format ("% 08x:", chwnd );
If (cstitle = "")
{
Mlookstring = CSTM + csclass;
} Else
{
Mlookstring = CSTM + cstitle;
}
// The Window handle here is not the same program? ( Problem! ) But the result is the same as that of the others.
Hinstance = (hinstance ):: Getwindowlong (Chwnd, dwl_user );
:: Getmodulefilename (Hinstance, szbuffer, sizeof (szbuffer ));
MessageBox (szbuffer, mlookstring );
}
Chwnd =: getwindow (chwnd, gw_hwndnext );
}

Answer:

The problem is that getwindowlong (chwnd, dwl_user) in Win32)Always Returns the hinstance currently runningSo the file name you get is always one. Therefore, you must enumerate all "process program names" to obtain the program names.

=== What is the difference between the handle and pointer?

Many of my friends who are learning VC have heard of pointer and handle.
However, the differences between them are often unclear.
First, the handle is a sign of a window, that is, all the elements inherited from the cwnd class, which have the handle member.
What he can do is the only window that represents a desktop. A pointer is an address. If it points to an object in memory, you can perform any operation on it. Of course, it is not limited to your own applications, if you can get a pointer to an object of another application, you can also perform operations. However, to obtain the pointer, you must first find the handle of the window and use the fromhandle function to obtain the pointer.

=== Question 1:

How to send a string in a custom message? For example:
Sendmessage (mywnd, wm_userdefined, 0, 0)
How do I write string buffer into wparam or lparam?

You can pass the string address, because the address is exactly 32 characters. For example:
Char s [256];
Sendmessage (mywnd, wm_userdefined, (wparam) s, 0)
The receiver only needs to assign wparam to a char. However, this method can only be used to transmit data within a process.

=== Question 2:
1. in the VC application framework, how do I add a self-made class and define the Class Object? Can I generate this object only when I click a menu item with the mouse? (The constructor of this class has parameters ).
2. Message sending function:
Postmessage (hwnd handle, wm_mymessage,
Wparam, lparam)
Medium:
How to obtain the first parameter?
If my message is generated in my own application, can I display some data (using the textout function) in the window that I want to send to the application?
(This can also be said: Use Appwizard to generate the application framework, how to obtain the window handle in the generated class (such as cview), and put it in the postmessage function .)
3. What is the use of wparam and lparam in the message response function? How does VC ensure that these two data are transmitted to the function? There are many problems. Thank you!
Level: just getting started

Answer:

1. This is a positive question. You can use classwizard to define the class or manually enter it. If the class definition is already in a file, you only need to use project | add files to add the file to the project. To define class objects, you only need to define them in the corresponding events of your menu items. For example:
{
...
Myclass myobject ("hello ");
Myobject. mymethod ();
...
}
2. There is certainly no problem in passing messages in your own program. As long as you know that the window class to be called is inherited from cwnd, you can use the getsafehwnd function to obtain the window handle. However, it is too troublesome to use custom messages in the same program. You can add a member function to the class you want to call. If you want to display data, simply call this member function? Why use postmessage? Generally, you are willing to use custom messages only for inter-program calls. In this case, you can use findwindow to obtain the window handle (qa000251 "How to Use findwindow () function to find the program ").
3. For messages that have defined message processing functions in MFC, MFC automatically maps wparam and lparam to a more easy-to-use method. For example, onmousemove (uint nflags, cpoint point ). For functions that cannot be automatically processed by MFC, if you use on_message to define the message function, MFC will pass the original wparam and lparam to your function without any processing.

=== An undisclosed Win32 API function:Gettaskmanwindow ()

In the following example:Getprocaddress getparent (hwnd)
Hmodule huser32 =Getmodulehandle("USER32 ");

Download

// Getaskmanwnd. cpp (Windows NT/2000) /// use an undisclosed Win32 API function: gettaskmanwindow, // operate on the Windows taskbar (display/hide ). This function returns the window handle with the taskbar button. /// This example will show you how you can obtain a handle to the // Windows taskbar window. /// (c) 1999 Ashot Oganesyan K, Smartline, INC // mailto: ashot@aha.ru, http://www.protect-me.com # include <windows. h> # include <stdio. h> // USER32! Gettaskmanwindow (NT specific !) /// This function returns a handle to the window that ownes the taskbar buttons // hwnd gettaskmanwindow () // typedef hwnd (winapi * procgettaskmanwnd) (void ); procgettaskmanwnd gettaskmanwindow; void main (INT argc, char * argv []) {If (argc <2) {printf ("Usage: \ n \ ngetaskmanwnd.exe S | H \ n "); return;} hmodule huser32 = getmodulehandle ("USER32"); If (! Huser32) return; gettaskmanwindow = (procgettaskmanwnd) getprocaddress (huser32, "gettaskmanwindow"); If (! Gettaskmanwindow) return; Hwnd = gettaskmanwindow (); If (! Hwnd) return; If (* argv [1] = 'H' | * argv [1] = 'H') showwindow ( Getparent (hwnd) , Sw_hide); elseshowwindow (getparent (hwnd), sw_show );}

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.