Vc6.0 basic knowledge usage Summary

Source: Internet
Author: User

During this time, I used MFC as a media player project and found a lot of materials. The technologies used mainly include ado, multithreading, DirectShow, and MFC related controls, including ActiveX Controls (Windows Media Player and active movie3) and so on. For more information, see Windows Media Player Development documentation and msdn.

First, let's talk about what we 've learned over this period of time. In the past month, through the Media Player project, I am familiar with the use of ADO to operate access databases and SQL statements (although the statements used are relatively simple, there is no non-increment, delete, modify, and query) use of 17 classes corresponding to Window Media Player, clistbox in the list box of MFC, ctreectrl in the tree control, and clistctrl in multiple threads. Next, sort out the information you have collected (mainly through csdn, blog Park, and Baidu Library ).

1. VC obtains the status of a menu and checks the status.
When I was doing notepad, I encountered the following problem: how to obtain whether or not the menu item is currently selected (checked). If it is selected, click the menu and deselect it. If it is not selected, click the menu and select it. The specific implementation code is as follows:
Void cmynotepaddlg: onstatus () // Command Message ing function of the menu
{
// Todo: add your command handler code here
If (afxgetmainwnd ()-> getmenu ()-> getmenustate (id_status, mf_checked ))
{
Afxgetmainwnd ()-> getmenu ()-> checkmenuitem (id_status, mf_unchecked );
}
Else
{
Afxgetmainwnd ()-> getmenu ()-> checkmenuitem (id_status, mf_checked );
}
}

2. Hide a part of the window in VC, such as querying the Left and Right windows of the database and displaying the playlist.
The Code is as follows:
Void cmymediaplayerdlg: onbuttonhide ()
{
// Todo: add your control notification handler code here
Cstring STR;
If (getdlgitemtext (idc_button_hide, STR), STR = "<")
{
Setdlgitemtext (idc_button_hide, "> ");
}
Else
{
Setdlgitemtext (idc_button_hide, "<");
}
Static crect rectlarge;
Static crect rectsmall;
 
If (rectlarge. isrectnull ())
{
Crect rectseparator;
Getwindowrect (& rectlarge );
Getdlgitem (idc_separator)-> getwindowrect (& rectseparator );

Rectsmall. Left = rectlarge. Left;
Rectsmall. Top = rectlarge. Top;
Rectsmall. Right = rectseparator. Right;
Rectsmall. Bottom = rectlarge. bottom;
}
If (STR = "<")
{
Setwindowpos (null, 0, 0, rectsmall. Width (), rectsmall. Height (), swp_nomove | swp_nozorder );
}
Else
{
Setwindowpos (null, 0, 0, rectlarge. Width (), rectlarge. Height (), swp_nomove | swp_nozorder );
}
}

3. How do I add a menu to the specified dialog box?
Violation 1: Static Loading menu, that is, editing resources instead of code.

Go to the resources in the dialog box. In the resource idd_mymediaplayer_dialog in the dialog box to be added, select the resources in the dialog box, right-click and choose Properties, in the menu on the General tab, select the menu to be added, such as idr_mainframe_menu. In this way, the menu can be displayed when the class Object of the dialog box is created.
In this way, the menu is associated with the dialog box.

 

Method 2: load the menu directly through code

In the oninitdialog () function of the dialog box class, you can add such a few lines of code.

// Load the menu
Cmenu menu;
Menu. loadmenu (idr_menu1 );
Setmenu (& menu );

4. obtain various pointers in MFC
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. 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, so that you can operate and use
Function.
Directory:

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) obtain the pointer of the View class from the document class

1) Get the doc pointer in the view

Cyousdidoc * pdoc = getdocument (); one view can only 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 in the view.

Cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd;

4) obtain the view (created) pointer
Cmainframe * pmain = (cmaimframe *) afxgetapp ()-> m_pmainwnd;
Cyouview * pview = (cyouview *) pmain-> getactiveview ();

5) Get the pointer to the current document

Cdocument * pcurrentdoc = (cframewnd *) m_pmainwnd-> getactivedocument ();

6) Get 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 you add the toolbar and status bar variables to the framework, you can do the same.
(Cmainframe *) getparent ()-> m_wndtoolbar;
(Cmainframe *) getparent ()-> m_wndstatusbar;

8) obtain the menu pointer in mainframe.

Cmenu * pmenu = m_pmainwnd-> getmenu ();

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
The purpose of getting a View class pointer from a document is generally to control the positioning of multiple views in the same document. My experience is particularly true when the ceditview produces multiple view classes, this function is very required.
The cdocument class provides two functions for positioning the View class:
Getfirstviewposition () and getnextview ()
Virtual position getfirstviewposition () const;
Virtual cview * getnextview (Position & rposition) const;

5. Use of the file operation file opening dialog box class cfiledialoag and cfile of MFC

(1) create an open file dialog box:
Cfiledialog DLG (true, // true indicates the create open file dialog box, and false indicates the save file dialog box.
". Txt", // The default file type to open
Null, // default open file name
Ofn_hidereadonly | ofn_overwriteprompt, // open the read-only file
"Text file (*. txt) | *. txt | all files (*. *) | *. * |"); // all types of files that can be opened
(2). Click the OK key on the open file dialog box.
If (DLG. domodal () = idok)
{
Cstring m_filepath = DLG. getpathname (); //// // retrieve the file path
Cstring m_path;
M_path = m_filepath; // put the file path into m_path
Updatedata (false );
}
(3) open the file:
Cfile file;
File. Open (m_path, cfile: moderead );

