Windows程序设计 BITBLT.C--STRETCH.C--BRICKS1.C--BRICKS2.C--BRICKS3.C 范例分析笔记

本文介绍了如何使用 Windows API 进行位图加载、创建及显示的相关技术,包括使用 LoadBitmap 加载位图资源,CreateBitmap 创建自定义位图,以及 CreatePatternBrush 建立位图刷子来刷新窗体背景。
摘要由CSDN通过智能技术生成
/*---------------------------------------
   BITBLT.C – 利用BITBLT 函数抓取标题栏
                     图标区域到窗口显示区域
 
   STRETCH.C – 利用 StretchBlt 函数抓取窗口
                        标题图标区域到窗口显示区域,
                        并拉伸显示
  ---------------------------------------*/
 
#include <windows.h>
 
LRESULT CALLBACK WndProc ( HWND , UINT , WPARAM , LPARAM) ;
 
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance ,
                    PSTR szCmdLine , int iCmdShow)
{
     static TCHAR szAppName [] = TEXT ( " BitBlt ") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     wndclass . style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass . lpfnWndProc   = WndProc ;
     wndclass . cbClsExtra    = 0 ;
     wndclass . cbWndExtra    = 0 ;
     wndclass . hInstance     = hInstance ;
     wndclass . hIcon         = LoadIcon ( NULL , IDI_INFORMATION ) ;
     wndclass . hCursor       = LoadCursor ( NULL , IDC_ARROW ) ;
     wndclass . hbrBackground = ( HBRUSH) GetStockObject ( WHITE_BRUSH ) ;
     wndclass . lpszMenuName  = NULL ;
     wndclass . lpszClassName = szAppName ;
    
     if ( ! RegisterClass ( & wndclass))
     {
          MessageBox ( NULL , TEXT ( " This program requires Windows NT! " ),
                      szAppName , MB_ICONERROR ) ;
          return 0 ;
     }
    
     hwnd = CreateWindow ( szAppName , TEXT ( " BitBlt Demo " ),
                           WS_OVERLAPPEDWINDOW ,
                          CW_USEDEFAULT , CW_USEDEFAULT ,
                          CW_USEDEFAULT , CW_USEDEFAULT ,
                          NULL , NULL , hInstance , NULL ) ;
 
     ShowWindow ( hwnd , iCmdShow) ;
     UpdateWindow ( hwnd) ;
 
     while ( GetMessage ( & msg , NULL , 0 , 0))
     {
          TranslateMessage ( & msg) ;
          DispatchMessage ( & msg) ;
     }
     return msg . wParam ;
}
 
LRESULT CALLBACK WndProc ( HWND hwnd , UINT message , WPARAM wParam , LPARAM lParam)
{
     static int  cxClient , cyClient , cxSource , cySource ;
     HDC         hdcClient , hdcWindow ;
     int         x , y ;
     PAINTSTRUCT ps ;
    
     switch ( message)
     {
     case WM_CREATE :
          // 窗口的边框的宽度 + 标题栏按钮的宽度 = 标题栏图标及按钮的宽度
          cxSource = GetSystemMetrics ( SM_CXSIZEFRAME ) +
                     GetSystemMetrics ( SM_CXSIZE ) ;
          // 窗口的边框的高度 + 窗口标题的高度 = 窗口标题栏的高度
          cySource = GetSystemMetrics ( SM_CYSIZEFRAME ) +
                     GetSystemMetrics ( SM_CYCAPTION ) ;
          return 0 ;
 
     case WM_SIZE :
          cxClient = LOWORD ( lParam) ;
          cyClient = HIWORD ( lParam) ;
          return 0 ;
 
     case WM_PAINT :
          hdcClient = BeginPaint ( hwnd , &ps) ; // 取得窗口重绘区域句柄
          hdcWindow = GetWindowDC ( hwnd) ; // 取得整个窗口的句柄
 
          for ( y = 0 ; y < cyClient ; y += cySource)
          for ( x = 0 ; x < cxClient ; x += cxSource)
                //将整个窗口的句柄作为来源,传送到窗口重绘区域。通过循环而达到平铺位图到重绘区域的目的
              BitBlt ( hdcClient , x , y , cxSource , cySource , hdcWindow , 0 , 0 , SRCCOPY ) ;
 
         /* StretchBlt 方法:
            以窗体标题栏图标区域为来源,拉伸显示到窗口重绘区域 */
          StretchBlt ( hdcClient , 0 , 0 , cxClient , cyClient ,
                      hdcWindow , 0 , 0 , cxSource , cySource , MERGECOPY ) ;
 
 
          ReleaseDC ( hwnd , hdcWindow) ;
          EndPaint ( hwnd , &ps) ;
          return 0 ;
 
     case WM_DESTROY :
          PostQuitMessage ( 0) ;
          return 0 ;
     }
     return DefWindowProc ( hwnd , message , wParam , lParam) ;
}


