One of Visual C ++ programming skills

Source: Internet
Author: User

One of Visual C ++ programming skills

1. How can I obtain the instance handle of an application?

2. How to get the pointer to the main window of the application through code?

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

4. How to program and end the application? How can I control windows reboot by programming?

5. How to add other applications?

6. Determine the Application Path

7. obtain various directory information

8. How to customize messages

 

1. How can I obtain the instance handle of an application?

The instance handle of the application is stored in cwinappim_hinstance.

Afxgetinstancdhandle to get the handle.

Example: handle hinstance = AfxGetInstanceHandle ();

2. How to get the pointer to the main window of the application through code?

The pointer of the main window is saved in cwinthread: m_pmainwnd, And the afxgetmainwnd implementation is called.

Afxgetmainwnd ()-> showwindow (sw_showmaxmized); // maximize the program.

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

Two methods:

(1) Use the SDK function shgetfileinfo or use extracticon to obtain the 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 of notepad.exe are generally obtained using the getwindowsdirectory function. If you call the paint brush under Win95, you should use the method of accessing the registry to obtain the path and create a more sophisticated program, the considerations should be comprehensive.

4. How to program and end the application? How can I control windows reboot by programming?

This is very simple and a common problem in programming.

First, send the wm_close message to the window and call the cwnd: onclose member function. The user can be prompted.

Whether to save the modified data.

Example: afxgetmainwindow ()-> sendmessage (wm_close );

You can also create a custom function terminate window.

Void terminate window (lpcstr pcaption)

{

Cwnd * pwnd = cwnd: findwindow (null, pcaption );

If (pwnd)

Pwnd-> sendmessage (wm_close );

}

Note: The findwindow function is not recommended because it cannot automatically change the title bar. For example, if we want to check whether notepad is running and do not know the title bar of notepad beforehand, findwindow will be powerless, you can do this by enumerating the Windows task list. I have a detailed introduction in the book "Windows 95 API developer Guide" by mechanical Publishing House.

The second question is whether the use exitwindowsex function control system is a reboot or a restart of windows. If you have mentioned this before, you will not mention it again.

5. How to add other applications?

I remember it was a high frequency of appearance.

Three SDK functions are available: winexec, ShellExecute, and CreateProcess.

Winexec is the simplest. It has two parameters: Specify the path before and specify the display mode after. the next parameter is worth mentioning. For example, if the mud uses sw_showmaxmized to add a program without the maximum button, such as neterm and calc, the normal form will not appear, but it has been added to the task list. ShellExecute is a little more flexible than winexex. You can specify a working directory. The following example is to directly open C:/temp/1.txtwithout planting the application associated with the TXT file, after many installation programs are completed, a window will be opened to display readme or FAQ. I guess that's exactly what it is.

ShellExecute (null, null, _ T ("1.txt"), null, _ T (" C: // Temp "), sw_showmaxmized );

CreateProcess is the most complex. It has a total of ten parameters, but most of them can be replaced by null. It can specify the process's security attributes, Inheritance Information, class priority, and so on. Let's look at a very simple example:

Startupinfo stinfo; // start window information

Processinfo procinfo; // Process Information

CreateProcess (null, _ T ("notepad.exe"), null, null. False, normal_priority _

Class, null, null, & stinfo, & procinfo );

6. Determine the Application Path

Someone seems to have asked this question a few days ago.

Use getmodulefilename to obtain the application path, and then remove the executable file name.

Example:

Tchar exefullpath [max_path]; // max_path is defined in the API, as if 128

Getmodulefilename (null, exefullpath, max_path)

7. obtain various directory information

Windows Directory: Use "getwindowsdirectory"

System Directory in Windows: Use "getsystemdirectory"

Temp Directory: Use "gettemppath"

Current Directory: Use "getcurrentdirectory"

Note that the first parameter of the first two functions is the directory variable name, the last is the buffer, and the last two are opposite.

8. How to customize messages

Some people have asked, but it is not difficult.

(1) manually define the message, so you can write # define wm_my_message (wm_user + 100 ),

The MS recommends at least wm_user + 100;

(2) write the message processing function and use wparam and lparam to return lresult.

Lresult cmainframe: onmymessage (wparam, lparam)

{

// Add your handler

}

(3) declare in afx_msg of the class, that is, macro ing"

 

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.