Common VC skills

Source: Internet
Author: User

1. How to obtain the application instance handle?

The application instance handle is stored in cwinappim_hinstance. You can call afxgetinstancdhandle to obtain 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 tips:

(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): 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 end the application with vbprogramming skills? 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. This allows users to prompt whether to save the modified data.

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

You can also create a custom function terminatewindow.

Void terminatewindow (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 restarts windows.

I have already talked about music before, so I 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, that is, neterm, Calc, and so on, 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 www.setasp.com
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, 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"
 
9. How do I change the window icon?

Send the wm_section message to the window.

Example:

Hicon = afxgetapp ()-> loadicon (idi_icon );
Assert (hicon );
Afxgetmainwnd ()-> sendmessage (wm_section, true, (lparam) hicon );

10. How can I change the default window style?

Override cwnd: precreatewindow and modify the createstruct structure to specify the window style and other creation information.

Example: Delete "Max" button and set original window's position and size

Bool cmainframe: precreatewindow (createstruct & CS)
{
CS. Style & = ~ Ws_maxinizemox;
CS. x = cs. Y = 0;
CS. Cx = getsystemmetrics (sm_cxscreen/2 );
CS. Cy = getsystemmetrics (sm_cyscreen/2 );
Return cmdiframewnd: precreatewindow (CS );

}

11. How to center the window display?

Easy, call function cwnd: center windows
Example (1): center window (); // relative to it's parent
// Relative to screen
Example (2): center window (cwnd: getdesktopwindow ());
// Relative to application's mainwindow
Afxgetmainwnd ()-> center window ();

12. How can I maximize and minimize windows and MDI windows as soon as they are started?

Let's talk about the window first.

Set the m_ncmdshow value in the initstance function.

M_ncmdshow = sw_showmaxmized; // maximize
M_ncmdshow = sw_showminmized; // minimized
M_ncmdshow = sw_shownormal; // Normal Mode

MDI window:

If you want to create a new application, you can use the advanced button of the MFC Appwizard to maximize or minimize it in the MDI child window style group. You can also reload the precreatewindow function of the MDI window and set
Ws_maxmize or ws_minmize; If derived from cmdichildwnd, call cwnd: Show window in the oninitialupdate function to specify the style of the MDI child window.

13. How to keep the program in a very small state?

Interesting question

In case of large recovery program forms, Windows sends a WM_QUERY-OPEN message, uses classwizard to set the member function onqueryopen (), and adds the following code:

Bool cmainframe: onqueryopen ()
{
Return false;
}

14. How do I limit the window size?

That is, the fixeddialog format. In Windows, wm_getmaxmininfo is sent to track and respond to it. In ongetmaxmininfo, write the following code:

15. How to Make the window invisible?

It is easy to hide the window with sw_hide. You can use findwindow and showwindow to control the window.

16. How to keep the window at the forefront?

There are two ways.

Bringwindowtotop (handle );

Setwindowpos function, which specifies the top style of the window. Use ws_ex_topmost to extend the window style.

Example:

Void toggletopmost (cwnd * pwnd)
{
Assert_valid (pwnd );
Pwnd-> setwindowpos (pwnd-> getstyle () & ws_ex_topmost )? & Wndnotopmost: & wndtopmost, 0, 0, 0, ssp_nosize | wsp_nomove };

}

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.