Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
SSW_8_11.doc
Скачиваний:
0
Добавлен:
01.05.2025
Размер:
5.22 Mб
Скачать

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:

  1. To receive the device context, to get possibility of information output ti thw working area of window;

  2. To get a compatible with it (see (1)) memory device context, where the raster images will be stored before output to the display.

  3. To select the required image from resource file to the memory device context.

  4. 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:

  1. To declare the device context dc, linked to the current window;

  2. To declare memory device context memDC;

  3. By CreateCompatibleDC() function memory device context is provided to be compatible to device context dc;

  4. 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;

  5. 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:

  1. 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()

  1. 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);

}

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]