home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 25: Programming / pc_actual_25.iso / Basic / GridOne / setup.EXE / BINARYDATADLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-09  |  6.6 KB  |  235 lines

  1. // BinaryDataDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "BinaryData.h"
  6. #include "BinaryDataDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14.  
  15. LPCTSTR g_sampleDescription = 
  16.    _T("This sample demonstrates how to display images when image itself ")
  17.    _T("is stored as a cell value. The First two rows display images retrieved from ")
  18.    _T("the module's resources. Third row demonstrates how to display an image ")
  19.    _T("from the image list. Click it to change an image.")
  20.    _T("");
  21.  
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CBinaryDataDlg dialog
  25.  
  26. CBinaryDataDlg::CBinaryDataDlg(CWnd* pParent /*=NULL*/)
  27.     : CDialog(CBinaryDataDlg::IDD, pParent)
  28. {
  29.     //{{AFX_DATA_INIT(CBinaryDataDlg)
  30.     m_description = g_sampleDescription;
  31.     //}}AFX_DATA_INIT
  32.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  33.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  34. }
  35.  
  36. void CBinaryDataDlg::DoDataExchange(CDataExchange* pDX)
  37. {
  38.     CDialog::DoDataExchange(pDX);
  39.     //{{AFX_DATA_MAP(CBinaryDataDlg)
  40.     DDX_Text(pDX, IDC_DESCR, m_description);
  41.     //}}AFX_DATA_MAP
  42.    DDX_BeeGrid(pDX, IDC_GRID1, m_grid);
  43. }
  44.  
  45. BEGIN_MESSAGE_MAP(CBinaryDataDlg, CDialog)
  46.     //{{AFX_MSG_MAP(CBinaryDataDlg)
  47.     ON_WM_PAINT()
  48.     ON_WM_QUERYDRAGICON()
  49.     //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51.  
  52. /////////////////////////////////////////////////////////////////////////////
  53. // CBinaryDataDlg message handlers
  54.  
  55. BOOL CBinaryDataDlg::OnInitDialog()
  56. {
  57.     CDialog::OnInitDialog();
  58.  
  59.     // Set the icon for this dialog.  The framework does this automatically
  60.     //  when the application's main window is not a dialog
  61.     SetIcon(m_hIcon, TRUE);            // Set big icon
  62.     SetIcon(m_hIcon, FALSE);        // Set small icon
  63.     
  64.    //
  65.     // Initialize BeeGrid
  66.    //
  67.  
  68.    try
  69.    {
  70.       // Create image list
  71.       CImageList images;
  72.       images.Create(IDB_IMAGELIST, 16, 0, CLR_DEFAULT);
  73.       m_grid.intf->PutImages(long(images.GetSafeHandle()));
  74.  
  75.       m_grid.intf->AllowEdit         = VARIANT_FALSE;
  76.       m_grid.intf->AllowAddNew       = VARIANT_FALSE;
  77.       m_grid.intf->AllowDelete       = VARIANT_FALSE;
  78.       m_grid.intf->FitLastColumn     = VARIANT_TRUE;
  79.       m_grid.intf->GroupByBoxVisible = VARIANT_FALSE;
  80.  
  81.       // We will display three images in three rows
  82.       const int rowCount = 3;
  83.       m_grid.intf->DataRowCount = rowCount;
  84.  
  85.       // Add two columns
  86.       IsgColumnsPtr spColumns = m_grid.intf->Columns;
  87.       spColumns->RemoveAll(VARIANT_FALSE);
  88.  
  89.       IsgColumnPtr spCol = spColumns->Add(L"Type");
  90.       spCol->Caption = L"Type";
  91.  
  92.       // Initialize image column
  93.       spCol = spColumns->Add(L"Image");
  94.       spCol->Caption = L"Resource Images";
  95.       spCol->DataType = sgtBinary;
  96.       spCol->HeadingStyle->TextAlignment = sgAlignCenterCenter;
  97.       spCol->Style->DisplayType = sgDisplayPicture;
  98.       spCol->Style->PictureAlignment = sgPicAlignStretch;
  99.  
  100.       m_grid.intf->Rows->At(3L)->Cells->Item[2]->Style->PictureAlignment = sgPicAlignCenterCenter;
  101.  
  102.       // Initialize row height
  103.       m_grid.intf->Rows->At(1L)->Height = 50;
  104.       m_grid.intf->Rows->At(2L)->Height = 130;
  105.       m_grid.intf->Rows->At(3L)->Height = 30;
  106.       
  107.       // Fill data
  108.       IsgArrayPtr spArray = m_grid.intf->Array;
  109.  
  110.       _variant_t image;
  111.  
  112.       spArray->Value[0][0] = L"BMP Image";
  113.       if (ReadResourceImage(RT_BITMAP, MAKEINTRESOURCE(IDB_BITMAP1), image))
  114.          spArray->Value[0][1] = image;
  115.  
  116.       spArray->Value[1][0] = L"WMF Image";
  117.       if (ReadResourceImage(_T("WMF"), MAKEINTRESOURCE(IDR_WMF1), image))
  118.          spArray->Value[1][1] = image;
  119.  
  120.       spArray->Value[2][0] = L"Image List";
  121.       spArray->Value[2][1] = 1L;
  122.    }
  123.    catch (_com_error& e)
  124.    {
  125.       AfxMessageBox(e.Description());
  126.    }
  127.    
  128.     return TRUE;  // return TRUE  unless you set the focus to a control
  129. }
  130.  
  131.  
  132. bool CBinaryDataDlg::ReadResourceImage(LPCTSTR type, 
  133.                                        LPCTSTR name, 
  134.                                        _variant_t& image)
  135. {
  136.    image.Clear();
  137.    bool imageLoaded = false;
  138.  
  139.    HMODULE hModule = AfxGetResourceHandle();
  140.  
  141.    HRSRC hRsrc = ::FindResource(hModule, name, type);
  142.    if (hRsrc != 0)
  143.    {
  144.       HGLOBAL resource = ::LoadResource(hModule, hRsrc);
  145.       if (resource != 0)
  146.       {
  147.          void* pData = ::LockResource(resource);
  148.          if (pData != 0)
  149.          {
  150.             DWORD size = ::SizeofResource(hModule, hRsrc);
  151.  
  152.             // We have the buffer with the resource data
  153.             // Copy resource data to the SAFEARRAY of bytes
  154.  
  155.             SAFEARRAY* sa = ::SafeArrayCreateVector(VT_UI1, 0, size);
  156.             if (sa != 0)
  157.             {
  158.                ::SafeArrayLock(sa);
  159.                ::CopyMemory(sa->pvData, pData, size);
  160.  
  161.                image.vt = VT_ARRAY|VT_UI1;
  162.                image.parray = sa;
  163.                
  164.                imageLoaded = true;
  165.  
  166.                ::SafeArrayUnlock(sa);
  167.             }
  168.          }
  169.          
  170.          ::FreeResource(resource);
  171.       }
  172.    }
  173.    return imageLoaded;
  174. }
  175.  
  176. // If you add a minimize button to your dialog, you will need the code below
  177. //  to draw the icon.  For MFC applications using the document/view model,
  178. //  this is automatically done for you by the framework.
  179.  
  180. void CBinaryDataDlg::OnPaint() 
  181. {
  182.     if (IsIconic())
  183.     {
  184.         CPaintDC dc(this); // device context for painting
  185.  
  186.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  187.  
  188.         // Center icon in client rectangle
  189.         int cxIcon = GetSystemMetrics(SM_CXICON);
  190.         int cyIcon = GetSystemMetrics(SM_CYICON);
  191.         CRect rect;
  192.         GetClientRect(&rect);
  193.         int x = (rect.Width() - cxIcon + 1) / 2;
  194.         int y = (rect.Height() - cyIcon + 1) / 2;
  195.  
  196.         // Draw the icon
  197.         dc.DrawIcon(x, y, m_hIcon);
  198.     }
  199.     else
  200.     {
  201.         CDialog::OnPaint();
  202.     }
  203. }
  204.  
  205. // The system calls this to obtain the cursor to display while the user drags
  206. //  the minimized window.
  207. HCURSOR CBinaryDataDlg::OnQueryDragIcon()
  208. {
  209.     return (HCURSOR) m_hIcon;
  210. }
  211.  
  212. BEGIN_EVENTSINK_MAP(CBinaryDataDlg, CDialog)
  213.     //{{AFX_EVENTSINK_MAP(CBinaryDataDlg)
  214.     ON_EVENT(CBinaryDataDlg, IDC_GRID1, -600 /* Click */, OnClickGrid1, VTS_NONE)
  215.     //}}AFX_EVENTSINK_MAP
  216. END_EVENTSINK_MAP()
  217.  
  218. void CBinaryDataDlg::OnClickGrid1() 
  219. {
  220.    // Cycle images when cliked on the imagelist image
  221.  
  222.    if (m_grid.intf->Row == 3)
  223.    {
  224.       long imageIndex = m_grid.intf->Array->Value[2][1];
  225.  
  226.       if (imageIndex >= 24)
  227.          imageIndex = 1;
  228.       else
  229.          imageIndex += 1;
  230.  
  231.       m_grid.intf->Array->Value[2][1] = imageIndex;
  232.       m_grid.intf->Rows->At(3)->Redraw();
  233.    }
  234. }
  235.