/*----------------------------------------
   BRICKS1.C – LoadBitmap 位图资源的载入使用
  ----------------------------------------*/
 
#include <windows.h>
 
LRESULT CALLBACK WndProc ( HWND , UINT , WPARAM , LPARAM) ;
 
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance ,
                    PSTR szCmdLine , int iCmdShow)
{
     static TCHAR szAppName [] = TEXT ( " Bricks1 ") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     wndclass . style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass . lpfnWndProc   = WndProc ;
     wndclass . cbClsExtra    = 0 ;
     wndclass . cbWndExtra    = 0 ;
     wndclass . hInstance     = hInstance ;
     wndclass . hIcon         = LoadIcon ( NULL , IDI_APPLICATION ) ;
     wndclass . hCursor       = LoadCursor ( NULL , IDC_ARROW ) ;
     wndclass . hbrBackground = ( HBRUSH) GetStockObject ( WHITE_BRUSH ) ;
     wndclass . lpszMenuName  = NULL ;
     wndclass . lpszClassName = szAppName ;
    
     if ( ! RegisterClass ( & wndclass))
     {
          MessageBox ( NULL , TEXT ( " This program requires Windows NT! " ),
                      szAppName , MB_ICONERROR ) ;
          return 0 ;
     }
    
     hwnd = CreateWindow ( szAppName , TEXT ( " LoadBitmap Demo " ),
                          WS_OVERLAPPEDWINDOW ,
                          CW_USEDEFAULT , CW_USEDEFAULT ,
                          CW_USEDEFAULT , CW_USEDEFAULT ,
                          NULL , NULL , hInstance , NULL ) ;
 
     ShowWindow ( hwnd , iCmdShow) ;
     UpdateWindow ( hwnd) ;
 
     while ( GetMessage ( & msg , NULL , 0 , 0))
     {
           TranslateMessage ( & msg) ;
          DispatchMessage ( & msg) ;
     }
     return msg . wParam ;
}
 
LRESULT CALLBACK WndProc ( HWND hwnd , UINT message , WPARAM wParam , LPARAM lParam)
{
     static HBITMAP hBitmap ;
     static int     cxClient , cyClient , cxSource , cySource ;
     BITMAP         bitmap ;
     HDC            hdc , hdcMem ;
     HINSTANCE      hInstance ;
     int            x , y ;
     PAINTSTRUCT    ps ;
    
     switch ( message)
     {
     case WM_CREATE :
          // 强制转换消息中 lParam 参数为指向 CREATESTRUCT 结构的指针 "LPCREATESTRUCT", 并调取 hInstance 元素
          hInstance = (( LPCREATESTRUCT) lParam) -> hInstance ;
 
          hBitmap = LoadBitmap ( hInstance , TEXT ( " Bricks ")) ; // 载入位图资源进内存 , 取得句柄
 
          GetObject ( hBitmap , sizeof ( BITMAP ), & bitmap) ; // 取位图资源信息
 
          cxSource = bitmap . bmWidth ;
          cySource = bitmap . bmHeight// 取位图资源的宽高
 
          return 0 ;
 
     case WM_SIZE :
          cxClient = LOWORD ( lParam) ;
          cyClient = HIWORD ( lParam) ;
          return 0 ;
 
     case WM_PAINT :
          hdc = BeginPaint ( hwnd , &ps) ;

          // 建立与视讯显示器兼容的内存设备内容 , 取得句柄
          hdcMem = CreateCompatibleDC ( hdc) ;
          SelectObject ( hdcMem , hBitmap) ; // 将资源位图选择进内存设备内容
 
          for ( y = 0 ; y < cyClient ; y += cySource)
          for ( x = 0 ; x < cxClient ; x += cxSource)
          {
               // 将内存设备内容中的位图 , 平铺到显示区域
               BitBlt ( hdc , x , y , cxSource , cySource , hdcMem , 0 , 0 , SRCCOPY ) ;
          }
 
          DeleteDC ( hdcMem) ; // 释放内存设备内容
          EndPaint ( hwnd , &ps) ;
          return 0 ;
 
     case WM_DESTROY :
          DeleteObject ( hBitmap) ; //释放载入的位图资源
          PostQuitMessage ( 0) ;
          return 0 ;
     }
     return DefWindowProc ( hwnd , message , wParam , lParam) ;
}


