1. How do I get an instance handle of the application? The instance handle of the application is saved in CWINAPPIM_HINSTANCE, which can call the AFXGetInstancdHandle to get the handle .example: handle hinstance = afxgetInstanceHandle (); 2. How to get the application main window of the application main window? The pointer of the window is saved in CwinThread :: m_pmainwnd, call the AFXGetMainWnd implementation. AFXGETMAINWND () -> ShowWindow (sw_showmaxmize); // Maximize 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 to get the icon resource Handle, (2 SDK functions, SHGETFILEINFO, more information about files, such as size icons, properties, types, etc.example (1): display 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.hicon);}}}}} The same feature, Use Extracticon Functionvoid Csampleview :: OnDraw (CDC * PDC) {HiCon Hicon = :: Extracticon (AFXGetInstanceHandle (), _ T ("Notepad.exe"), 0); IF (Hicon && Hicon! = (Hicon) -1 PDC-> Drawicon (10, 10, HICON);} Description: Get NotePad.exe's path Formant 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, you want It is a more important program, considering it to comprehensively .4. How to program end applications? How do I program to control Windows reboot? This is a simple and programmed problems. The first question, send the window WM_CLOSE message, call CWnd :: OnClose member function. Allows whether the user prompts if the modified data is saved .Example: AFXGETMAINDOW () -> SendMessage (WM_CLOSE); you can also create a custom function Terminate Windowvoid Terminate Window (LPCSTR PCAPTION) {CWND * PWND = CWND :: FindWindow (NULL, PCAPTION); if (PWND) PWND -> Sendm Essage (WM_Close);} Description: The FindWindow function is not advocated because it cannot process the title bar automatically change, such as we want to detect NOTEPAD is not running, do not know the title bar of NOTEPAD in advance, then FindWindow does not work, you can Implemented by enumerating a WINDOWS task list. There is a more detailed introduction in the Mechanical Press, "Windows 95API Developer Guide", and no longer say more. 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 that this seems to be a high appearance frequency. 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 you do it. SHELLEXECUTE (NULL, NULL, _T ("1.txt"), NULL, _T ("C: // Temp"), sw_showmaxmized); CreateProcess is the most complicated, 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, and more. Take a very simple EXAMPLE: StartupInfo Stinfo; // Start the window information processInfo procinfo; // The process of the process CreateProcess (NULL, _T ("NOTEPAD.EXE"), NULL, NULL.FALSE, NORMAL_PRIORITY_CLAS, NULL, NULL, & Stinfo, & Procinfo; 6. Determines the path to the application, some people have asked This problem. IUSE getModuleFileName gains the path to the application and then removes the executable file name. Example: TCHAR exeFullPath [MAX_PATH]; // MAX_PATH defined it in the API, as if 128GetModuleFileName (NULL, exeFullPath, MAX_PATH) 7 to obtain a variety of directory information in the Windows directory: Use "GetWindowsDirectory" system directory under Windows: Use. "GetSystemDirectory" Temp Directory: Use "getTemppath" Current Directory: Use "getcurrentdirectory" Please note the first parameter of the first two functions as a directory variable name; the latter 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), the MS recommended at least WM_USER 100; (2) Write a message processing function, use WPARAM, LPARAM Return LRESULT.LRESULT CMAINFRAME :: ONMYMESSAGE (WPARAM WPARAM, LPARAM LPARAM) {// Add Your Processing Function} (3) Declaration at the AFX_MSG of the class, that is, the "Macro Mapping" 9. How to change the window icon? Send a WM_SECTION message to the window.
Example: Hicon Hicon = AFXGetApp () -> Loadicon; ASSERT (HICON); AFXGETMAINWND () -> SendMessage (WM_SECTION, TRUE, (LPARAM) Hicon; 10. How to change the default style of the window? Replay CWnd :: PreCreateWindow CREATESTRUCT structure and modify and specify the styles other creating .Example: Delete "Max" Button and Set Original window's Position and SizeBOOL 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 place the window? Easy, Call Function CWnd :: Center WindowsExample (1): Center Window (); // Relative to it's parent // Relative to ScreenExample (2): Center Window (CWnd :: GetDesktopWindow ()); // Relative to Application's MainWindowAfxGetMainWnd () - > Center window (); 12. How to make the window and MDI window to maximize and minimize? First, the window. M_nCmdShow set value in function InitStance .m_nCmdShow = SW_SHOWMAXMIZED; // maximize m_nCmdShow = SW_SHOWMINMIZED; // minimize m_nCmdShow = SW_SHOWNORMAL; // MDI windows normal way: If you are creating a new application can be used MFC AppWizard The Advanced button and the detection maximizes or minimizes the MDI sub-window style group; also overloads the MDI Window's PrecreateWindow function, sets WS_MAXMIZE or WS_MINMIZE; if you derive from cmdichildwnd, call the CWnd :: Show Window in the OnInitialUpdate function to specify MDI CHILD WINDOW style. 13. How to keep the program so small? That is very interesting: When the recovery program is large, Windows will send a WM_Query-Open message, set the member function with ClassWizard ONQUERYOPEN (), Add Following Code: Bool CMAINFRAME :: ONQUERYOPEN () {return false;} 14. How to limit the size of the window?