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

C++ Builder

  1. #include <windows.h>  
  2. #include "Draw_2_Boxes.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. static HPALETTE hPalette;
  88. static int      nColorData;
  89.    switch( uMsg )
  90.    {
  91.       case WM_CREATE  :
  92.               {
  93.                  HANDLE       hRes, hPal;
  94.                  LPBITMAPINFO lpBi;
  95.                  LPLOGPALETTE lpPal;
  96.                  int          i;
  97.                  // Load the bitmap.
  98.                  //.................
  99.                  hRes = LoadResource( hInst, 
  100.                             FindResource( hInst, "testdib", RT_BITMAP ) );
  101.                  lpBi = (LPBITMAPINFO)LockResource( hRes );
  102.                  // Find out how many colors we need.
  103.                  //..................................
  104.                  if ( lpBi->bmiHeader.biClrUsed != 0 )
  105.                     nColorData = lpBi->bmiHeader.biClrUsed;
  106.                  else
  107.                     switch( lpBi->bmiHeader.biBitCount )
  108.                     {
  109.                        case 1  : nColorData = 2;   break; // Monochrome
  110.                        case 4  : nColorData = 16;  break; // VGA
  111.                        case 8  : nColorData = 256; break; // SVGA
  112.                        case 24 : nColorData = 0;   break; // True Color
  113.                     }
  114.                  // Allocate memory for color palette.
  115.                  //...................................
  116.                  hPal = GlobalAlloc( GHND, sizeof(LOGPALETTE)+
  117.                                         (nColorData * sizeof(PALETTEENTRY)) );
  118.                  lpPal = (LPLOGPALETTE)GlobalLock( hPal );
  119.                  lpPal->palVersion    = 0x300;      
  120.                  lpPal->palNumEntries = nColorData;
  121.                  // Load each color into the palette.
  122.                  //..................................
  123.                  for ( i = 0; i < nColorData; i++ )
  124.                  {
  125.                     lpPal->palPalEntry[i].peRed   = lpBi->bmiColors[i].rgbRed;
  126.                     lpPal->palPalEntry[i].peGreen = 
  127.                                                   lpBi->bmiColors[i].rgbGreen;
  128.                     lpPal->palPalEntry[i].peBlue  = 
  129.                                                   lpBi->bmiColors[i].rgbBlue;
  130.                  }
  131.                  // Create the Palette.
  132.                  //....................
  133.                  hPalette = CreatePalette( lpPal );
  134.                  GlobalUnlock( hPal );
  135.                  GlobalFree( hPal );
  136.               }
  137.               break;
  138.       case WM_PAINT :
  139.               {
  140.                  PAINTSTRUCT  ps;
  141.                  LPBITMAPINFO lpBi;
  142.                  HANDLE       hRes;
  143.                  LPTSTR       lpBits;
  144.                  BeginPaint( hWnd, &ps );
  145.                  // Select palette and realize it.
  146.                  //...............................
  147.                  SelectPalette( ps.hdc, hPalette, FALSE );
  148.                  RealizePalette( ps.hdc );
  149.                  // Load the bitmap.
  150.                  //.................
  151.                  hRes = LoadResource( hInst, 
  152.                             FindResource( hInst, "testdib", RT_BITMAP ) );
  153.                  lpBi = (LPBITMAPINFO)LockResource( hRes );
  154.                  // Locate the bitmap data.
  155.                  //........................
  156.                  lpBits =  (LPSTR) lpBi;
  157.                  lpBits +=  lpBi->bmiHeader.biSize + 
  158.                             ( nColorData*sizeof(RGBQUAD) );
  159.                  // Paint the bitmap normal size.
  160.                  //..............................
  161.                  SetDIBitsToDevice( ps.hdc, 10, 10, 
  162.                                             lpBi->bmiHeader.biWidth,
  163.                                             lpBi->bmiHeader.biHeight,
  164.                                             0, 0,
  165.                                             0, lpBi->bmiHeader.biHeight,
  166.                                             lpBits, lpBi, DIB_RGB_COLORS );
  167.                  // Paint the bitmap 200% size.
  168.                  //............................
  169.                  StretchDIBits( ps.hdc, 40+lpBi->bmiHeader.biWidth, 10,
  170.                                         lpBi->bmiHeader.biWidth*2,
  171.                                         lpBi->bmiHeader.biHeight*2,
  172.                                         0, 0,
  173.                                         lpBi->bmiHeader.biWidth,
  174.                                         lpBi->bmiHeader.biHeight,
  175.                                         lpBits, lpBi, DIB_RGB_COLORS, 
  176.                                         SRCCOPY );
  177.                  EndPaint( hWnd, &ps );
  178.               }
  179.               break;
  180.       case WM_COMMAND :
  181.               switch( LOWORD( wParam ) )
  182.               {
  183.                  case IDM_ABOUT :
  184.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  185.                         break;
  186.                  case IDM_EXIT :
  187.                         DestroyWindow( hWnd );
  188.                         break;
  189.               }
  190.               break;
  191.       
  192.       case WM_DESTROY :
  193.               DeleteObject( hPalette );
  194.               PostQuitMessage(0);
  195.               break;
  196.       default :
  197.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  198.    }
  199.    return( 0L );
  200. }
  201. LRESULT CALLBACK About( HWND hDlg,           
  202.                         UINT message,        
  203.                         WPARAM wParam,       
  204.                         LPARAM lParam)
  205. {
  206.    switch (message) 
  207.    {
  208.        case WM_INITDIALOG: 
  209.                return (TRUE);
  210.        case WM_COMMAND:                              
  211.                if (   LOWORD(wParam) == IDOK         
  212.                    || LOWORD(wParam) == IDCANCEL)    
  213.                {
  214.                        EndDialog(hDlg, TRUE);        
  215.                        return (TRUE);
  216.                }
  217.                break;
  218.    }
  219.    return (FALSE); 
  220. }