Create_DIB_Bitmap.cpp
Upload User: dq031136
Upload Date: 2022-08-08
Package Size: 802k
Code Size: 8k
Development Platform:

C++ Builder

  1. #include <windows.h>  
  2. #include "Create_DIB_Bitmap.h" 
  3. #if defined (WIN32)
  4. #define IS_WIN32 TRUE
  5. #else
  6. #define IS_WIN32 FALSE
  7. #endif
  8. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  9. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  10. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  11. HINSTANCE hInst;   // current instance
  12. LPCTSTR lpszAppName  = "MyApp";
  13. LPCTSTR lpszTitle    = "My Application"; 
  14. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  15. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  16.                       LPTSTR lpCmdLine, int nCmdShow)
  17. {
  18.    MSG      msg;
  19.    HWND     hWnd; 
  20.    WNDCLASS wc;
  21.    // Register the main application window class.
  22.    //............................................
  23.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  24.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  25.    wc.cbClsExtra    = 0;                      
  26.    wc.cbWndExtra    = 0;                      
  27.    wc.hInstance     = hInstance;              
  28.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  29.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  30.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  31.    wc.lpszMenuName  = lpszAppName;              
  32.    wc.lpszClassName = lpszAppName;              
  33.    if ( IS_WIN95 )
  34.    {
  35.       if ( !RegisterWin95( &wc ) )
  36.          return( FALSE );
  37.    }
  38.    else if ( !RegisterClass( &wc ) )
  39.       return( FALSE );
  40.    hInst = hInstance; 
  41.    // Create the main application window.
  42.    //....................................
  43.    hWnd = CreateWindow( lpszAppName, 
  44.                         lpszTitle,    
  45.                         WS_OVERLAPPEDWINDOW, 
  46.                         CW_USEDEFAULT, 0, 
  47.                         CW_USEDEFAULT, 0,  
  48.                         NULL,              
  49.                         NULL,              
  50.                         hInstance,         
  51.                         NULL               
  52.                       );
  53.    if ( !hWnd ) 
  54.       return( FALSE );
  55.    ShowWindow( hWnd, nCmdShow ); 
  56.    UpdateWindow( hWnd );         
  57.    while( GetMessage( &msg, NULL, 0, 0) )   
  58.    {
  59.       TranslateMessage( &msg ); 
  60.       DispatchMessage( &msg );  
  61.    }
  62.    return( msg.wParam ); 
  63. }
  64. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  65. {
  66.    WNDCLASSEX wcex;
  67.    wcex.style         = lpwc->style;
  68.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  69.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  70.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  71.    wcex.hInstance     = lpwc->hInstance;
  72.    wcex.hIcon         = lpwc->hIcon;
  73.    wcex.hCursor       = lpwc->hCursor;
  74.    wcex.hbrBackground = lpwc->hbrBackground;
  75.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  76.    wcex.lpszClassName = lpwc->lpszClassName;
  77.    // Added elements for Windows 95.
  78.    //...............................
  79.    wcex.cbSize = sizeof(WNDCLASSEX);
  80.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  81.                             IMAGE_ICON, 16, 16,
  82.                             LR_DEFAULTCOLOR );
  83.    return RegisterClassEx( &wcex );
  84. }
  85. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  86. {
  87.    switch( uMsg )
  88.    {
  89.       case WM_COMMAND :
  90.               switch( LOWORD( wParam ) )
  91.               {
  92.                  case IDM_TEST :
  93.                         {
  94.                            BITMAPINFOHEADER  bi;
  95.                            BITMAPINFOHEADER* lpbi;
  96.                            HBITMAP           hBitmap;
  97.                            HDC               hDC, hMemDC;
  98.                            HANDLE            hDIB;
  99.                            // Initialize the BITMAPINFOHEADER structure.
  100.                            //...........................................
  101.                            bi.biSize     = sizeof( BITMAPINFOHEADER );
  102.                            bi.biWidth    = 50;
  103.                            bi.biHeight   = 50;
  104.                            bi.biPlanes   = 1;
  105.                            bi.biBitCount = 4;
  106.                            bi.biCompression   = BI_RGB;
  107.                            bi.biSizeImage     = 0;
  108.                            bi.biXPelsPerMeter = 0;
  109.                            bi.biYPelsPerMeter = 0;
  110.                            bi.biClrUsed       = 0;
  111.                            bi.biClrImportant  = 0;
  112.                            hDC = GetDC( hWnd );
  113.                            // Create DIB.
  114.                            //............
  115.                            hBitmap = CreateDIBitmap( hDC, &bi, 0L, NULL, 
  116.                                                      NULL, 0 );
  117.                            // Allocate memory for BITMAPINFO structure.
  118.                            //..........................................
  119.                            hDIB    = GlobalAlloc( GHND, 
  120.                                                   sizeof( BITMAPINFOHEADER )+
  121.                                                   16 * sizeof( RGBQUAD ) );
  122.                            lpbi = (BITMAPINFOHEADER*)GlobalLock( hDIB );
  123.                            // Copy bi to top of BITMAPINFO structure.
  124.                            //........................................
  125.                            *lpbi = bi;
  126.                            // Use GetDIBits() to init bi struct data.
  127.                            //........................................
  128.                            GetDIBits( hDC, hBitmap, 0, 50, NULL, 
  129.                                       (LPBITMAPINFO)lpbi, DIB_RGB_COLORS );
  130.                            GlobalUnlock( hDIB );
  131.                            // Create a memory device context 
  132.                            // and select the DIB into it.
  133.                            //...............................
  134.                            hMemDC = CreateCompatibleDC( hDC );
  135.                            SelectObject( hMemDC, hBitmap );
  136.                            // Paint on memory device context.
  137.                            //................................
  138.                            SelectObject( hMemDC, GetStockObject(BLACK_BRUSH));
  139.                            Rectangle( hMemDC, 0, 0, 50, 50 );
  140.                            SelectObject( hMemDC, GetStockObject(WHITE_BRUSH));
  141.                            Ellipse( hMemDC, 0, 0, 50, 50 );
  142.                            Ellipse( hMemDC, 10, 0, 40, 50 );
  143.                            Ellipse( hMemDC, 20, 0, 30, 50 );
  144.                            // Paint the bitmap on the display.
  145.                            //.................................
  146.                            BitBlt( hDC, 0, 0, 50, 50,
  147.                                    hMemDC, 0, 0, SRCCOPY );
  148.                            DeleteDC( hMemDC );
  149.                            GlobalFree( hDIB );
  150.                            ReleaseDC( hWnd, hDC );
  151.                         }
  152.                         break;
  153.                  case IDM_ABOUT :
  154.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  155.                         break;
  156.                  case IDM_EXIT :
  157.                         DestroyWindow( hWnd );
  158.                         break;
  159.               }
  160.               break;
  161.       
  162.       case WM_DESTROY :
  163.               PostQuitMessage(0);
  164.               break;
  165.       default :
  166.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  167.    }
  168.    return( 0L );
  169. }
  170. LRESULT CALLBACK About( HWND hDlg,           
  171.                         UINT message,        
  172.                         WPARAM wParam,       
  173.                         LPARAM lParam)
  174. {
  175.    switch (message) 
  176.    {
  177.        case WM_INITDIALOG: 
  178.                return (TRUE);
  179.        case WM_COMMAND:                              
  180.                if (   LOWORD(wParam) == IDOK         
  181.                    || LOWORD(wParam) == IDCANCEL)    
  182.                {
  183.                        EndDialog(hDlg, TRUE);        
  184.                        return (TRUE);
  185.                }
  186.                break;
  187.    }
  188.    return (FALSE); 
  189. }