
- •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.
Icon and cursor loading
To use icon and cursor, they should be previously loaded. Their prototypes:
HICON CWinApp::LoadIcon( LPCTSTR lpszResourceName ) const;
HICON CWinApp::LoadIcon( UINT nIDResource ) const;
HCURSOR CWinApp::LoadCursor( LPCTSTR lpszResourceName ) const;
HCURSOR CWinApp::LoadCursor( UINT nIDResource ) const;
Parameters:
lpszResourceName Points to a null-terminated string that contains the name of the cursor resource. You can use a CString for this argument.
nIDResource ID number of the cursor resource.
The brushes, icons and their styles
To register the window class, there should be set the brush handle (which is used to fill the working area of window by some background color). The application uses or stsndard brush or created in the program.
The standard bruch can be get by the following function:
BOOL CGdiObject::CreateStockObject( int nIndex );
Parameter - nIndex A constant specifying the type of stock object desired. It can be one of the following values:
BLACK_BRUSH Black brush.
DKGRAY_BRUSH Dark gray brush.
GRAY_BRUSH Gray brush.
HOLLOW_BRUSH Hollow brush.
LTGRAY_BRUSH Light gray brush.
NULL_BRUSH Null brush.
WHITE_BRUSH White brush.
BLACK_PEN Black pen.
NULL_PEN Null pen.
WHITE_PEN White pen.
ANSI_FIXED_FONT ANSI fixed system font.
ANSI_VAR_FONT ANSI variable system font.
DEVICE_DEFAULT_FONT Device-dependent font.
OEM_FIXED_FONT OEM-dependent fixed font.
SYSTEM_FONT The system font. By default, Windows uses the system font to draw menus, dialog-box controls, and other text. In Windows versions 3.0 and later, the system font is proportional width; earlier versions of Windows use a fixed-width system font.
SYSTEM_FIXED_FONT The fixed-width system font used in Windows prior to version 3.0. This object is available for compatibility with earlier versions of Windows.
DEFAULT_PALETTE Default color palette. This palette consists of the 20 static colors in the system palette.
To create the brush we can use the next instrucrions:
CBrush bkbrush; // the brush object setting
Bkbrush.CreateStockObject(WHITE_BRUSH); // brush is linked to white background
The small icon presents the program for a task panel, big – in the Explorer window, for Desktop. If we have created own cursor, it will be visible on mouse locating in limits of your window. Listing 3.7 shows the example of program with loaded brush and cursor.
Listing 3.7 The program example with loaded brush and cursor implementation.
// The header file contents
class CMainWin : public CFrameWnd
{public: CMainWin(LPCSTR);
DECLARE_MESSAGE_MAP()
};
class CApp : public CWinApp
{public: BOOL InitInstance();
};
// The implementation file
#include<afxwin.h>
CMainWin::CMainWin(LPCSTR ClassName)
{Create(ClassName,"MainWindow");}
BOOL CApp::InitInstance()
{CBrush bkBrush;
bkBrush.CreateStockObject(WHITE_BRUSH);
LPCSTR cname = AfxRegisterWndClass (0, LoadCursor(IDC_CURSOR1), a, LoadIcon(IDI_ICON1));
m_pMainWnd = new CMainWin(cname); // The name of window class is passed to Main Window constructor
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
BEGIN_MESSAGE_MAP(CMainWin,CFrameWnd)
END_MESSAGE_MAP()
CApp App;
The standard brushes and icons using
There are defined several system icons and cursors in Windows. They can be used in the program. For that you can use special functions LoadIStandardIcon() and LoadStandardCursor(). Here is their prototypes:
HICON CWinApp::LoadStandardIcon( LPCTSTR lpszIconName );
HCURSOR LoadStandardCursor( LPCTSTR lpszCursorName );
lpszIconName A manifest constant identifier that specifies a predefined Windows icon. These identifiers are defined in WINDOWS.H. The following list shows the possible predefined values and meanings for lpszIconName:
IDI_APPLICATION Default application icon
IDI_HAND Hand-shaped icon used in serious warning messages
IDI_QUESTION Question-mark shape used in prompting messages
IDI_EXCLAMATION Exclamation point shape used in warning messages
IDI_ASTERISK Asterisk shape used in informative messages
lpszCursorName An IDC_ manifest constant identifier that specifies a predefined Windows cursor. These identifiers are defined in WINDOWS.H. The following list shows the possible predefined values and meanings for lpszCursorName:
IDC_ARROW Standard arrow cursor
IDC_IBEAM Standard text-insertion cursor
IDC_WAIT Hourglass cursor used when Windows performs a time-consuming task
IDC_CROSS Cross-hair cursor for selection
IDC_UPARROW Arrow that points straight up
IDC_SIZE Obsolete and unsupported; use IDC_SIZEALL
IDC_SIZEALL A four-pointed arrow. The cursor to use to resize a window.
IDC_ICON Obsolete and unsupported. Use IDC_ARROW.
IDC_SIZENWSE Two-headed arrow with ends at upper left and lower right
IDC_SIZENESW Two-headed arrow with ends at upper right and lower left
IDC_SIZEWE Horizontal two-headed arrow
IDC_SIZENS Vertical two-headed arrow
So, you can simply change the text in Listing 3.7:
LPCSTR cname = AfxRegisterWndClass (0, LoadCursor(IDC_CROSS), a, LoadIcon(IDI_HAND));
Working with raster images
So, as Windows is graphic operational system, it’s gives possibilities to use the images in programs.
The raster image is a standalone graphic resource, which is used as single unit. It contains bit-serial description of image, outputted to the screen without changes. The raster images are described by CBitmap class.
Usually image is created in graphical editor (f.e Paint of MS Windows). That files are added to the resource file. By the way, there are some difficulties with output of files, created by other programs (not in the resource editor of Visual C++).
Raster images output
If image is created and located in resource file, it can be outputted to the display so much time as you want.
First of all, it’s need to create the object of CBitmap type and by LoadBitmap() to load the raster image from the resource file.
BOOL CBitmap::LoadBitmap( LPCTSTR lpszResourceName );
BOOL CBitmap::LoadBitmap( UINT nIDResource );
Parameters: lpszResourceName Points to a null-terminated string that contains the name of the bitmap resource.
nIDResource Specifies the resource ID number of the bitmap resource.
Function loads the bitmap resource named by lpszResourceName or identified by the ID number in nIDResource from the application's executable file. The loaded bitmap is attached to the CBitmap object. If the bitmap identified by lpszResourceName does not exist or if there is insufficient memory to load the bitmap, the function returns 0.
CBitmap bit; // creating of CBitmap object
bit.LoadBitmap(IDB_BITMAP1); // bitmap loading
To output the object to the screen, it’s need:
To receive the device context, to get possibility of information output ti thw working area of window;
To get a compatible with it (see (1)) memory device context, where the raster images will be stored before output to the display.
To select the required image from resource file to the memory device context.
To copy the image from memory device context to the window device context.
In the result, the image will be outputted to the display.
Program with raster image output
In the example program the raster image in outputted to the screen after every left button mouse click. We assume, that image is already loaded under m_bmp1 name.
Listing 3.8 Left mouse handler, providing image output
void CMain::OnLButtonDown(UINT flags,CPoint Loc)
{CClientDC dc(this); // getting device context of main window (this)
CDC memDC; // the memory device context declaration
memDC.CreateCompatibleDC(&dc); // making memory device context compatible // to device context of main window (dc)
memDC.SelectObject(&bmp1); // image selecting to memory device context
dc.BitBlt(Loc.x,Loc.y,48,48,&memDC,0,0,SRCCOPY); // information copying from // memory device context (memDC) to device
// context of main window (dc)
}
So our activity is following:
To declare the device context dc, linked to the current window;
To declare memory device context memDC;
By CreateCompatibleDC() function memory device context is provided to be compatible to device context dc;
Before image output to the display, it’s selected to the memory device context, by SelectObject() function call – so there are can be used several images in application, but we select the one only;
To show the contents of selected object to display, there are used BitBlt() function, which copies the image from the source context to context, linked to object, which call the function yourself. The format of that function:
BOOL CDC::BitBlt( int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop );
Parameters:
x Specifies the logical x-coordinate of the upper-left corner of the destination rectangle.
y Specifies the logical y-coordinate of the upper-left corner of the destination rectangle.
nWidth Specifies the width (in logical units) of the destination rectangle and source bitmap.
nHeight Specifies the height (in logical units) of the destination rectangle and source bitmap.
pSrcDC Pointer to a CDC object that identifies the device context from which the bitmap will be copied. It must be NULL if dwRop specifies a raster operation that does not include a source.
xSrc Specifies the logical x-coordinate of the upper-left corner of the source bitmap.
ySrc Specifies the logical y-coordinate of the upper-left corner of the source bitmap.
dwRop Specifies the raster operation to be performed. Raster-operation codes define how the GDI combines colors in output operations that involve a current brush, a possible source bitmap, and a destination bitmap. The following lists raster-operation codes for dwRop and their descriptions:
BLACKNESS Turns all output black.
DSTINVERT Inverts the destination bitmap.
PATCOPY Copies the pattern to the destination bitmap.
PATINVERT Combines the destination bitmap with the pattern using the Boolean XOR operator.
On object of type CBitmap erase, the image is deleted, while it can be deleted manually, by CGdiObject::DeleteObject() function.
Considering the program with several images, where different (two) images are displayed by various button of mouse, we can notice some differences:
The images are loaded in the constructor:
CMain::CMain(LPCSTR namen)
{Create(NULL,"Win-32");
bmp1.LoadBitmap(IDB_BITMAP1); bmp2.LoadBitmap(IDB_BITMAP1);
}
BEGIN_MESSAGE_MAP(CMain,CFrameWnd)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
The handlers of mouse button click:
void CMain::OnLButtonDown(UINT flags,CPoint Loc)
{CClientDC dc(this);
CDC memDC;
memDC.CreateCompatibleDC(&dc);
memDC.SelectObject(&bmp1);
dc.BitBlt(Loc.x,Loc.y,48,48,&memDC,0,0,SRCCOPY);
}
void CMain::OnRButtonDown(UINT flags,CPoint Loc)
{CClientDC dc(this); CDC memDC;
memDC.CreateCompatibleDC(&dc); memDC.SelectObject(&bmp2);
dc.BitBlt(Loc.x,Loc.y,48,48,&memDC,0,0,SRCCOPY); }
So, in this way, you can easily operate with any number of images in your program, while certainly there are a lot of other interesting and complex problems of graphics output.
The next Listing 3.9 contains the full text of program with image ouput.
Listing 3.9 The simple program with image output
// App.h: interface for the classes
class CApp : public CWinApp
{public:
BOOL InitInstance();
};
class CMain : public CFrameWnd
{public:
void OnExit();
void OnPicture();
CMain();
DECLARE_MESSAGE_MAP()
private:
CBitmap bmp;
};
// App.cpp: implementation classes.
// IDB_BITMAP1 – the image resource
#include <afxwin.h>
#include "App.h"
#include "resource.h"
CMain::CMain()
{Create(NULL,"Image output Program",
WS_OVERLAPPEDWINDOW, rectDefault,NULL, MAKEINTRESOURCE(IDR_MENU1));
bmp.LoadBitmap(IDB_BITMAP1);
}
BOOL CApp::InitInstance()
{m_pMainWnd = new CMain;
m_pMainWnd->ShowWindow(m_nCmdShow);
m_pMainWnd->UpdateWindow();
return TRUE;
}
CApp App;
BEGIN_MESSAGE_MAP(CMain,CFrameWnd)
ON_COMMAND(ID_FIRST_PICTURE,OnPicture)
ON_COMMAND(ID_FIRST_EXIT,OnExit)
END_MESSAGE_MAP()
void CMain::OnPicture()
{BITMAP bm;
bmp.GetBitmap(&bm);
CClientDC dc(this);
CDC mdc;
mdc.CreateCompatibleDC(&dc);
mdc.SelectObject(&bmp);
dc.BitBlt(50,50,bm.bmWidth,bm.bmHeight,
&mdc,0,0,SRCCOPY);
}
void CMain::OnExit()
{SendMessage(WM_CLOSE);
}