(4). Read files row by row: cstdiofile file; // class of files that can be read row by row
Cstring strline;
While (file. readstring (strline) ///// put each row into the strline string
{
Afxmessgebox (strline );
}
(5) determine the read words:
Strline = "1 | 2 | 3 |"; // string to be judged
Int strindex1 = strline. Find ('|'); // search for "|" in the string"
Cstring A [11];
If (-1! = Strindex1) // If "|" is found,-1 is not returned.
{
Int I = 0;
While (-1! = Strindex1 )//
{
Strindex1 = strline. Find ('| ');
A [I] = strline. Left (strindex1 );
Strline = strline. Right (strline. getlength ()-strIndex1-1 );
I ++;
If (I> 10) // exit the loop
Break;
}
}
}

6. VC obtains the current local time
(1). Custom function: void mygetdatetime (char * buff)
{
Word syear, smonth, SDAY, Shour, sminute, sSecond;
Systemtime curtime; // defines the systemtime Class Object
Getlocaltime (& curtime); // get the local time
Syear = curtime. wyear; // obtain the year
SM. wmonth; // obtain the month
SDAY = curtime. wday;
Shour = curtime. whour;
Sminute = curtime. wminute;
Ssec. wsecond;
Sprintf (buff, "% d, % d", syear, smonth, SDAY, Shour, sminute, sSecond ); /// put year, month, day, hour, minute, and second into the buff
}
(2). Call time:
Char B [20];
Mygetdatetime (B); // The array name is actually a pointer

7. open multiple files in the MFC file dialog box
Try
{
Cstring STR ("", 10000); // This setting is used to store the value of string space 10000 in the file name and path.
Cfiledialog openfile (true, null, null,
Ofn_hidereadonly | ofn_overwriteprompt | ofn_allowmultiselect, "All files (*. *) | *. * |", afxgetmainwnd ());
Openfile. m_ofn.lpstrfile = Str. getbuffer (10000 );
Openfile. m_ofn.nmaxfile = 10000;
Openfile. m_ofn.lstructsize = sizeof (openfile. m_ofn );
Cstring pathname ("", max_path );
Cfilestatus status;
Openfile. domodal ();

Position MPOs = openfile. getstartposition ();
While (MPOs! = NULL ){
Pathname = openfile. getnextpathname (MPOs );
Cfile: getstatus (pathname, status );
This-> m_listresult.insertitem (count ++, pathname); // write the file name including the path to a list control.
}
}
Catch (cexception * E)
{
Afxmessagebox (_ T ("An error occurred while opening the file! "));
E-> Delete ();
}

If (filedialog. domodal () = idok)
{
Cstring filename; // = filedialog. getpathname ();
// M_mediaplayer.put_url (filename );

Int m_countitem = m_listctrl.getitemcount ();
Position Pos = filedialog. getstartposition ();
For (; pos! = NULL ;)
{
// Cstring m_filename = filedialog. getfilename ();
Filename = filedialog. getnextpathname (POS );
// Cstring STR = "C: myprojectmy.exe ";
Int NPOs = filename. reversefind ('');
Cstring m_filename = filename. mid (NPOs + 1 );
}

8. A valid method for converting a cstring to a char array in vs2005 or later versions
Use the API function widechartomultibyte
The routine is as follows:
Cstring filename;
Char strfilename [200] = {0 };

Widechartomultibyte (cp_acp, 0, (maid) filename,-1, strfilename, 200, null, null );

9. Traverse folders and Their subfolders
Use the FileFind Class findfile/filenextfile () and other member functions provided by MFC.
The Code is as follows:
Void scanfile (cstring DIR)
{
Cfilefind finder;
Cstring add = l "\*";
Cstring dirspec = dir + Add; // complete the directory of the folder to be traversed
Bool bworking = finder. findfile (dirspec );

While (bworking)
{
Bworking = finder. findnextfile ();
If (! Finder. isdots () // The node is not scanned.
{
If (finder. isdirectory () // scan the folder
{
Cstring strdirectory = finder. getfilepath ();
Scanfile (strdirectory); // recursively call scanfile ()
}
Else // The file is scanned
{
// Cstring strfile = finder. getfilepath (); obtain the full file path.
// Perform a series of custom operations
}
}
}
Finder. Close ();
}

 

 

At last, I would like to thank the elders for their articles and code, which has taught me a lot.

I would like to recommend several it websites, which I often visit.

Csdn: I don't need to talk about it. The largest it Chinese community in the legend, the resources and articles are both good, that is, the code quality may be poor.

Programmer Joint Development Network: http://www.pudn.com, the largest source code download Chinese site, the only difficulty is to upload source code or information to become a member, code quality in general is better than csdn.

Such as Peng Network: http://www.rupeng.com, although it is for college students, but some things is better, pay attention to the basis.

Http://www.codeforge.com: an English open source code sharing site, I heard that the download code do not point, registration can be.

Http://www.codeproject.com/:a website in English. If the website contains a code and answers the question, the personal website is still correct.

Http://www.javaeye.com/:a javalearning website.

Http://bbs.chinaunix.net: chinaunix Learning Community
Http://my.oschina.net: Open Source Chinese community

I hope the above websites will help you.

 

 

 

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.