/*-----------------------------------------
   BRICKS2.C – 利用CreateBitmap 函数建立单色 
                        8*8 位图,并平铺到显示区域
  -----------------------------------------*/
 
#include <windows.h>
 
LRESULT CALLBACK WndProc ( HWND , UINT , WPARAM , LPARAM) ;
 
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance ,
                    PSTR szCmdLine , int iCmdShow)
{
     static TCHAR szAppName [] = TEXT ( " Bricks2 ") ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     wndclass . style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass . lpfnWndProc   = WndProc ;
     wndclass . cbClsExtra    = 0 ;
     wndclass . cbWndExtra    = 0 ;
     wndclass . hInstance     = hInstance ;
     wndclass . hIcon         = LoadIcon ( NULL , IDI_APPLICATION ) ;
     wndclass . hCursor       = LoadCursor ( NULL , IDC_ARROW ) ;
     wndclass . hbrBackground = ( HBRUSH) GetStockObject ( WHITE_BRUSH ) ;
     wndclass . lpszMenuName  = NULL ;
     wndclass . lpszClassName = szAppName ;
    
     if ( ! RegisterClass ( & wndclass))
     {
          MessageBox ( NULL , TEXT ( " This program requires Windows NT! " ),
                      szAppName , MB_ICONERROR ) ;
          return 0 ;
     }
    
     hwnd = CreateWindow ( szAppName , TEXT ( " CreateBitmap Demo " ),
                          WS_OVERLAPPEDWINDOW ,
                          CW_USEDEFAULT , CW_USEDEFAULT ,
                           CW_USEDEFAULT , CW_USEDEFAULT ,
                          NULL , NULL , hInstance , NULL ) ;
 
     ShowWindow ( hwnd , iCmdShow) ;
     UpdateWindow ( hwnd) ;
 
     while ( GetMessage ( & msg , NULL , 0 , 0))
     {
          TranslateMessage ( & msg) ;
           DispatchMessage ( & msg) ;
     }
     return msg . wParam ;
}
 
