VC operation SQL Server master slave table
Author: Yuanhuai Shan spare time, made a small program with VC, a VC SQL server mode, the program content is relatively simple, the main set and the following: 1. How window is divided 2. How to communicate between multi-view 3. The operation and event processing of the list view 4. The operation and event processing of the tree control is used to dynamically generate a directory based on the database. 5. Implementation of the display of the master-slave table tells the program details;
I. How to split the window
Splitting windows are applicable to the SDI application, but also suitable for MDI applications. It usually resident is expressed in class csplitterwnd. For Windows, CSPLitterWnd is a real window that fully occupies the client area of the frame window and the view occupies the pane area of the cut window. Dynamic Split and Static Separation Dynamic Separation allows the user to separate the window at any time, and the user can separate by the selection menu or by dragging the split frame in the scroll bar. The dynamic split window is used by a view class. Static cutting, when the window is created for the first time, the pane has been cut, they can't be changed. The user can move the pane border, but the window cannot be merged or re-divided. The static diversity window allows multiple view classes to be used, and these view classes are configured when they can be created. In a static cutoff window, each pane has its own scroll bar. Dynamic split is relatively simple, not practical, look at static separation below. BOOL CMainFrame :: OnCreateClient (LPCREATESTRUCT lpcs, CCreateContext * pContext) {CRect rect; GetClientRect (& rect); m_wndSplitter1.CreateStatic (this, 1,2); m_wndSplitter1.CreateView (0,0, RUNTIME_CLASS (CClassTreeView), CSize (150,150) , pContext); m_wndSplitter2.CreateStatic (& m_wndSplitter1,2,1, WS_CHILD | WS_VISIBLE, m_wndSplitter1.IdFromRowCol (0,1)); m_wndSplitter2.CreateView (1,0, RUNTIME_CLASS (CDagDetialView), CSize (0,0), pContext) M_Wndsplitter2.createView (0, 10, Runtime_Class (CDagView), CSIZE (0, 0), PCONText); M_WndSplitter2.seRowInfo (0, Rect.Height () / 2,0); return true; // No The onCreateClient ()} of the category class is the method used by this program Demo. For details, please refer to MSDN.
II. Communication between multi-views.
This program Demo used three view classes, Winzard generated one, and the rest is I added. About communication between multi-view is mainly Pointer, how you want to visit, how to? There are two ways 1, and if you share a document class if you have multiple views, you can use the main frame. Since my program is a document class, other view classes are independent, so the second method is adopted. The following code is an example of a CDagDetialView class pointer, the rest of the class. CMainFrame * pFrame = static_cast
To tell the truth, there are many attributes of the list, standard, extended style parameters, a lot of stacks, see those English documents, search hard, find a feature they need, and the taste is not mentioned. Fortunately, I am very patience :) It's fine, I have remembered a big half, I am afraid. List controls displays data, use LVS_REPORT style, better look, just like DataGrid. Setting the window style requires the setWindowl and this API function. This function is common. The parameter is also a lot. The clicking event of the processing list is to process the message nm_click, and the event when the selection is changed is to process lvn_itemchanged, and different messages may be different, and take different time. void CDagView :: OnItemchanged (NMHDR * pNMHDR, LRESULT * pResult) {NM_LISTVIEW * pNMListView = (NM_LISTVIEW *) pNMHDR; // TODO: Add your control notification handler code here CListCtrl & refListCtrl = GetListCtrl (); if (pNMListView-> uNewState & LVIS_SELECTED) ... // When selecting a list item, the event triggers presult = 0;} The same method is handled by clicking, double-clicking and other incidents, no longer mentioned.
four. The operation and event processing of the tree control, using recursive according to database dynamic generation directories
Tree controls, like list controls, very important in VC data development. Often used to display grading, classified information. In fact, it is mentioned that a large pile of trees in the mind, what is the binary tree, find a tree, Hawman tree, and I remembered the common algorithm of traversal trees - recursive. Although the stack is more efficient, the use of recursive often makes the program design. Look at the following frame: PRS-> MoveFirst (); _variant_t var; cstring strablename, stratable; htreeItem hchildItem; while (! Prs-> adoEof) {var = PRS-> getCollect ("menucode"); if (var.vt! = VT_NULL) strTableCode = (LPCSTR) _bstr_t (var);! if (strTableCode.Find (strParent, 0) && strTableCode.GetLength () == strParent.GetLength () 2) // recursive function namely {hChildItem = refTree outlet . INSERTITEM (STRTABLENAME, HTREENODE, TVI_LAST); Inittree (Reftree, HchildItem, STRTableCode);} PRS-> MoveNext ();} ... Take advantage of the tree structure when recursive. Then combined with recursive thinking, fused together, Here is the theory and the actual combination point. Different development languages provide operation interfaces, such as delphi, .dotnet. I am in .Net also implements a similar tree, the algorithm thinks, just different operations, because the interface provided by the DOTNET is different. Event tree control process: void CClassTreeView :: OnSelchanged (NMHDR * pNMHDR, LRESULT * pResult) {NM_TREEVIEW * pNMTreeView = (NM_TREEVIEW *) pNMHDR; // TODO: Add your control notification handler code here CTreeCtrl & refTreeCtrl = GetTreeCtrl (); HTREEITEM hSelected = pNMTreeView-> itemNew.hItem; if (hSelected = NULL!) {m_strMenuName = refTreeCtrl.GetItemText (hSelected); if (AfxGetMainWnd () -> IsWindowEnabled ()) UpdateDagView ();}
// ----------- * PRESULT = 0;}
Fives. Implement the display of the master-slave table
Since I assume that you don't know how many fields in the database, the field name is not known. All are dynamically generated. So first retrieve primary keys. After retrieving the main key, then get the list of data corresponding to the primary key column of the list. Then use these primary key columns to retrieve the data from the table as the value of the retrieval condition (from the primary key of the table), will be displayed. It seems that the process is also very simple. If you do it in DOTNET, it is too simple, but the VC operates, it is a bit trouble. Take the data from the list to take the primary key column of the currently selected row, and the DOTNET is more than the DOTNET, you know where VC is troublesome. (But I still prefer VC, 嘿) a DataGrid, if I know the column name, I have passed the line number of the current line (CurrentrowIndex) If your background is a DATATABLE binding, you can get this value directly: MyTable.Rows [currnetrowindex] ["pkey1"]. TOSTRING (); Of course, if you are not directly bound to a DataTable, it is a result of a result, then it may be slightly cumbersome. This situation is not discussed here. Let's take a look at the VC, (if there is a better way, please inform me, thank you) int CDagview :: getColumnIndex (ClistCtrl & ref, lpctstr strcol) {CHEADERCTRL * PHEADER = ref.getHeaderCtrl (); INT ncount = PHEADER-> GETITEMCOUNT (); tchar lpbuffer [256]; bool ffound = false; hditem hdi; hdi.mask = hdi_text; hdi.psztext = lpbuffer; hdi.cchtextmax = 256; int index = -1; for INT i = 0;! FFound && (i
In fact, this program is relatively simple, debugging under Windows 2K! Write here, deficiency, forgive me! Sometimes, good techniques, if there is a lack of effective examples, it will become unknown. Like the list, the tree control parameters are many, although using VC , although they can play their strongest feature, there is a part of the parameters, only a brief introduction in MSDN, there is no detailed example description (I guess: Microsoft is definitely More detailed examples and documents, or the engine, not an external announcement) This will explore the efforts of the majority of programmers, knowledge sharing, and make progress together. Sambios wrote in 2004-9-2