
- •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.
7. Thread multitasking and it’s implementation in mfc
7.1 The basic features of multitasking
Microsoft Windows has two types of multitasking. First is based on the processes, second – on threads. The Microsoft Foundation Class Library (MFC) provides support for multithreaded applications.
A "process" is an executing instance of an application. For example, when you double-click the Notepad icon, you start a process that runs Notepad.
A "thread" is a path of execution within a process. When you start Notepad, the operating system creates a process and begins executing the primary thread of that process. When this thread terminates, so does the process. This primary thread is supplied to the operating system by the startup code in the form of a function address. Usually, it is the address of the main or WinMain function that is supplied.
You can create additional threads in your application if you wish. You may want to do this to handle background or maintenance tasks when you don't want the user to wait for them to complete. All threads in MFC applications are represented by CWinThread objects. In most situations, you don't even have to explicitly create these objects; instead call the framework helper function AfxBeginThread, which creates the CWinThread object for you. To make your multithread programming you would to include AFXMT.H library file.
MFC distinguishes two types of threads: user-interface threads and worker threads. User-interface threads are commonly used to handle user input and respond to events and messages generated by the user. Worker threads are commonly used to complete tasks, such as recalculation, that do not require user input. The Win32 API does not distinguish between types of threads; it just needs to know the thread's starting address so it can begin to execute the thread. MFC handles user-interface threads specially by supplying a message pump for events in the user interface. CWinApp is an example of a user-interface thread object, as it derives from CWinThread and handles events and messages generated by the user.
Special attention should be given to situations where more than one thread may require access to the same object. Writing and debugging multithreaded programming is inherently a complicated and tricky undertaking, as you must ensure that objects are not accessed by more than one thread at a time.
Worker thread creation
A worker thread is commonly used to handle background tasks that the user shouldn't have to wait for to continue using your application. Tasks such as recalculation and background printing are good examples of worker threads. This article details the steps necessary to create a worker thread.
Creating a worker thread is a relatively simple task. Only two steps are required to get your thread running: implementing the controlling function and starting the thread. It is not necessary to derive a class from CWinThread. You can if you need a special version of CWinThread, but it is not required for most simple worker threads. You can use CWinThread without modification. The starting of the thread is provided by AfxBeginThread function.
There are two overloaded versions of AfxBeginThread: one for user-interface threads and one for worker threads. To begin execution of your worker thread, call AfxBeginThread providing the following information:
The address of the controlling function.
The parameter to be passed to the controlling function.
(Optional) The desired priority of the thread. The default is normal priority. (Optional) The desired stack size for the thread. The default is the same size stack as the creating thread.
(Optional) CREATE_SUSPENDED if you want the thread to be created in a suspended state. The default is 0, or start the thread normally.
(Optional) The desired security attributes. The default is the same access as the parent thread. AfxBeginThread creates and initializes a CWinThread object for you, starts it, and returns its address so you can refer to it later. Checks are made throughout the procedure to make sure all objects are deallocated properly should any part of the creation fail.
The prototypes of AfxBeginThread() function are the following:
CWinThread* AfxBeginThread( AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
CWinThread* AfxBeginThread( CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL );
Parameters
pfnThreadProc Points to the controlling function for the worker thread. Cannot be NULL. This function must be declared as follows: UINT MyControllingFunction(LPVOID pParam );
pThreadClass The RUNTIME_CLASS of an object derived from CWinThread.
pParam Parameter to be passed to the controlling function as shown in the parameter to the function declaration in pfnThreadProc.
nPriority The desired priority of the thread. If 0, the same priority as the creating thread will be used. For a full list and description of the available priorities, see SetThreadPriority in the Win32 Programmer's Reference.
nStackSize Specifies the size in bytes of the stack for the new thread. If 0, the stack size defaults to the same size stack as the creating thread.
dwCreateFlags Specifies an additional flag that controls the creation of the thread. This flag can contain one of two values:
CREATE_SUSPENDED Start the thread with a suspend count of one. The thread will not execute until ResumeThread is called;
0 Start the thread immediately after creation.
lpSecurityAttrs Points to a SECURITY_ATTRIBUTES structure that specifies the security attributes for the thread. If NULL, the same security attributes as the creating thread will be used.
AfxBeginThread creates a new CWinThread object, calls its CreateThread function to start executing the thread, and returns a pointer to the thread. Checks are made throughout the procedure to make sure all objects are deallocated properly should any part of the creation fail. To end the thread, call AfxEndThread from within the thread, or return from the controlling function of the worker thread.
void AfxEndThread( UINT nExitCode );
// nExitCode Specifies the exit code of the thread.
The example of program with threads
The main features: on the selection of Execute Thread menu item – there will be created a worker thread. The thread provides 10 short sound signals, displaying the signal number with its identifier. The new thread can be run before the previous thread finish. Also in that program the conception of virtual window is used.
So the changes in the main program:
To include to the main menu of our program, the new item IDM_THREAD;
To change the main window class as in the Listing 7.1;
Listing 7.1 The CMainWin class implementation to provide the threads
Class CMainWin : CframeWnd
{public:
int m_X, m_Y; // the position to input
CDC m_memDC; // the device context, linked to the virtual window
CBitmap m_bmp; // the virtual window image
Cbrush m_bkBrush; // the virtual window brush
CMainWin();
afx_msg void OnPaint();
afx_msg void OnThreads();
DECLARE_MESSAGE_MAP()
};
To declare globally the functions for the threads:
UINT MyThread(LPVOID WinObjPtr);
int ThreadID=0; // if we have two threads: int ThreadID1=0; int ThreadID2=0;
int maxX, maxY;
To create the main window of application as in Listing 7.2:
Listing 7.2 The CMainWin constructor implementation
CMainWin::CMainWin()
{Create(……); // usual format for Create function
m_X=m_Y=0;
maxX=GetSystemMetrics(SM_CXSCREEN);
maxY=GetSystemMetrics(SM_CYSCREEN);
CClientDC dc(this);
m_memDC.CreateCompatibleDC(&DC);
m_bmp.CreateCompatibleBitmap(&DC, maxX, maxY);
m_memDC.SelectObject(&m_bmp);
m_bkBrush.CreateStockObject(WHITE_BRUSH);
m_memDC.SelectObject(&m_bkBrush);
m_memDC.PatBlt(0,0,maxX, maxY, PATCOPY); // virtual window filling
}
To implement the handler on IDM_THEAD selection as below:
afx_msg void CMainWIn::OnThreads()
{AfxBeginThread(MyThread, this);
}
To implement the worker thread as shown in Listing 7.3
Listing 7.3. The worker thread function implementation
UINT MyThread(LPVOID WinObjPtr)
{int i;
TEXTMETRICS tm;
int Tid = ThreadID;
char str[255];
ThreadID++;
CMainWin *ptr=(CMainWin*) WinObjPtr;
ptr-> m_memDC.GetTextMetrics(&tm);
for(i=0; i<10; i++)
{Sleep(500);
wsprintf(atr, “Thread #%d, beep #%d”, Tid,i);
ptr->m_memDC.TextOut(ptr->m_X, ptr->m_Y, str, str_len(str));
ptr->m_Y=ptr->m_Y+tm.tmHeght+tm.tmExtendLeading;
ptr->InvalidateRect(NULL);
MessageBeep(MB_OK);
}
ThreadID++;
return 0;
}
The suspending and resuming of worker thread execution
The SuspendThread() and ResumeThread() function suspend and resume the thread execution. There are their prototypes:
DWORD CWinThread::SuspendThread( );
DWORD CWinThread::ResumeThread( );
On it’s execution, every thread has the suspend count. If that value is equal to 0, the thread is running normally. On SuspendThread() function call, the counter has increment (+1), on ResumeThread() function, the counter decrements.
The control on threads priorities
Every thread has it’s own thread priority settings. That settings are the combination of two values: the general value of process priority class and the value of thread’s priority, relatively to the class.
The real thread priority is a sum of process class priority and thread priority level.
The thread priority indicates, how much of processor working time the thread needs (the higher priority – more time, the less priority – less time).
There are special API-functions, giving the information on priority class and that priority settings: GetPriorityClass(); and SetPriorityClass();
DWORD GetPriorityClass(HANDLE hProcess); // hProcess is a handle to the process
BOOL SetPriorityClass(HANDLE hProcess, DWORD dwPriorityClass);
Here: HANDLE hProcess // handle to the process
DWORD dwPriorityClass // priority class value
The process’s priority class is one of the following values:
Priority |
Meaning |
HIGH_PRIORITY_CLASS |
Indicates a process that performs time-critical tasks that must be executed immediately for it to run correctly. The threads of a high-priority class process preempt the threads of normal or idle priority class processes. An example is Windows Task List, which must respond quickly when called by the user, regardless of the load on the operating system. Use extreme care when using the high-priority class, because a high-priority class CPU-bound application can use nearly all available cycles. |
IDLE_PRIORITY_CLASS |
Indicates a process whose threads run only when the system is idle and are preempted by the threads of any process running in a higher priority class. An example is a screen saver. The idle priority class is inherited by child processes. |
NORMAL_PRIORITY_CLASS |
Indicates a normal process with no special scheduling needs. |
REALTIME_PRIORITY_CLASS |
Indicates a process that has the highest possible priority. The threads of a real-time priority class process preempt the threads of all other processes, including operating system processes performing important tasks. For example, a real-time process that executes for more than a very brief interval can cause disk caches not to flush or cause the mouse to be unresponsive. |
The thread priority means, how much processor time the thread takes in the general process time. On thread creating, the thread has normal priority.
The thread priorities are controlled by CWinThread function.
To determine the priority value, you can use GetThreadPriority(), to increase or decrease the priority – SetThreadPriority():
BOOL CWinThread::SetThreadPriority(int nPriority);
int CWinThread::GetThreadPriority( );
The parameter nPriority specifies the new thread priority level within its priority class. This parameter must be one of the following values, listed from highest priority to lowest:
THREAD_PRIORITY_TIME_CRITICAL
THREAD_PRIORITY_HIGHEST
THREAD_PRIORITY_ABOVE_NORMAL
THREAD_PRIORITY_NORMAL
THREAD_PRIORITY_BELOW_NORMAL
THREAD_PRIORITY_LOWEST
THREAD_PRIORITY_IDL