LRESULT CALLBACK WndProc ( HWND hwnd , UINT message , WPARAM wParam , LPARAM lParam)
{
     //初始化位图结构信息,大小8*8,每行占2字节,1颜色位面,每像素占1位
     static BITMAP  bitmap = { 0 , 8 , 8 , 2 , 1 , 1 } ;
     //初始化位图数据,0为黑色,1为白色
     static BYTE    bits [ 8 ][ 2 ] = { 0xFF , 0 , 0x0C , 0 , 0x0C , 0 , 0x0C , 0 ,
                                    0xFF , 0 , 0xC0 , 0 , 0xC0 , 0 , 0xC0 , 0 } ;
     static HBITMAP hBitmap ;
     static int     cxClient , cyClient , cxSource , cySource ;
     HDC            hdc , hdcMem ;
     int            x , y ;
     PAINTSTRUCT    ps ;
    
     switch ( message)
     {
     case WM_CREATE :
          bitmap . bmBits = bits ; // 设定建立位图时所用数据数组指针
          hBitmap = CreateBitmapIndirect ( & bitmap) ; // 用bitmap 结构中的信息建立位图
          cxSource = bitmap . bmWidth ;
          cySource = bitmap . bmHeight// 取位图的宽和高
          return 0 ;
 
     case WM_SIZE :
          cxClient = LOWORD ( lParam) ;
          cyClient = HIWORD ( lParam) ;
          return 0 ;
 
     case WM_PAINT :
          hdc = BeginPaint ( hwnd , &ps) ;
 
          hdcMem = CreateCompatibleDC ( hdc) ; // 建立与视讯显示器兼容的内存设备内容
          SelectObject ( hdcMem , hBitmap) ;  // 将位图选进内存设备内容
 
          for ( y = 0 ; y < cyClient ; y += cySource)
          for ( x = 0 ; x < cxClient ; x += cxSource)
          {
               // 以内存设备内容为来源,平铺显示位图到显示区域
               BitBlt ( hdc , x , y , cxSource , cySource , hdcMem , 0 , 0 , SRCCOPY ) ;
          }
 
          DeleteDC ( hdcMem) ; // 删除内存设备内容
          EndPaint ( hwnd , &ps) ;
          return 0 ;
 
     case WM_DESTROY :
          DeleteObject ( hBitmap) ; // 删除建立的位图
          PostQuitMessage ( 0) ;
          return 0 ;
     }
     return DefWindowProc ( hwnd , message , wParam , lParam) ;
}


/*-----------------------------------------------
   BRICKS3.C – 利用CreatePatternBrush 函数,建立
                        位图刷子,刷新窗体背景
  -----------------------------------------------*/
 
#include <windows.h>
 
LRESULT CALLBACK WndProc ( HWND , UINT , WPARAM , LPARAM) ;
 
int WINAPI WinMain ( HINSTANCE hInstance , HINSTANCE hPrevInstance ,
                    PSTR szCmdLine , int iCmdShow)
{
     static TCHAR szAppName [] = TEXT ( " Bricks3 ") ;
     HBITMAP      hBitmap ;
     HBRUSH       hBrush ;
     HWND         hwnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;
 
     hBitmap = LoadBitmap ( hInstance , TEXT ( " Bricks ")) ; // 载入位图资源进内存
     hBrush = CreatePatternBrush ( hBitmap) ; // 以内存中位图来建立刷子 , 用于刷新窗体背景
     DeleteObject ( hBitmap) ; // 释放位图资源
 
     wndclass . style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass . lpfnWndProc   = WndProc ;
     wndclass . cbClsExtra    = 0 ;
     wndclass . cbWndExtra    = 0 ;
     wndclass . hInstance     = hInstance ;
     wndclass . hIcon         = LoadIcon ( NULL , IDI_APPLICATION ) ;
     wndclass . hCursor       = LoadCursor ( NULL , IDC_ARROW ) ;
     wndclass . hbrBackground = hBrush//位图刷子句柄
     wndclass . lpszMenuName  = NULL ;
     wndclass . lpszClassName = szAppName ;
    
     if ( ! RegisterClass ( & wndclass))
     {
          MessageBox ( NULL , TEXT ( " This program requires Windows NT! " ),
                      szAppName , MB_ICONERROR ) ;
          return 0 ;
     }
    
     hwnd = CreateWindow ( szAppName , TEXT ( " CreatePatternBrush Demo " ),
                          WS_OVERLAPPEDWINDOW ,
                           CW_USEDEFAULT , CW_USEDEFAULT ,
                          CW_USEDEFAULT , CW_USEDEFAULT ,
                          NULL , NULL , hInstance , NULL ) ;
 
     ShowWindow ( hwnd , iCmdShow) ;
     UpdateWindow ( hwnd) ;
 
     while ( GetMessage ( & msg , NULL , 0 , 0))
     {
          TranslateMessage ( & msg) ;
          DispatchMessage ( & msg) ;
     }
 
     DeleteObject ( hBrush) ; // 删除位图刷子
     return msg . wParam ;
}
 
LRESULT CALLBACK WndProc ( HWND hwnd , UINT message , WPARAM wParam , LPARAM lParam)
{
     switch ( message)
     {
     case WM_DESTROY :
          PostQuitMessage ( 0) ;
          return 0 ;
     }
     return DefWindowProc ( hwnd , message , wParam , lParam) ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值