
- •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 elements to the tree
The elements addition to the tree is provided by different variants of InserItem() function:
HTREEITEM CTreeCtrl::InsertItem( LPTV_INSERTSTRUCT lpInsertStruct );
HTREEITEM CTreeCtrl::InsertItem(UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam, HTREEITEM hParent, HTREEITEM hInsertAfter );
HTREEITEM CTreeCtrl::InsertItem( LPCTSTR lpszItem, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST );
HTREEITEM CTreeCtrl::InsertItem( LPCTSTR lpszItem, int nImage, int nSelectedImage, HTREEITEM hParent = TVI_ROOT, HTREEITEM hInsertAfter = TVI_LAST);
Here are the following parameters:
lpInsertStruct A pointer to a TV_INSERTSTRUCT that specifies the attributes of the tree view item to be inserted.
nMask Integer specifying which attributes to set.
lpszItem Address of a string containing the item's text.
nImage Index of the item's image in the tree view control's image list.
nSelectedImage Index of the item's selected image in the tree view control's image list.
nState Specifies values for the item's states.
nStateMask Specifies which states are to be set.
lParam A 32-bit application-specific value associated with the item.
hParent Handle of the inserted item's parent.
hInsertAfter Handle of the item after which the new item is to be inserted.
The TV_INSERTSTRUCT structure is defined as follows:
typedef struct _TV_INSERTSTRUCT {
HTREEITEM hParent;
HTREEITEM hInsertAfter;
TV_ITEM item;
} TV_INSERTSTRUCT;
hParent Handle of the parent item. If this parameter is the TVI_ROOT value or NULL, the item is inserted at the root of the tree view control.
hInsertAfter Handle of the item after which the new item is to be inserted or one of the following values:
TVI_FIRST Inserts the item at the beginning of the list.
TVI_LAST Inserts the item at the end of the list.
TVI_SORT Inserts the item into the list in alphabetical order.
item A TV_ITEM structure, which contains information about the item to be added to the tree view control.
In it’s turn, TV_ITEM structure has such view:
The TV_ITEM structure specifies or receives attributes of a tree-view item.
typedef struct _TV_ITEM { tvi
UINT mask;
HTREEITEM hItem;
UINT state;
UINT stateMask;
LPSTR pszText;
int cchTextMax;
int iImage;
int iSelectedImage;
int cChildren;
LPARAM lParam;
} TV_ITEM, FAR *LPTV_ITEM;
The elements of structure:
mask - Array of flags that indicate which of the other structure members contain valid data. When this structure is used with the TVM_GETITEM message, the mask member indicates the item attributes to retrieve. This member can be a combination of the following values:
Value |
Meaning |
TVIF_CHILDREN |
The cChildren member is valid. |
TVIF_HANDLE |
The hItem member is valid. |
TVIF_IMAGE |
The iImage member is valid. |
TVIF_PARAM |
The lParam member is valid. |
TVIF_SELECTEDIMAGE |
The iSelectedImage member is valid. |
TVIF_STATE |
The state and stateMask members are valid. |
TVIF_TEXT |
The pszText and cchTextMax members are valid. |
hItem - Identifies the item to which this structure refers.
state - A set of bit flags and image list indexes that indicate the item’s state. For a list of item state bit flags, see Tree-View Item States. For information about the state image and overlay image indexes, see Tree-View Image Lists.
When setting the state of an item, the stateMask member indicates the bits of the state member that are valid. When retrieving the state of an item, the state member returns the current state for the bits indicated in the stateMask member.
stateMask - Specifies the bits of the state member that are valid.
pszText - Pointer to a null-terminated string that contains the item text if the structure specifies item attributes. If this member is the LPSTR_TEXTCALLBACK value, the parent window is responsible for storing the name. In this case, the tree-view control sends the parent window a TVN_GETDISPINFO notification message when it needs the item text for displaying, sorting, or editing, and a TVN_SETDISPINFO notification message when the item text changes.
If the structure is receiving item attributes, this member is the pointer to the buffer that receives the item text.
cchTextMax - Size of the buffer pointed to by the pszText member if the structure is receiving item attributes. If the structure specifies item attributes, this member is ignored.
iImage - Index in the tree-view control’s image list of the icon image to use when the item is in the non-selected state.
If this member is the I_IMAGECALLBACK value, the parent window is responsible for storing the index. In this case, the tree-view control sends the parent a TVN_GETDISPINFO notification message to get the index when it needs to display the image.
iSelectedImage - Index in the tree-view control’s image list of the icon image to use when the item is in the selected state. If this member is the I_IMAGECALLBACK value, the parent window is responsible for storing the index. In this case, the tree-view control sends the parent a TVN_GETDISPINFO notification message to get the index when it needs to display the image.
cChildren - Flag that indicates whether the item has associated child items. This member is one of the following values:
Value |
Meaning |
zero |
The item has no child items. |
1 |
The item has one or more child items. |
I_CHILDRENCALLBACK |
The parent window keeps track of whether the item has child items. In this case, when the tree-view control needs to display the item, the control sends the parent a TVN_GETDISPINFO notification message to determine whether the item has child items. |
If the tree-view control has the TVS_HASBUTTONS style, it uses this member to determine whether to display the button indicating the presence of child items. You can use this member to force the control to display the button even though the item does not have any child items inserted. This allows you to display the button while minimizing the control’s memory usage by inserting child items only when the item is visible or expanded.
lParam - A 32-bit value to associate with the item.
The expansion and collapsing on tree nodes
To expand or collapse the tree MFC propose the expand function implementation:
BOOL CTreeCtrl::Expand( HTREEITEM hItem, UINT nCode );
Here are the parameters:
hItem Handle of the tree item being expanded.
nCode A flag indicating the type of action to be taken. This flag can have one of the following values:
TVE_COLLAPSE Collapses the list.
TVE_COLLAPSERESET Collapses the list and removes the child items.
TVE_EXPAND Expands the list.
TVE_TOGGLE Collapses the list if it is currently expanded or expands it if it is currently collapsed.
All the function activity depends on element, defined in hItem parameter.
Some function to work with trees
To retrieve the currently selected item of the tree view control use such function:
HTREEITEM CTreeCtrl::GetSelectedItem( );
To select the element we can use special Select() function:
BOOL CTreeCtrl::Select( HTREEITEM hItem, UINT nCode );
We the such parameters:
hItem Handle of a tree item.
nCode The type of action to take. This parameter can be one of the following values: TVGN_CARET Sets the selection to the given item.
TVGN_DROPHILITE Redraws the given item in the style used to indicate the target of a drag-and-drop operation.
TVGN_FIRSTVISIBLE Scrolls the tree view vertically so that the given item is the first visible item.
If nCode contains the value TVGN_CARET, the parent window receives the TVN_SELCHANGING and TVN_SELCHANGED notification messages. In addition, if the specified item is the child of a collapsed parent item, the parent's list of child items is expanded to reveal the specified item. In this case, the parent window receives the TVN_ITEMEXPANDING and TVN_ITEMEXPANDED notification messages.
To receive the information on element, use GetItem() function:
BOOL CTreeCtrl::GetItem( TV_ITEM* pItem );
Here, pItem A pointer to a TV_ITEM structure.
In otherwise case – to set the information on element user SetItem() function:
BOOL CTreeCtrl::SetItem( TV_ITEM* pItem );
BOOL CTreeCtrl::SetItem( HTREEITEM hItem, UINT nMask, LPCTSTR lpszItem, int nImage, int nSelectedImage, UINT nState, UINT nStateMask, LPARAM lParam );
Parameters:
pItem A pointer to a TV_ITEM structure that contains the new item attributes. hItem Handle of the item whose attributes are to be set.
nMask Integer specifying which attributes to set.
lpszItem Address of a string containing the item's text.
nImage Index of the item's image in the tree view control's image list.
nSelectedImage Index of the item's selected image in the tree view control's image list.
nState Specifies values for the item's states.
nStateMask Specifies which states are to be set.
lParam A 32-bit application-specific value associated with the item.
In the TV_ITEM structure, the hItem member identifies the item, and the mask member specifies which attributes to set.
If the mask member or the nMask parameter specifies the TVIF_TEXT value, the pszText member or the lpszItem is the address of a null-terminated string and the cchTextMax member is ignored. If mask (or nMask) specifies the TVIF_STATE value, the stateMask member or the nStateMask parameter specifies which item states to change and the state member or nState parameter contains the values for those states.
To delete the element of the tree, you can use DeleteItem():
BOOL CTreeCtrl::DeleteItem( HTREEITEM hItem );
hItem Handle of the tree item to be deleted. If hitem has the TVI_ROOT value, all items are deleted from the tree view control.
The messages of Tree view controls
On any action execution, there will be send the WM_NOTIFY message. The identity codes for that messages have the following values:
TVN_BEGINDRAG - notifies a tree-view control’s parent window that a drag-and-drop operation involving the left mouse button is being initiated.
TVN_BEGINRDRAG - notifies a tree-view control’s parent window about the initiation of a drag-and-drop operation involving the right mouse button.
TVN_DELETEITEM - notifies a tree-view control’s parent window that an item is being deleted.
TVN_ITEMEXPANDED - notifies a tree-view control’s parent window that a parent item’s list of child items has expanded or collapsed
TVN_ITEMEXPANDING - notifies a tree-view control’s parent window that a parent item’s list of child items is about to expand or collapse.
TVN_SELCHANGED - notifies a tree-view control’s parent window that the selection has changed from one item to another.
TVN_SELCHANGING - notifies a tree-view control’s parent window that the selection is about to change from one item to another.
Here, we shall consider the basic elements of program to implement the properties of tree view control. So, that items:
In the resources of project create the items to implement: “Expand” – to expand the elements of the tree (at some point of expanding); ”Expand All” – to expand all the nodes of the tree; “Collapse” – to close all the elements of the tree;
Include such elements to main window class:
Class CMainWin : public CFrameWnd
{CTreeCtrl m_Tree; // the object of window to view the tree
void InitTree(); // the function to initiate the window of tree viewing
public:
CMainWin();
void OnExpand();
void OnExpandAll();
void OnCollapse();
void OnExit();
void OnPaint();
void OnTreeChange(NMHDR *hdr,
LRESULT *NotUsed);
DECLARE_MESSAGE_MAP()
};
Define the global variables:
HTREEITEM hTreeCtrl[NUM];
HTREEITEM hTreeCurrent;
Make some changes in the main window constructor:
CMainWin::CMainWin()
{Create(NULL,"Window with toolbars", WS_OVERLAPPEDWINDOW, rectDefault, NULL, MAKEINTRESOURCE(IDR_MENU1));
InitCommonControls();
RECT r;
r.left=r.top=30;
r.right=120;r.bottom=100;
m_Tree.Create(WS_VISIBLE | WS_CHILD | WS_BORDER | TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT,
r,this,ID_TREE);
InitTree();
}
Define the function to initiate the function of tree viewing:
void CMainWin::InitTree()
{TV_INSERTSTRUCT tvs;
TV_ITEM tvi;
tvs.hInsertAfter = TVI_LAST;
tvi.mask=TVIF_TEXT;
tvi.pszText="One";
tvs.hParent=TVI_ROOT;
tvs.item=tvi;
hTreeCtrl[0]=m_Tree.InsertItem(&tvs);
hTreeCurrent=hTreeCtrl[0];
tvi.pszText="Two";
tvs.hParent=hTreeCtrl[0];
tvs.item=tvi;
hTreeCtrl[1]=m_Tree.InsertItem(&tvs);
tvi.pszText="Three";
tvs.hParent=hTreeCtrl[1];
tvs.item=tvi;
hTreeCtrl[2]=m_Tree.InsertItem(&tvs);
tvi.pszText="Four";
tvs.hParent=hTreeCtrl[2];
tvs.item=tvi;
hTreeCtrl[3]=m_Tree.InsertItem(&tvs);
tvi.pszText="Five";
tvs.hParent=hTreeCtrl[2];
tvs.item=tvi;
hTreeCtrl[4]=m_Tree.InsertItem(&tvs);
}
Define the messages map:
BEGIN_MESSAGE_MAP(CMainWin,CFrameWnd)
ON_COMMAND(ID_CHANGE,OnChange)
ON_COMMAND(ID_TREE_EXPAND,OnExpand)
ON_COMMAND(ID_TREE_EXPANDALL,OnExpandAll)
ON_COMMAND(ID_TREE_COLLAPSE,OnCollapse)
ON_NOTIFY(TVN_SELCHANGED,ID_TREE,OnTreeChange)
ON_NOTIFY_RANGE(TTN_NEEDTEXT,0,IDR_TOOLBAR1,OnTTip)
ON_WM_PAINT()
ON_WM_SIZE()
END_MESSAGE_MAP()
Define the handlers:
void CMainWin::OnExpand() // handler to expand the tree’s node
{m_Tree.Expand(hTreeCurrent,TVE_EXPAND);}
void CMainWin::OnExpandAll() // handler to expand all the nodes of the tree
{int i;
for(i=0;i<NUM;i++)
m_Tree.Expand(hTreeCtrl[i],TVE_EXPAND);
}
void CMainWin::OnCollapse() // handler to collapse the tree’s nodes
{m_Tree.Expand(hTreeCurrent,TVE_COLLAPSE);}
void CMainWin::OnPaint() // handler to redraw the window
{CPaintDC dc(this);
TV_ITEM tvi;
char str1[40];
char str2[40];
tvi.hItem = hTreeCurrent;
tvi.pszText = str1;
tvi.cchTextMax = sizeof(str1)-1;
tvi.mask=TVIF_TEXT | TVIF_HANDLE;
m_Tree.GetItem(&tvi); // retrieves the name of new selected element
wsprintf(str2,"Current Selection %s",tvi.pszText);
dc.TextOut(2,150,str2,strlen(str2));
}
// OnTreeChange() function retrieves information on selected element and redraws the window
void CMainWin::OnTreeChange(NMHDR * hdr, LRESULT * NotUsed)
{hTreeCurrent=m_Tree.GetSelectedItem();
InvalidateRect(NULL);
}