// Maximize the window
M_pMainWnd-> ShowWindow (SW_SHOWMAXIMIZED );
M_pMainWnd-> UpdateWindow ();
//////////////////////////////////////// //////////////////////////
// Extended style: enables the List to have the entire row selection, table, and whole row pre-selection functions
DWORD dwStyleEx;
DwStyleEx = LVS_EX_FULLROWSELECT | LVS_REPORT | LVS_EX_ONECLICKACTIVATE;
M_GndDzStatusList.SetExtendedStyle (dwStyleEx );
//////////////////////////////////////// //////////////////////////
// Set the cursor
SetCursor (AfxGetApp ()-> LoadCursor (IDC_CrossArrow ));
// Check the menu
PCmdUI-> SetCheck (m_bIsDrawLine );
// Color
PDC-> SetTextColor (RGB (0, 0, 0 ));
// Background color
PDC-> SetBkColor (RGB (192,255,192 ));
// Obtain the CString length.
CClientDC dc (this );
CString sTextWord;
CSize cSize;
CSize = dc. GetTextExtent (m_strBaseWord [m_BaseWordID]. TextWord,
M_strBaseWord [m_BaseWordID]. TextWord. GetLength ());
// Obtain the current font information
TEXTMETRIC tm;
CClientDC dc (this );
Dc. GetTextMetrics (& tm );
// Create the cursor width and height
CreateSolidCaret (tm. tmAveCharWidth/8, tm. tmHeight );
// Set the cursor
SetCaretPos (m_pPoint );
// Display the cursor
ShowCaret ();
//////////////////////////////////////// //////////////////////////
AfxMessageBox (_ T ("delete") + Purpose + _ T ("failed! "), MB_ICONSTOP );
// Select an item
M_ExpenseList.SetSelectionMark (0 );
// Insert the first entry
M_ExpenseList.InsertItem (nIndex, m_Purpose, 0 );
// Automatically select an item
M_GndDzStatusList.SetItemState (nItem, LVIS_SELECTED, LVIS_SELECTED );
// Automatically scroll to ensure that the selected items can be viewed
M_ContractList.EnsureVisible (nIndex, 1 );
// Delete % in CString
CString sString. Remove ('% ');
// Prevent repeated Box Addition
While (pStockMsgSet-> IsEOF () = FALSE)
{
If (m_StockTypeBox.FindStringExact (-1, pStockMsgSet-> m_StockType) <0)
M_StockTypeBox.AddString (pStockMsgSet-> m_StockType );
PStockMsgSet-> MoveNext ();
}
// Open the database
TRY
{
PStationNameSet-> Open ();
}
CATCH (CDBException, e)
{
AfxMessageBox ("unable to open the [StationName] Table,/n export factory site failed! ");
Delete pStationNameSet;
Return FALSE;
}
END_CATCH
//////////////////////////////////////// //////////////////////////
// Retrieve the current directory
Char Path [255];
GetCurrentDirectory (255, Path );
//////////////////////////////////////// //////////////////////////
// Use the dialog box to find the file
CString sSelectPathName;
CString sSelectName;
CString sSelectExt;
CFileDialog dlg (TRUE );
Dlg. m_ofn.Flags = OFN_NOCHANGEDIR;
Dlg. m_ofn.lpstrFilter = "Micrsoft Acess database (*. mdb)/0 *. mdb/0/0 ";
If (dlg. DoModal () = IDCANCEL)
Return FALSE;
// Create ODBC and select database
SSelectPathName = dlg. GetPathName ();
SSelectName = dlg. GetFileTitle ();
SSelectExt = dlg. GetFileExt ();
If (sSelectExt. CompareNoCase ("mdb ")! = 0)
{
AfxMessageBox ("the selected file extension must be. mdb! /N please reselect the database. ");
Return FALSE;
}
//////////////////////////////////////// //////////////////////////
// Display the drive letter
Void CFileMakerView: FillDriveInfo ()
{
DWORD dwDrives;
Char;
CString DriveName;
DwDrives = GetLogicalDrives ();
A = 'a ';
While (dwDrives> 0)
{
If (dwDrives % 2 = 1)
{
DriveName. Format ("% c", );
GetTreeCtrl (). InsertItem (DriveName, m_nImageClose, m_nImageOpen, TVI_ROOT, TVI_LAST );
}
A ++;
DwDrives/= 2;
}
}
//////////////////////////////////////// //////////////////////////
// Determine whether the program is running (BOOL CMakeSheetApp: InitInstance ())
HANDLE Handle;
Handle = CreateMutex (NULL, TRUE, _ T ("MakeSheet3.0 "));
If (Handle = NULL)
Return FALSE;
If (GetLastError () = ERROR_ALREADY_EXISTS)
{
AfxMessageBox ("MakeSheet3.0 is running! ", MB_ICONSTOP );
Return FALSE;
}
//////////////////////////////////////// //////////////////////////
// Obtain the directory where the System is located (compatible with WindowsNT)
Char cSysDir [100], cFileName [] = "// odbcjt32.dll ";
GetSystemDirectory (cSysDir, 100 );
// Connect two strings
Strcat (cSysDir, cFileName );
//////////////////////////////////////// //////////////////////////
// Add a bitmap to the dialog box
// Define (public) in the header file)
CDibApi m_BackBmp;
BOOL CDeviceQueryDlg: OnInitDialog ()
{
M_backbmp .openbmp (". // map .bmp ");
M_backbmp .initdibdata ();
}
Void CDeviceQueryDlg: OnPaint ()
{
M_backbmp .drawdib (& dc, 0, 0 );
}
//------------------------------------------------------------
I also want to write one. It happened recently!
# Error: Please use the/MD switch for _ AFXDLL builds
/************/
ALT + F7-> c/c ++-> Select Code Generation-> userun-time library in category
Debug Multihreaded Dll
//------------------------------------------------------------
Change the background of a static text box
Inherits the CStatic class and reloads the CtlColor.
HBRUSH CMyStatic: CtlColor (CDC * pDC, UINT nCtlColor)
{
// TODO: Change any attributes of the DC here
// TODO: Return a non-NULL brush if the parent's handler shocould not be called
// Return NULL;
ASSERT (nCtlColor = CTLCOLOR_STATIC );
PDC-> SetBkMode (TRANSPARENT );
Return (HBRUSH) GetStockObject (NULL_BRUSH );
}
//------------------------------------------------------------
Upload values in the controls in the two dialog boxes
Define a static variable m_judge in CDialog2
Assume that the variable corresponding to IDC_TEXT is m_text:
If (CDialog1.DoModul () = IDOK)
{
CDialog2: m_judge = m_text; //
}
//------------------------------------------------------------
These are often used in Microsoft source code.
1. Calculate the number of elements in the array
# Define NUMELMS (aa) (sizeof (aa)/sizeof (aa) [0])
2. Substitute for redirecting to goto by mistake
Do {
// Break when an error occurs;
Return TRUE;
} While (1 );
// Clear the job
...
Return FALSE;
//------------------------------------------------------------
About registering controls
ActiveX control file name is preferably short file name (8.3), because the corresponding file name in the registry is short file name,
If it is a long file name, it is changed to xxx ~ 1, xxx ~ 2, etc. This is prone to errors !!
For example, if a control name is MyActiveX. ocx, the result is recorded as MyActi ~ in the registry ~ 1. ocx. Make an experiment: Register
Then, change the file name to MyActiveX1.ocx. The result system still considers MyActiveX1.ocx as the original file !!
So it is prone to errors.
ActiveX Control File Name and path name should not use Chinese! Otherwise, registration is successful, but it cannot be used.
//-----------------------------------------------------------------------------
1) Get the Doc pointer in the View
2) obtain the MainFrame pointer in the App
3) obtain the MainFrame pointer in the View.
4) obtain the View (created) pointer
5) Get the pointer to the current document
6) Get the status bar and toolbar pointer
7) Get the status bar and toolbar Variables
8) obtain the menu pointer in Mainframe.
9) obtain the application class from any class
10) Get the pointer of the View class from the document class (1)
11) Get the document template pointer in the App
12) obtain the document class pointer from the document template
13) Get the document template pointer in the document class
14) Get the pointer of the View class from the document class (2)
15) obtain the pointer of another view class from one view class
In VC programming, the biggest obstacle and problem for students who have just started learning is the message mechanism and pointer acquisition and operation. In fact, these contents are basically required for every VC learning tool book, and can be solved through many MSDN problems.
The following text mainly refers to some of my experiences in Pointer usage in programming. please correct me if it is inappropriate.
Generally, the framework we use is the MFC App Wizard (exe) Framework generated by Wizard provided by VC. There are pointer acquisition and Operation Problems in both multi-document and single-document.
The following section describes the general framework and then the usage of pointers in multithreading. The class to be used must contain the response header file. First, we generally obtain the instance pointer this, which is supported by the class (view, document, and dialog box). The purpose of this is to send a pointer to other classes or functions through functions in the class, this allows you to operate and use functions in a non-class.
1) Get the Doc pointer CYouSDIDoc * pDoc = GetDocument () in the View. Only one View can have one document.
2) obtain the MainFrame pointer in the App
The m_pMainWnd variable in CWinApp is the MainFrame pointer.
Alternatively, you can: CMainFrame * pMain = (CMainFrame *) AfxGetMainWnd ();
3) obtain the MainFrame pointer CMainFrame * pMain = (CmaimFrame *) AfxGetApp ()-> m_pMainWnd in the View;
4) obtain the View (created) pointer CMainFrame * pMain = (CmaimFrame *) AfxGetApp ()-> m_pMainWnd;
CyouView * pView = (CyouView *) pMain-> GetActiveView ();
5) obtain the current document pointer CDocument * pCurrentDoc = (CFrameWnd *) m_pMainWnd-> GetActiveDocument ();
6) obtain the status bar and toolbar pointer CStatusBar * pStatusBar = (CStatusBar *) AfxGetMainWnd ()-> GetDescendantWindow (AFX_IDW_STATUS_BAR );
CToolBar * pToolBar = (CtoolBar *) AfxGetMainWnd ()-> GetDescendantWindow (AFX_IDW_TOOLBAR );
7) if the toolbar and status bar variables are added to the framework, you can also (CMainFrame *) GetParent ()-> m_wndToolBar;
(CMainFrame *) GetParent ()-> m_wndStatusBar;
8) obtain the menu pointer CMenu * pMenu = m_pMainWnd-> GetMenu () in Mainframe ();
9) obtain the application class from any class
Obtain it using the global function AfxGetApp () of MFC.
10) obtain the pointer of the View class from the document class
I am from Hangzhou.
The CDocument class provides two functions for positioning the View class:
GetFirstViewPosition () and GetNextView () virtual POSITION GetFirstViewPosition () const;
Virtual CView * GetNextView (POSITION & rPosition) const;
Note: The parameters in the GetNextView () Brackets use the reference method, so the value may change after execution.
GetFirstViewPosition () is used to return the POSITION of the first view (not a view pointer, but a POSITION value). GetNextView () has two functions: return the pointer of the next View class and use the reference call method to change the value of the input POSITION type parameter. Obviously, there is only one view class in the Test program. Therefore, you only need to call these two functions once to obtain the CTestView pointer as follows (you need to define a POSITION structure variable to assist in the operation ): CTestView * pTestView;
POSITION pos = GetFirstViewPosition ();
PTestView = GetNextView (pos );
In this way, you can access the pTestView pointer of the CTestView class. after executing a few sentences, the variable pos = NULL, because there is no next View class, naturally there is no POSITION for the next View class. however, these statements are too simple to have strong versatility and security features. As mentioned earlier, when you want to return a pointer to a specified class in multiple views, we need to traverse all view classes until the specified class is found. When judging whether a class Pointer Points to an instance of a class, you can use the IsKindOf () member function to perform a row check, for example, pView-> IsKindOf (RUNTIME_CLASS (CTestView ));
You can check whether pView refers to the CTestView class.
With the above foundation, we can get any class pointer from the document class. For convenience, we use it as a member function of the document class. It has a parameter that indicates the class pointer to be obtained. Implementation: CView * CTestDoc: GetView (CRuntimeClass * pClass)
{
CView * pView;
POSITION pos = GetFirstViewPosition ();
While (pos! = NULL ){
PView = GetNextView (pos );
If (! PView-> IsKindOf (pClass ))
Break;
}
If (! PView-> IsKindOf (pClass )){
AfxMessageBox ("Connt Locate the View./r/n http://www.VCKBASE.com ");
Return NULL;
}
Return pView;
}
Two View class member functions IsKindOf () are used to determine whether to exit the while loop. There are three possible reasons:
1. If pos is NULL, no view class exists;
2. pView has met the requirements.
1 and 2 are the same. This is because the GetNextView () function is to change the current view pointer to the POSITION of a view and return the current view pointer. Therefore, pos is the POSITION of the next View class of pView, it is possible that both pos = NULL and pView meet the requirements. When the required view is the last view and is the last view class, it is cited as follows. Therefore, two judgments are required.
Use this function in the following format (taking the CTestView pointer as an example): CTestView * pTestView = (CTestView *) GetView (RUNTIME_CLASS (CTestView ));
RUNTIME_CLASS is a macro. It can be understood simply as a function of converting the class name to CRuntimeClass as a pointer.
As for forced type conversion, it is also for the sake of security, because the pointer types from the same base class are mutually compatible. This forced type conversion may not be necessary, but it can avoid some possible troubles.
3. the integration of pointers 1 and 2 from one view class to the other, it is easy to obtain the method of mutual pointer acquisition between view classes: it is to use the document class as a transit, first use the method 1 to get the pointer to the document class, and then use the method 2 to get another view class using the view locating function of the document class. Similarly, you can implement a function:
(Assume you want to obtain a pointer to other view classes from CTestAView) CView * CTestAView: GetView (CRuntimeClass * pClass)
{
CTestDoc * pDoc = (CTestDoc *) GetDocument ();
CView * pView;
POSITION pos = pDoc-> GetFirstViewPosition ();
While (pos! = NULL ){
PView = pDoc-> GetNextView (pos );
If (! PView-> IsKindOf (pClass ))
Break;
}
If (! PView-> IsKindOf (pClass )){
AfxMessageBox ("Connt Locate the View .");
Return NULL;
}
Return pView;
}
Compared with GetView () in 2, this function adds the first sentence to get the document class pointer, and adds the document class pointer before GetFirstViewPosition () and GetNextView, to indicate that they are document class member functions.
With this function, to obtain the CTestBView pointer from CTestAView, you only need to: CTestBView * pTestbView = (CTestView *) GetView (RUNTIME_CLASS (CTestBView ));
11) multiple document templates can also be added to a single document. However, in general development, multiple document templates can be developed using the MDI method. The method is very similar to the method for obtaining the preceding view, please refer to MSDN (the following four contents (11, 12, 13, 14) Source: http://sanjianxia.myrice.com/vc/vc45.htm)
You can use CWinApp: GetFirstDocTemplatePostion to obtain the location of the first document template registered by the application;
Use this value to call the CWinApp: GetNextDocTemplate function to obtain the first CDocTemplate object pointer. POSITION GetFirstDocTemplate () const;
CDocTemplate * GetNextDocTemplate (POSITION & pos) const;
The second function returns the document template identified by the pos. POSITION is a value defined by MFC for iteration or object pointer retrieval. With these two functions, the application can traverse the entire document template list. If the retrieved document template is the last in the template list, the pos parameter is set to NULL.
//-----------------------------------------------------------------------------
FAQs for beginners:
1. How to Make the compiled VC program run properly on other machines without VC installed:
Select release on the Compilation toolbar and Rebuild All.
2. How to Make a button Gray:
Method 1: GetDlgItem (Button ID)-> EnableWindow (false );
Method 2: associate a control variable (such as m_button1) with the button to be dimmed, and then execute the following code:
M_button1.EnableWindow (false );
3. If you find that you want to operate a data source after creating a project, but you do not want to re-open a project, the method is as follows:
A. Click "create Class Wizard" or "MFC ClassWizard" in the menu ".
B. Click Add Class.
C. Enter the name of your database class and select CRecordset as the base class. The following operations will surely be done.
4. How to gray a menu:
Strictly speaking, this is not a preliminary problem, and its implementation method is also very simple.
The implementation principle of changing menus to gray is actually different from that of changing buttons to Gray:
The logic code is written by the programmer before the logic code is embedded into the message processing function UPDATE_COMMAND_UI of the menu item. When the program is executed, the program determines whether to gray the menu items based on the code in the UPDATE_COMMAND_UI:
A. click the "Create Class Wizard" or "MFC classWizard" menu item, select the desired menu ID from the Object IDs in Message Maps on the first option page, and select UPDATE_COMMAND_UI on the right, click Add Function to Add a message processing Function. Click Edit Code to Add the following Code to the message processing Function (a program I have done ):
Extern short MyRight;
PCmdUI-> Enable (MyRight = 111 | MyRight = 222 );
This code is used to determine whether the menu item is grayed out based on the value of the global variable MyRight. If the condition is not true, the menu item is grayed out. You can also compile a global function and determine whether the global function is grayed out based on the return value of the function.
5. How to Solve the unexpected end of file problem:
When you compile an MFC project, if the above prompt appears, and you are sure that the {} in your header file is supporting and does not contain ";", you can add the following code at the beginning of the header file:
# Include "stdafx. h" to solve this problem.
6. How can I find the source of a function definition?
Place the cursor before the name of the function to be queried, click the source browser on the menu, and select Definitions and References.
1 RECT, PRECT, NPRECT, LPRECT; what is the difference between them?
RECT is a structure that represents a rectangle.
PRECT indicates a pointer to the Hold Structure
NPRECT indicates the near pointer to the Hold Structure
LPRECT indicates a long pointer to the Hold Structure
The last three structures are the same in 32-bit Windows, and they are different in 16-bit Windows systems, these structure pointers are defined only for compatibility with the old 16-bit program. For today's programs, they are all developed on 32-bit Windows, so they are all the same, it depends on your habits.