1. How do I get an instance handle of the application?
The edition handle of the application is saved in CWINAPPIM_HINSTANCE, which can be called so
AfxgetInstancdHandle gets the handle.
Example: handle hinstance = afxgetinstancehandle ();
2. How to get the pointer of the application main window through the code?
The main window of the main window is saved in CWINTHREAD:: M_PMAINWD, calling the AFXGetMainWnd implementation.
AFXGETMAINWND () -> showwindow (sw_showmaxmize); // maximizes the program.
3. How to get an icon for other programs in the program?
Two methods:
(1) SDK function ShgetfileInfo or use Extractic to get the icon resource from the Handle,
(2) The SDK function SHGETFILEINFO has a lot of information about the file, such as size icon, attribute, type, etc.
Example (1): Displays the NotePad icon 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.chicon);
}
}
EXAMPLE (2): The same feature, Use Extracticon FUNCTION
Void CsampleView :: OnDraw (CDC * PDC)
{
HiCon Hicon = :: Extracticon (AfxgetInstanceHandle (), _ T
("NOTEPAD.EXE"), 0);
IF (Hicon && HiCon! = (HICON) -1)
PDC-> Drawicon (10, 10, Hicon);
}
Description: Get the path of NOTEPAD.EXE to get the getWindowsDirectory function, if it is a brush to call Win95, you should use the way to access the registry to get its path. To make a more elegant program, consider should comprehensively.
4. How to program end the app? How to program the control of Windows reboot?
This is a very simple and problematic issues that are often encountered.
First question, send a WM_CLOSE message to the window, call the CWnd :: ONCLOSE member function. Allow user tips
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;
}
Note: The FindWindow function is not advocated because it cannot process automatic changes in the title bar. For example, we have to detect that nothingpad is running, do not know the title bar of NOTEPAD in advance, then FindWindow can be powerful, you can enumerate the WINDOWS task list The way is achieved. In the Mechanical Press, "Windows 95 API Developer Guide" has a more detailed introduction, this is no longer speaking.
The second question, the USE EXITWINDOWSEX FUNCTION function control system is rebooted, or restarting Windows. There is no longer mentioned in front of it.
5. How to add other applications?
I remember this seems to be a problem with high appearance.
Three SDK functions WINEXEC, Shellexecute, and createProcess can be used.
Winexec is the easiest, two parameters, the previous specified path, the latter specified display method. The latter parameter is worth mentioning, such as the mud with sw_showmaxmize how to add a program without maximizing the button, huh, NETERM, CALC, etc. There will be no normal form, but it has been added to the task list. SHELLEXECUTE is flexible than WinExex, you can specify a work directory, the following EXAMPLE is open directly C: /TEMP/1.txt, without adding an application associated with TXT files, many installations will open a window after completion, to display Readme or FAQ, even if it is this. SHELLEXECUTE (NULL, NULL, _T ("1.txt"), NULL, _T ("C: // Temp"), sw_showmaxmized;
CreateProcess is the most complex, with a total of ten parameters, but most of them can be replaced with null, which can specify the security attributes of the process, inherit information, priority, etc., come to see very simple EXAMPLE:
StartupInfo Stinfo; // Start the window information
ProcessInfo procinfo; // process information
CreateProcess (NULL, _T ("NOTEPAD.EXE"), NULL, NULL.FALSE, NORMAL_PRIORITY_
Class, Null, Null, & Stinfo, & Procinfo;
6. Determine the path to the application
Some people have asked this question before.
Use getModuleFileName Get the path to the application and then remove the executable file name.
EXAMPLE:
Tchar exefullpath [max_path]; // max_path is defined in the API, as if it is 128
GetModuleFileName (NULL, EXEFULLPATH, MAX_PATH)
7. Get various directory information
Windows directory: Use "getWindowsDirectory"
System directory under Windows: Use "getsystemdirectory"
Temp Directory: Uses "getTemppath"
Current Directory: Use "getcurrentdirectory"
Please note the first parameter of the first two functions as a directory variable name, and then one is a buffer; the next two opposite.
8. How to customize the message
Some people have asked, in fact, it is not difficult.
(1) Manually defined the message, can you write #define WM_MY_MESSAGE (WM_USER 100),
MS recommended at least WM_USER 100;
(2) Write the message processing function, use WPARAM, LPARAM Returns LRESULT.
Lresult CMAINFRAME :: ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM)
{
/ / Add your handler
}
(3) Declaration at the AFX_MSG of the class, that is, "macro map"