home *** CD-ROM | disk | FTP | other *** search
- // BinaryDataDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "BinaryData.h"
- #include "BinaryDataDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
-
- LPCTSTR g_sampleDescription =
- _T("This sample demonstrates how to display images when image itself ")
- _T("is stored as a cell value. The First two rows display images retrieved from ")
- _T("the module's resources. Third row demonstrates how to display an image ")
- _T("from the image list. Click it to change an image.")
- _T("");
-
-
- /////////////////////////////////////////////////////////////////////////////
- // CBinaryDataDlg dialog
-
- CBinaryDataDlg::CBinaryDataDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CBinaryDataDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CBinaryDataDlg)
- m_description = g_sampleDescription;
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CBinaryDataDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CBinaryDataDlg)
- DDX_Text(pDX, IDC_DESCR, m_description);
- //}}AFX_DATA_MAP
- DDX_BeeGrid(pDX, IDC_GRID1, m_grid);
- }
-
- BEGIN_MESSAGE_MAP(CBinaryDataDlg, CDialog)
- //{{AFX_MSG_MAP(CBinaryDataDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CBinaryDataDlg message handlers
-
- BOOL CBinaryDataDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- //
- // Initialize BeeGrid
- //
-
- try
- {
- // Create image list
- CImageList images;
- images.Create(IDB_IMAGELIST, 16, 0, CLR_DEFAULT);
- m_grid.intf->PutImages(long(images.GetSafeHandle()));
-
- m_grid.intf->AllowEdit = VARIANT_FALSE;
- m_grid.intf->AllowAddNew = VARIANT_FALSE;
- m_grid.intf->AllowDelete = VARIANT_FALSE;
- m_grid.intf->FitLastColumn = VARIANT_TRUE;
- m_grid.intf->GroupByBoxVisible = VARIANT_FALSE;
-
- // We will display three images in three rows
- const int rowCount = 3;
- m_grid.intf->DataRowCount = rowCount;
-
- // Add two columns
- IsgColumnsPtr spColumns = m_grid.intf->Columns;
- spColumns->RemoveAll(VARIANT_FALSE);
-
- IsgColumnPtr spCol = spColumns->Add(L"Type");
- spCol->Caption = L"Type";
-
- // Initialize image column
- spCol = spColumns->Add(L"Image");
- spCol->Caption = L"Resource Images";
- spCol->DataType = sgtBinary;
- spCol->HeadingStyle->TextAlignment = sgAlignCenterCenter;
- spCol->Style->DisplayType = sgDisplayPicture;
- spCol->Style->PictureAlignment = sgPicAlignStretch;
-
- m_grid.intf->Rows->At(3L)->Cells->Item[2]->Style->PictureAlignment = sgPicAlignCenterCenter;
-
- // Initialize row height
- m_grid.intf->Rows->At(1L)->Height = 50;
- m_grid.intf->Rows->At(2L)->Height = 130;
- m_grid.intf->Rows->At(3L)->Height = 30;
-
- // Fill data
- IsgArrayPtr spArray = m_grid.intf->Array;
-
- _variant_t image;
-
- spArray->Value[0][0] = L"BMP Image";
- if (ReadResourceImage(RT_BITMAP, MAKEINTRESOURCE(IDB_BITMAP1), image))
- spArray->Value[0][1] = image;
-
- spArray->Value[1][0] = L"WMF Image";
- if (ReadResourceImage(_T("WMF"), MAKEINTRESOURCE(IDR_WMF1), image))
- spArray->Value[1][1] = image;
-
- spArray->Value[2][0] = L"Image List";
- spArray->Value[2][1] = 1L;
- }
- catch (_com_error& e)
- {
- AfxMessageBox(e.Description());
- }
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
-
- bool CBinaryDataDlg::ReadResourceImage(LPCTSTR type,
- LPCTSTR name,
- _variant_t& image)
- {
- image.Clear();
- bool imageLoaded = false;
-
- HMODULE hModule = AfxGetResourceHandle();
-
- HRSRC hRsrc = ::FindResource(hModule, name, type);
- if (hRsrc != 0)
- {
- HGLOBAL resource = ::LoadResource(hModule, hRsrc);
- if (resource != 0)
- {
- void* pData = ::LockResource(resource);
- if (pData != 0)
- {
- DWORD size = ::SizeofResource(hModule, hRsrc);
-
- // We have the buffer with the resource data
- // Copy resource data to the SAFEARRAY of bytes
-
- SAFEARRAY* sa = ::SafeArrayCreateVector(VT_UI1, 0, size);
- if (sa != 0)
- {
- ::SafeArrayLock(sa);
- ::CopyMemory(sa->pvData, pData, size);
-
- image.vt = VT_ARRAY|VT_UI1;
- image.parray = sa;
-
- imageLoaded = true;
-
- ::SafeArrayUnlock(sa);
- }
- }
-
- ::FreeResource(resource);
- }
- }
- return imageLoaded;
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CBinaryDataDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CBinaryDataDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- BEGIN_EVENTSINK_MAP(CBinaryDataDlg, CDialog)
- //{{AFX_EVENTSINK_MAP(CBinaryDataDlg)
- ON_EVENT(CBinaryDataDlg, IDC_GRID1, -600 /* Click */, OnClickGrid1, VTS_NONE)
- //}}AFX_EVENTSINK_MAP
- END_EVENTSINK_MAP()
-
- void CBinaryDataDlg::OnClickGrid1()
- {
- // Cycle images when cliked on the imagelist image
-
- if (m_grid.intf->Row == 3)
- {
- long imageIndex = m_grid.intf->Array->Value[2][1];
-
- if (imageIndex >= 24)
- imageIndex = 1;
- else
- imageIndex += 1;
-
- m_grid.intf->Array->Value[2][1] = imageIndex;
- m_grid.intf->Rows->At(3)->Redraw();
- }
- }
-