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

C++ Builder

  1. #include <windows.h>  
  2. #include "Crosshatch_Box.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 HENHMETAFILE hMetaFile = NULL;
  88.    switch( uMsg )
  89.    {
  90.       case WM_CREATE  : 
  91.               {
  92.                  HDC  hMetaDC;
  93.                  HDC  hRefDC;
  94.                  RECT rect;
  95.                  int  iWidthMM, iHeightMM, iWidthPels, iHeightPels;
  96.                  int  iMMPerPelX, iMMPerPelY;
  97.                  hRefDC = GetDC( hWnd );
  98.                  // Compute the size of the client area in 0.01 mm.
  99.                  //................................................
  100.                  iWidthMM    = GetDeviceCaps(hRefDC, HORZSIZE);
  101.                  iHeightMM   = GetDeviceCaps(hRefDC, VERTSIZE);
  102.                  iWidthPels  = GetDeviceCaps(hRefDC, HORZRES);
  103.                  iHeightPels = GetDeviceCaps(hRefDC, VERTRES);
  104.                  iMMPerPelX = (iWidthMM * 100)/iWidthPels;
  105.                  iMMPerPelY = (iHeightMM * 100)/iHeightPels;
  106.                  GetClientRect(hWnd, &rect);
  107.                  rect.right = rect.right * iMMPerPelX;
  108.                  rect.bottom = rect.bottom * iMMPerPelY;
  109.       
  110.                  // Create an enhanced metafile.
  111.                  //.............................
  112.                  hMetaDC = CreateEnhMetaFile( hRefDC, NULL, &rect, 
  113.                                               "Test Metafile" );
  114.                  // Draw on the metafile.
  115.                  //......................
  116.                  if ( hMetaDC )
  117.                  {
  118.                     HBRUSH hBrush;
  119.                     GdiComment( hMetaDC, 23, "This is for an example." );
  120.                     hBrush = CreateHatchBrush( HS_DIAGCROSS, RGB(0, 0, 255) );
  121.                     SelectObject( hMetaDC, hBrush );
  122.                     Rectangle( hMetaDC, 10, 10, 200, 200 );
  123.                     hMetaFile = CloseEnhMetaFile( hMetaDC );
  124.                     DeleteObject( hBrush );
  125.                  }
  126.                
  127.                  ReleaseDC( hWnd, hRefDC );
  128.               }
  129.               break;
  130.       case WM_PAINT   : 
  131.               {
  132.                  PAINTSTRUCT ps;
  133.                  RECT        rect;
  134.                  BeginPaint( hWnd, &ps );
  135.                  GetClientRect( hWnd, &rect );
  136.                  PlayEnhMetaFile( ps.hdc, hMetaFile, &rect );
  137.                  EndPaint( hWnd, &ps );
  138.               }
  139.               break;
  140.       case WM_COMMAND :
  141.               switch( LOWORD( wParam ) )
  142.               {
  143.                  case IDM_ABOUT :
  144.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  145.                         break;
  146.                  case IDM_EXIT :
  147.                         DestroyWindow( hWnd );
  148.                         break;
  149.               }
  150.               break;
  151.       
  152.       case WM_DESTROY :
  153.               DeleteEnhMetaFile( hMetaFile );
  154.               PostQuitMessage(0);
  155.               break;
  156.       default :
  157.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  158.    }
  159.    return( 0L );
  160. }
  161. LRESULT CALLBACK About( HWND hDlg,           
  162.                         UINT message,        
  163.                         WPARAM wParam,       
  164.                         LPARAM lParam)
  165. {
  166.    switch (message) 
  167.    {
  168.        case WM_INITDIALOG: 
  169.                return (TRUE);
  170.        case WM_COMMAND:                              
  171.                if (   LOWORD(wParam) == IDOK         
  172.                    || LOWORD(wParam) == IDCANCEL)    
  173.                {
  174.                        EndDialog(hDlg, TRUE);        
  175.                        return (TRUE);
  176.                }
  177.                break;
  178.    }
  179.    return (FALSE); 
  180. }