
- •The ministry of education and science of ukraine kharkiv national university of radio electronics
- •1. The Basics of Microsoft Foundation Classes
- •Mfc general information
- •A Framework of mfc-program
- •Creating the Application Class
- •Creating the Frame-Window Class
- •Declaring a Message Map and instantiation of application object global instance
- •Defining a Message Map
- •Messages and their processing in mfc-programs
- •Writing Message Map Functions
- •Message boxes and menus in mfc-programs
- •2. Dialog windows
- •2.1 Modal and modeless dialog windows
- •2.2 The control elements of dialog window
- •CListBox::AddString (lpctstr lpszItem ); // Call this member function to add a string (lpszItem) to a list box;
- •3. Additional control elements in mfc-programs. Working with icons, cursors, raster images
- •3.1 Additional control elements
- •Radio buttons
- •Afx_msg void cWnd::OnVScroll( uint nSbCode, uint nPos, cScrollBar* pScrollBar ); afx_msg void cWnd::OnHScroll( uint nSbCode, uint nPos, cScrollBar* pScrollBar );
- •Afx_msg void OnVScroll( uint nSbCode, uint nPos, cScrollBar* pScrollBar );
- •Working with icons, cursors, raster images
- •The icons and cursor registration
- •Icon and cursor loading
- •4. The elements of text processing in mfc
- •The redrawing problem decision
- •5. The Elements of working with graphics
- •5.1 The graphics functions.
- •Working with brushes
- •5.2 The mapping modes and output regions
- •6. Common control elements
- •Windows Common Controls
- •6.2 The toolbar using
- •On resizing, the message wm_size is sent and the standard handler OnSize() is called.
- •The working with Spins
- •The working with slider
- •To set the range (minimum and maximum positions) for the slider in a slider control use the following function:
- •The working with progress bar
- •The tree control using in mfc programs
- •Adding elements to the tree
- •The status bars usage
- •Bool cStatusBarCtrl::SetParts( int nParts, int* pWidths );
- •Tab controls using in mfc-programs
- •Int cTabCtrl::GetCurSel(); To Selects a tab in a tab control use SetCurSel() function:
- •Int cTabCtrl::SetCurSel( int nItem );
- •The property sheets and wizards
- •7. Thread multitasking and it’s implementation in mfc
- •7.1 The basic features of multitasking
- •7.2 The Synchronization
- •7.3 The working with semaphore
- •7. 4 The working with event object
- •8. The concept of Document view
- •8.1 Introduction to document conception
- •The control of documents storing
- •8.2 The dynamic creation of objects
- •The application framework creation
- •The main window and application classes creation
- •Listing 8.1 The example of main window class in Document / View concept
- •Listing 8.2 The example of document class in Document / View concept
- •8.3 The document framework creation
- •8.4 The initiation of application
- •8.5 The standard id’s, used in Document / View concept
- •9. The special types of menu and their implementation in mfc
- •9.1 The description of special menu styles
- •The mechanism to make changes in menus
- •9.2 The dynamic and floating menus implementation
- •CMenu::CreatePopupMenu
- •The example programs to work with dynamic menus
- •10. The system of help
- •10.1 The basic information on help structures
- •The call of help
- •The file of help
- •The Help file creating
- •The example of rtf file
- •10.2 The Help system including to the mfc-program
- •Parameters:
- •Return Values: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.
- •10.3 The handlers of help messages
- •The processing of help calls
- •Wm_commandhelp message processing
- •10.4 Wm_contextmenu message processing
- •11. Manipulating Device-Independent Bitmaps
- •11.1 The types of bitmap
- •11.2 The structures included to bitmap
- •Introducing the cDib Class
- •11.3 Programming the cDib Class
- •Loading a dib into Memory
- •Other cDib Member Functions
- •Creating ShowDib program
- •Modifying ShowDib's Resources
- •Adding Code to ShowDib
- •Examining the OnFileOpen() Function
- •Examining the OnDraw() Function
- •12. The elements of Database Programming
- •12.1 Understanding Database Concepts
- •Accessing a Database
- •12.2 Mfc odbc Classes
- •Registering the Database
- •Creating the Basic Employee Application
- •Creating the Database Display
- •Adding and Deleting Records
- •12.4 Sorting and Filtering
- •12.5 Odbc versus dao
- •13. Remote Automation
- •13.1 The introduction to Remote Automation
- •13.2 The Remote Automation Connection Manager and user components
- •13.3 Automation
- •Automation Clients
- •13.4 ActiveX
- •ActiveX Document Servers
- •ActiveX Document Containers
- •ActiveX Document Views
- •13.5 ActiveX Documents
- •ActiveX Controls
- •Interaction Between Controls with Windows and ActiveX Control Containers
- •13.6 Optimization of ActiveX Controls
- •13.7 Automation Servers
- •13.8 Connection Points
- •14. Microsoft DirectX and the main items of its using
- •14.2 The Component Object Model
- •IUnknown Interface
- •DirectX com Interfaces
- •DirectDraw Architecture
- •Other DirectDraw Features
- •Width and Pitch
- •14.5 Support for 3d Surfaces in DirectX
- •14.6 Direct3d Integration with DirectDraw
- •Direct3d Device Interface
- •Direct3d Texture Interface
- •The Basics of DirectDraw
- •Step 6: Writing to the Surface.The first half of the wm_timer message in ddex1 is devoted to writing to the back buffer, as shown in the following example:
- •Loading Bitmaps on the Back Buffer
- •Step 1: Creating the Palette. The ddex2 sample first loads the palette into a structure by using the following code:
- •Step 4: Flipping the Surfaces. Flipping surfaces in the ddex2 sample is essentially the same process as that in the first example. Blitting from an Off-Screen Surface
- •Step 1: Creating the Off-Screen Surfaces. The following code is added to the doInit function in ddex3 to create the two off-screen buffers:
- •Color Keys and Bitmap Animation
- •Dynamically Modifying Palettes
- •Optimizations and Customizations
- •Blitting with Color Fill
- •Determining the Capabilities of the Display Hardware
- •Storing Bitmaps in Display Memory
- •Triple Buffering
- •15. General information on OpenGl
- •15.1 Common information
- •Primitives and Commands
- •OpenGl Graphic Control
- •Execution Model
- •15.2 Basic OpenGl Operation
- •OpenGl Correctness Tips
- •15.3 OpenGl example program
- •Ph.D. Assosiate prof. Tsimbal Alexander m. System software, summary of lectures.
Adding and Deleting Records
As soon as you can add and delete records from a database table, you have a full-featured program for manipulating a flat (that is, not a relational) database. In this case, the "flat database" is the Employees table of the Department Store relational database. Adding and deleting records in a database table is an easier process than you might believe, thanks to Visual C++'s CRecordView and CRecordSet classes, which provide all of the member functions that you need to accomplish these common database tasks. Follow the steps that come next to include add and delete commands in the Employee application.
1. Choose the ResourceView tab, open the Menu folder, and double-click the IDR_MAINFRAME menu ID.
2. Open the Record menu in the editor, and then double-click the blank menu item at the bottom of the menu. The Menu Item Properties property sheet appears.
3. In the ID edit box, enter ID_RECORD_ADD, and in the Caption box, enter &Add Record. When you press Enter, the menu editor adds the new command to the Record menu.
4. In the next blank menu item, add a delete command with the ID ID_RECORD_DELETE and the caption &Delete Record.
5. In the ResourceView pane, open the Toolbar folder, and then double-click the IDR_MAINFRAME ID. The application's toolbar appears in the resource editor.
6. Click the blank toolbar button to select it, and then use the editor's tools to draw the icon (it's supposed to be blue) on the button.
7. Double-click the new button in the toolbar. The Toolbar Button Properties property sheet appears. Choose ID_RECORD_ADD in the ID box.
8. Create a new toolbar button that displays a red minus sign, giving the button the ID_RECORD_DELETE ID. Move the Add and Delete buttons to a position before the Help (question mark) button.
9. Choose the View, ClassWizard command from the menu bar, or just press Ctrl+W on your keyboard. The MFC Class Wizard property sheet appears. (Choose the Message Maps tab if necessary.)
10. Set the Class Name box to CEmployeeView, click the ID_RECORD_ADD ID in the Object IDs box, and then double-click COMMAND in the Messages box. The Add Member Function dialog box appears.
11. Click the OK button to accept the default name for the new function. The function appears in the Member Functions box.
12. Add a member function for the ID_RECORD_DELETE command in the same way shown in steps 10 and 11. Then click the OK button on the MFC ClassWizard property sheet to finalize your changes. The ClassWizard property sheet closes.
13. Open the EmployeeView.h file and, in the Attributes section of the class's declaration, add the following lines:
protected:
BOOL m_bAdding;
14. Open the EmployeeView.cpp file, and then add the following line to the class's constructor, right after the comment TODO: add construction code here:
m_bAdding = FALSE;
15. Still in EmployeeView.cpp, find the OnRecordAdd() function, and add the code shown in Listing 12.1 below.
Listing 12.1 Code for the OnRecordAdd() Function
m_pSet->AddNew();
m_bAdding = TRUE;
CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);
int result = pCtrl->SetReadOnly(FALSE);
UpdateData(FALSE);
16. Use ClassWizard to override the OnMove() member function in the CRecordView class. Click the Edit Code button to jump to the function in the text editor.
17. Replace the default code in the OnMove() function with the code shown in Listing 12.2.
Listing 12.2 Code for the OnMove() Function
if (m_bAdding)
{ m_bAdding = FALSE;
UpdateData(TRUE);
if (m_pSet->CanUpdate())
m_pSet->Update();
m_pSet->Requery();
UpdateData(FALSE);
CEdit* pCtrl =(CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);
pCtrl->SetReadOnly(TRUE);
return TRUE;
}
else return CRecordView::OnMove(nIDMoveCommand);
18. Add the code shown in Listing 12.3 to the OnRecordDelete() function.
Listing 12.3Code for the OnRecordDelete() Function
m_pSet->Delete();
m_pSet->MoveNext();
if (m_pSet->IsEOF())
m_pSet->MoveLast();
if (m_pSet->IsBOF())
m_pSet->SetFieldNull(NULL);
UpdateData(FALSE);
You've now modified the Employee application so that it can add and delete, as well as update, records. After compiling the application, run it by selecting the Build, Execute command from Developer Studio's menu bar or by pressing Ctrl+F5 on your keyboard. When you do, you see the Employee application's main window, which doesn't look any different than it did in the previous section. Now, however, you can add new records by clicking the Add button on the toolbar (or by selecting the Record, Add Record command in the menu bar) and delete records by clicking the Delete button (or by clicking the Record, Delete Record command).
When you click the Add button, the application displays a blank record. Fill in the fields for the record, and then, when you move to another record, the application automatically updates the database with the new record. To delete a record, just click the Delete button. The current record (the one on the screen) vanishes, each field being filled in with the symbol "<deleted>."
Examining the OnRecordAdd() Function
You now might be wondering how the C++ code that you added to the application works. When you click the Add button or choose the Record, Add command MFC routes program execution to the OnRecordAdd() command handler. In that function, the program first calls the CEmployeeSet object's AddNew() member function, like this:
m_pSet->AddNew();
The AddNew() member function sets up a blank record for the user to fill in. The new blank record doesn't appear on the screen, however, until the view window's UpdateData() function is called.
After creating the new record, the program sets the Boolean variable m_bAdding to TRUE, which indicates to the program that the user is in the process of adding a new record:
m_bAdding = TRUE;
Now, because the user is entering a new record, he or she needs to be able to change the contents of the Employee ID field, which is currently set to read-only. To change the read-only status of the control, the program first gets a pointer to the control and then calls the control's SetReadOnly() member function, like this:
CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);
int result = pCtrl->SetReadOnly(FALSE);
The SetReadOnly() member function changes the read-only style of a control. When the function's single argument is FALSE, the control is enabled for normal editing. When the function's argument is TRUE, the control is set to its read-only state, which prevents the user from changing the value displayed in the control.
Finally, as I mentioned previously, the program must call the view window's UpdateData() member function to display the new blank record, like this:
UpdateData(FALSE);
When the UpdateData() function's argument is FALSE, data is transferred from the record set to the control. When the argument is TRUE, data is transferred from the control to the record set.
Examining the OnMove() Function
Now that the user has a blank record on the screen, he or she can start filling in the edit controls with the necessary data. To actually add the new record to the database, the user must move to a new record, an action that forces a call to the view window's OnMove() member function. Normally, OnMove() does nothing more than display the next record. However, because you overrode the function and added your own code, you've enabled the program to save the new record.
When OnMove() is called, the first thing that the program does is check the Boolean variable m_bAdding to see whether the user is in the process of adding a new record:
if (m_bAdding)
If m_bAdding is FALSE, the body of the if statement is skipped, and the else clause is executed. In the else clause, the program calls the base class's (CRecordView) version of OnMove(), which performs the default behavior for moving to the next record. If m_bAdding is TRUE, the body of the if statement is executed. There, the program first resets the m_bAdding flag:
m_bAdding = FALSE;
Then the program calls UpdateData() to transfer data out of the view window's controls:
UpdateData(TRUE);
A call to the record set's CanUpdate() method determines whether it's okay to update the data source, after which a call to the record set's Update() member function adds the new record to the data source:
if(m_pSet->CanUpdate())
m_pSet->Update();
To rebuild the record set, the program must call the record set's Requery() member function, like this:
m_pSet->Requery();
A call to the view window's UpdateData() member function transfers new data to the window's controls:
UpdateData(FALSE);
Finally, the program sets the Employee ID field back to read-only so that the user can no longer change its contents:
CEdit* pCtrl = (CEdit*)GetDlgItem(IDC_EMPLOYEE_ID);
pCtrl->SetReadOnly(TRUE);
Examining the OnRecordDelete() Function
When the user clicks the Delete button (or chooses the Delete Record command from the Record menu), the OnRecordDelete() function gets called. In that function, deleting the record is just a matter of calling the record set's Delete() function:
m_pSet->Delete();
As soon as the record is deleted, however, the program should display another record in its stead. That's where the call to the record set's MoveNext() function comes in:
m_pSet->MoveNext();
The MoveNext() function moves the record set forward to the next record. A problem might arise, though, when the deleted record was in the last position or when the deleted record was the only record in the record set. A call to the record set's IsEOF() function will determine whether the record set was at the end. If the call to IsEOF() returns TRUE, the record set needs to be repositioned on the last record, which it is currently beyond. The record set's MoveLast() function takes care of this task. The code looks like this:
if (m_pSet->IsEOF())
m_pSet->MoveLast();
When the last record has been deleted from the record set, the record pointer will be at the beginning of the set. The program can test for this situation by calling the record set's IsBOF() function. If this function returns TRUE, the program sets the current record's fields to NULL, like this:
m_pSet->SetFieldNull(NULL);
Finally, the last task is to update the view window's display:
UpdateData(FALSE);