VC operations on the SQL Server Master/Slave table

Source: Internet
Author: User
Tags dotnet

Article Title: VC operations on the SQL Server Master/Slave table
Original Author: Yuan huaishan
Original location: vckbase
Released by: loose_went
Release type: Reprinted
Release date: 2004-10-16
Browse today: 41
Overview: 9955
Download Code: Skin ++ skin replacement Kit
· Supports multiple development languages
· The interface and business logic are completely separated to save development and maintenance time
· Complete skin replacement, including standard controls and general dialogs
· Skin supports the winblinds format, with thousands of skins available for free download
· Provides convenient design tools to design your own skin
 
 
 

In addition, I used VC as a small program, which is in the VC + SQL SERVER mode. The program content is relatively simple and mainly set up the following content:

How to split windows;
How to communicate between multiple views;
Operations and event handling in the list view;
Tree control operations and event processing, uses recursion to dynamically generate directories based on the database;
The following is a brief description of the program details;
I. How to separate windows

The split window is applicable to both SDI applications and MDI applications. It is usually represented by csplitterwnd. For Windows, csplitterwnd is a real window, which occupies the customer area of the frame window completely while the view occupies the pane area of the split window. Dynamic Splitting and static splitting the Dynamic Splitting allows you to split the window at any time. You can select a menu or drag the split box in the scroll bar to split the window. The dynamic split window uses a View class. Static Splitting: When a window is created for the first time, the pane is split and cannot be changed. You can move the border of the pane, but you cannot merge or divide the window. Multiple View classes can be used in the static split window, and these view classes can be configured during creation. In the static split window, each pane has its own scroll bar. Dynamic Splitting is relatively simple and not practical. Let's look at static splitting.

Bool cmainframe: oncreateclient (lpcreatestruct lpcs, ccreatecontext * pcontext)
{
Crect rect;
Getclientrect (& rect );
M_wndsplitter1.createstatic (this, 1, 2 );
M_wndsplitter1.createview (150,150, runtime_class (cclasstreeview), csize (), 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, 0, runtime_class (cdagview), csize (0, 0), pcontext );
M_wndsplitter2.setrowinfo (0, rect. Height ()/2, 0 );

Return true;

// The oncreateclient () of the base class cannot be called during overload ()
}

The above is the method used by the demo of this program. For more information about splitting, see msdn.

2. Communication between multiple views

The demo of this program uses three view classes. One is generated by winzard, and the other is added by me. The communication between multiple views mainly gets the pointer of the View class you want to access. How can we get it? There are two methods: 1. view classes (if multiple views share one document class) 2. View them through the main framework. Since my program is a document class and other view classes are independent, the second method is used.
The following code is an example of the cdagdetialview class pointer obtained by the cursor. The other classes are the same.

Cmainframe * pframe = static_cast (afxgetmainwnd (); // the winner's frame
Cdagdetialview * pdagdetialview =
Static_cast (pframe-> m_wndsplitter2.getpane (1, 0 ));

I think it is better to use the above form. Although the forced conversion Syntax of C is also good, it is better to use the C ++ syntax.
After obtaining the view pointer, you can do anything that you want to do
By the way, communication details are related to efficiency and must be properly handled. The demo program above is not considered too much. I think this is more suitable for you to understand the meaning. If the program modules are scattered, the program looks a lot simpler, but it is not easy to see the program process. Sorry.

Iii. List View operations and event handling

To be honest, there are a lot of attribute parameters in the list, and there are a lot of standard and extended style parameters. Look at the English documents and find a feature you need, the taste should not be mentioned. Fortunately, I am very patient :) I am so skilled. I remember more than half of them. What are you afraid. The list control displays data in the lvs_report style. It looks nice, just like a DataGrid. To set the window style, use the setwindowlong API function. This function is common. There are also a lot of parameters. The click event in the processing list is processing the message nm_click, and the event when processing the selected change is processing lvn_itemchanged. Different messages may have different parameters and need to be treated differently.

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)
... // Event triggered when a list item is selected
Presult = 0;
}

Process events such as click and double-click in the same way.

4. Tree control operations and event processing, using recursion to dynamically generate directories based on the database

Tree controls, like list controls, play an important role in VC data development. It is often used to display classification and classification information. As a matter of fact, when talking about trees, there will be a lot of trees in my mind, what binary trees, search trees, and Harman trees. This reminds me of the commonly used algorithms for Traversing trees-recursion. While using Stacks is more efficient, recursion is often used to simplify program design.
See the following program snippet:

PRS-> movefirst ();
_ Variant_t var;
Cstring strtablename, strtablecode;
Htreeitem hchilditem;
While (! PRS-> adoeof)
{
Var = PRS-> getcollect ("menucode ");
If (var. VT! = Vt_null)
Strtablecode = (lpcstr) _ bstr_t (VAR );

// This is the recursive exit of the function.
If (! Strtablecode. Find (strparent, 0 )&&
Strtablecode. getlength () = strparent. getlength () + 2)
{
Hchilditem = reftree. insertitem (strtablename, htreenode, tvi_last );
Inittree (reftree, hchilditem, strtablecode );
}
PRS-> movenext ();
}

Make full use of the features of the tree structure during recursion. Then we can combine them with recursion. Here is the combination of theory and practice. Different Development Languages provide different operation interfaces, such as Delphi and. DOTNET. In. net, I implemented a similar tree. The algorithm thought is the same, but the specific operations are different, because DOTNET provides different interfaces.
Event Processing for tree controls

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;
}

5. Display Master/Slave tables

Since I do not know how many fields are in the database, the field names are unknown. All are dynamically generated. Therefore, you must first retrieve the primary key. After the primary key is retrieved, the data corresponding to the primary key column of the row selected in the list is obtained. Then, use the values of these primary key columns as the search condition to retrieve the data from the table (the primary key of the table must be greater than the primary table) and finally display the results. It seems that the process is quite simple. If you use the DataGrid in DOTNET, it is too simple, right? But VC operations are a little troublesome. Take the data from the primary key column of the selected row from the list and compare it with DOTNET to know where VC is in trouble. (But I still like VC, hey ). A DataGrid. If I know the column name, the cursor retrieves the row number (currentrowindex)
If you bind a datatable directly in the background, you can directly obtain this value:

Mytable. Rows [currnetrowindex] ["pkey1"]. tostring ();

Of course, if you do not directly bind a datatable, but a result after consideration, it may be a little cumbersome. We will not discuss this situation here. Next, let's take a look at how it works in VC. (If you have a better method, please let me know. Thank you first)

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 <ncount); I ++)
{
Pheader-> getitem (I, & HDI );

If (strcmp (HDI. psztext, strcol) = 0)
{
Index = I;
Ffound = true;
}
}
Return Index;
}
Isubitem = getcolumnindex (reflistctrl );
Cstring strvalue = reflistctrl. getitemtext (icurrent, isubitem );

First, retrieve the column number, then obtain the row number, and finally obtain the value ...... well, write so much. Let's look at the code for details.

Vi. Conclusion

In fact, this program is relatively simple and can be debugged under Windows 2 k! Write it here. Sorry for the shortcomings! Sometimes, even the best technology, without effective examples, will become obscure. For example, there are many list and tree control parameters in this article. Although VC ++ can provide their most powerful functions, some parameters are briefly described in msdn, there are no detailed examples (I guess: Microsoft certainly has more detailed examples and documents, or the engine, which will not be published externally). This relies on the efforts of programmers to explore and share knowledge, everyone makes common progress.
 

Related Article

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.