The way we can create different types of pens, we can also create three different types of brushes. These are—solid brush, hatch brush and pattern brush. Let us now write a program that shows how to build these brushes and then use them to fill rectangles. Here is the OnPaint( ) handler which achieves this.
void OnPaint ( HWND hWnd )
{
HDC hdc ;
PAINTSTRUCT ps ;
HBRUSH hbr
void OnPaint ( HWND hWnd )
{
HDC hdc ;
PAINTSTRUCT ps ;
HBRUSH hbr
HGDIOBJ holdbr ;
HBITMAP hbmp ;
hdc = BeginPaint ( hWnd, &ps ) ;
hbr = CreateSolidBrush ( RGB (255, 0, 0 ) ) ;
holdbr = SelectObject ( hdc, hbr ) ;
Rectangle ( hdc, 5, 5, 105, 100 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
hbr = CreateHatchBrush ( HS_CROSS, RGB ( 255, 0, 0 ) ) ;
holdbr = SelectObject ( hdc, hbr ) ;
Rectangle ( hdc, 125, 5, 225, 100 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
hbmp = LoadBitmap ( hInst, MAKEINTRESOURCE ( IDB_BITMAP1 ) ) ;
hbr = CreatePatternBrush ( hbmp ) ;
holdbr = SelectObject ( hdc, hbr ) ;
Rectangle ( hdc, 245, 5, 345, 100 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
DeleteObject ( hbmp ) ;
EndPaint ( hWnd, &ps ) ;
DeleteObject ( hbr ) ;
}
HBITMAP hbmp ;
hdc = BeginPaint ( hWnd, &ps ) ;
hbr = CreateSolidBrush ( RGB (255, 0, 0 ) ) ;
holdbr = SelectObject ( hdc, hbr ) ;
Rectangle ( hdc, 5, 5, 105, 100 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
hbr = CreateHatchBrush ( HS_CROSS, RGB ( 255, 0, 0 ) ) ;
holdbr = SelectObject ( hdc, hbr ) ;
Rectangle ( hdc, 125, 5, 225, 100 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
hbmp = LoadBitmap ( hInst, MAKEINTRESOURCE ( IDB_BITMAP1 ) ) ;
hbr = CreatePatternBrush ( hbmp ) ;
holdbr = SelectObject ( hdc, hbr ) ;
Rectangle ( hdc, 245, 5, 345, 100 ) ;
SelectObject ( hdc, holdbr ) ;
DeleteObject ( hbr ) ;
DeleteObject ( hbmp ) ;
EndPaint ( hWnd, &ps ) ;
DeleteObject ( hbr ) ;
}
On execution of this program the window shown in Figure 18.4 appears
In the OnPaint( ) handler we have drawn three rectangles—first using a solid brush, second using a hatched brush and third using a pattern brush. Creating and using a solid brush and hatched brush is simple. We simply have to make calls to CreateSolidBrush( ) and CreateHatchBrush( ) respectively. For the hatch brush we have used the style HS_CROSS. There are several other styles defined in ‘windows.h’ that you can experiment with.
For creating a pattern brush we need to first create a bitmap (pattern). Instead of creating this pattern, we have used a readymade bitmap file. You can use any other bitmap file present on your hard disk.
Bitmaps, menus, icons, cursors that a Windows program may use are its resources. When the compile such a program we usually want these resources to become a part of our EXE file. If so done we do not have to ship these resources separately. To be able to use a resource (bitmap file in our case) it is not enough to just copy it in the project directory. Instead we need to carry out the steps mentioned below to add a bitmap file to the project.
(a) From the ‘Insert’ menu option of VC++ 6.0 select the ‘Resource’ option.
(b) From the dialog that pops up select ‘bitmap’ followed by the import button.For creating a pattern brush we need to first create a bitmap (pattern). Instead of creating this pattern, we have used a readymade bitmap file. You can use any other bitmap file present on your hard disk.
Bitmaps, menus, icons, cursors that a Windows program may use are its resources. When the compile such a program we usually want these resources to become a part of our EXE file. If so done we do not have to ship these resources separately. To be able to use a resource (bitmap file in our case) it is not enough to just copy it in the project directory. Instead we need to carry out the steps mentioned below to add a bitmap file to the project.
(a) From the ‘Insert’ menu option of VC++ 6.0 select the ‘Resource’ option.
(c)Select the suitable .bmp file.
(d)From the ‘File’ menu select the save option to save the generated resource script file (Script1.rc). (e) When we select ‘Save’ one more file called ‘resource.h’ also gets created.
Add the ‘Script1.rc’ file to the project using the Project | Add to Project | Files option.
While using the bitmap in the program it is always referred using an id. The id is #defined in the file ‘resource.h’. Somewhere information has to be stored linking the id with the actual .bmp file on the disk. This is done in the ‘Script1.rc’ file. We need to include the ‘resource.h’ file in the program.
To create the pattern brush we first need to load the bitmap in memory. We have done this using the LoadBitmap( ) API function. The first parameter passed to this function is the handle to the instance of the program. When InitInstance( ) function is called from WinMain( ) it stores the instance handle in a global variable hInst. We have passed this hInst to LoadBitmap( ). The second parameter passed to it is a string representing the bitmap. This string is created from the resource id using the MAKEINTRESOURCE macro. The LoadBitmap( ) function returns the handle to the bitmap. This handle is then passed to the CreatePatternBrush( ) function. This brush is then selected into the DC and then a rectangle is drawn using it.
Note that if the size of the bitmap is bigger than the rectangle being drawn then the bitmap is suitably clipped. On the other hand if the bitmap is smaller than the rectangle it is suitably replicated.
While doing the clean up firstly the brush is deleted followed by the bitmap.
No comments:
Post a Comment