Available in the LEADTOOLS Imaging toolkit. |
ODBC Example for C++ 4.0 and later
Take the following steps to create and use a simple database, containing only images. (For a more sophisticated approach, refer to Using ODBC to Access Image Data.)
Note: The LEADTOOLS ActiveX requires ODBC drivers that can properly handle long binary fields. Examples have been tested and will work with version 3.0 of the Microsoft ODBC Desktop Driver Pack. (This driver pack ships with Access 95 and works on 32-bit systems.) You can download drivers from the LEAD BBS or CompuServe forum. The CompuServe forum is at GO LEADTECH. The web page is ftp://ftp.leadtools.com/pub/utils/ODBC32.zip. The file to download is ODBC32.ZIP.
1. Start with a copy of the project that you created in Loading and Displaying an Image.
Note: This example changes the initialization of the window; so using a copy of the original project prevents problems with later reuse of the original project.
2. Create an ODBC data source, and name it LEADACCESS. The data source should have one long binary field, the table name should be ltvcpic and the field name should be photo. For a description of how to create an appropriate data source, refer to Using ODBC to Access Image Data.
3. Edit the TUTORDLG.CPP member function CTutorDlg::OnInitDialog. After the comment // TODO: Add extra initialization here, replace the code with the following:
// Position and size the main form so that it takes up most of the screen.
float Width = GetSystemMetrics(SM_CXSCREEN) * 0.9f;
float Height = GetSystemMetrics(SM_CYSCREEN) * 0.8f;
float Left = (GetSystemMetrics(SM_CXSCREEN) - Width) / 2.0f;
float Top = (GetSystemMetrics(SM_CYSCREEN) - Height) / 2.0f;
MoveWindow((int) Left, (int) Top, (int) Width, (int) Height);
m_Lead1.SetAutoRepaint(FALSE); // Take control of when the image paints
m_Lead1.SetPaintPalette(PAINTPALETTE_AUTO);
m_Lead1.SetEnableMethodErrors(FALSE);
// Make first param "ODBC;" to select data source from a listbox.
int nRet = m_Lead1.dbOpen("ODBC;DSN=LEADACCESS", "ltvcpic", "photo", 0);
if (nRet)
{
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
if (m_Lead1.GetDbIsEOF() && m_Lead1.GetDbIsBOF())
{
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
}
m_Lead1.dbMoveFirst();
m_Lead1.SetDbLoadBits(0);
m_Lead1.SetDbLockingMode(DB_LOCKINGMODE_OPTIMISTIC);
ImageInit();
}
return TRUE;
4. Edit the TUTORDLG.H file. After the line DECLARE_MESSAGE_MAP() in the class CTutorDlg, add the following line:
VOID ImageInit();
5. Edit the TUTORDLG.CPP file. Add the following member function.
VOID CTutorDlg::ImageInit()
{
CWaitCursor wait; // display hourglass
float HeightFactor;
float WidthFactor;
float HeightAllowed;
float WidthAllowed;
float Width = GetSystemMetrics(SM_CXSCREEN) * 0.9f;
float Height = GetSystemMetrics(SM_CYSCREEN) * 0.8f;
float Left = (GetSystemMetrics(SM_CXSCREEN) - Width) / 2.0f;
float Top = (GetSystemMetrics(SM_CYSCREEN) - Height) / 2.0f;
if (m_Lead1.GetBitmap() == 0)
{
m_Lead1.ForceRepaint();
return;
}
// Set the variables used for preserving the aspect ratio.
// Allow for a border of 1/8 of the form size.
// The units of measure do not matter, since we are calculating proportions.
HeightFactor = m_Lead1.GetBitmapHeight();
WidthFactor = m_Lead1.GetBitmapWidth();
CRect rcWindow;
GetClientRect(rcWindow);
HeightAllowed = rcWindow.Height() * 3.0f / 4;
WidthAllowed = rcWindow.Width() * 3.0f / 4;
// Center the LEAD control on the form, preserving the aspect ratio.
// Check to see if using the maximum width will make the image too tall.
// Set the dimensions based on the result.
if(((WidthAllowed * HeightFactor) / WidthFactor) < HeightAllowed)
{
Left = rcWindow.Width() / 8.0f;
Width = WidthAllowed;
Height = (Width * HeightFactor) / WidthFactor;
Top = (rcWindow.Height() - Height) / 2;
}
else
{
Top = rcWindow.Height() / 8.0f;
Height = HeightAllowed;
Width = (Height * WidthFactor) / HeightFactor;
Left = (rcWindow.Width() - Width) / 2;
}
rcWindow.SetRect(0, 0, (int) Width, (int) Height);
rcWindow.OffsetRect((int) Left, (int) Top);
m_Lead1.MoveWindow(rcWindow);
// Turn off scroll bars to make sure we use the full client area.
m_Lead1.SetAutoScroll(FALSE);
// Set the image display size to match the LEAD control
m_Lead1.SetDstRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
m_Lead1.SetDstClipRect(0.0f, 0.0f, m_Lead1.GetScaleWidth(), m_Lead1.GetScaleHeight());
// Display the image
m_Lead1.ForceRepaint(); // Repaint the image
}
6. In this step and the next one, change the resource file to add some buttons. For the first button, do the following:
a. Click on the ResourceView tab of the Project Workspace.
b. Double-click on the tutor resources folder.
c. Double-click on Dialog.
d. Double-click on IDD_TUTOR_DIALOG.
e. Select the command button control on the Controls toolbar.
f. Click and drag on the dialog window to create and position the button.
g. Double-click the button just created to bring up the Push Button Properties window.
h. Change the ID to IDC_ADDNEW and the caption to Add New.
i. Close the window.
7. For the other buttons, repeat Step 6 with the following differences for each button:
- With an ID of IDC_LOAD and caption Load
- With an ID of IDC_UPDATE and caption Update
- With an ID of IDC_DELETE and caption Delete
- With an ID of IDC_FIRST and caption |<
- With an ID of IDC_PREV and caption <
- With an ID of IDC_NEXT and caption >
- With an ID of IDC_LAST and caption >|
8. Select the CANCEL button and press DEL to remove it.
9. Double-click the OK button and rename it to Exit.
10. Press CTRL-F4 twice and answer YES to the message to save.
11. Add code for the IDC_FIRST button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_FIRST.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnFirst).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnFirst()
{
CWaitCursor wait; // display hourglass
if (m_Lead1.GetDbIsEOF() && m_Lead1.GetDbIsBOF())
{
MessageBox("The database is empty", "NOTICE", MB_OK);
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
}
else
{
m_Lead1.dbMoveFirst();
// Viewed image is in the database (after repaint)
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->SetWindowText("Update");
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
}
ImageInit();
}
12. Add code for the IDC_PREV button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_PREV.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnPrev).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnPrev()
{
CWaitCursor wait; // display hourglass
if (!m_Lead1.GetDbIsBOF())
{
m_Lead1.dbMovePrev();
// Viewed image is in the database (after repaint)
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->SetWindowText("Update");
}
// don't combine with above as an else case!
if (m_Lead1.GetDbIsBOF()) // BOF
{
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
if (!m_Lead1.GetDbIsEOF())
{
m_Lead1.dbMoveNext(); // move off BOF
MessageBox("Already at first record", "NOTICE", MB_OK);
}
else // BOF && EOF
{
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
}
}
else
{
GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
ImageInit();
}
}
13. Add code for the IDC_NEXT button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_NEXT.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnNext).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnNext()
{
CWaitCursor wait; // display hourglass
if (!m_Lead1.GetDbIsEOF())
{
m_Lead1.dbMoveNext();
// Viewed image is in the database (after repaint)
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->SetWindowText("Update");
}
// don't combine with above as an else case!
if (m_Lead1.GetDbIsEOF()) // EOF
{
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
if (!m_Lead1.GetDbIsBOF())
{
m_Lead1.dbMovePrev(); // move back from EOF
MessageBox("Already at last record", "NOTICE", MB_OK);
}
else // EOF && BOF
{
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
}
}
else
{
GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
ImageInit();
}
}
14. Add code for the IDC_LAST button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_LAST.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnLast).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnLast()
{
CWaitCursor wait; // display hourglass
if (m_Lead1.GetDbIsEOF() && m_Lead1.GetDbIsBOF())
{
MessageBox("The database is empty", "NOTICE", MB_OK);
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
}
else
{
m_Lead1.dbMoveLast();
// Viewed image is in the database (after repaint)
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->SetWindowText("Update");
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
}
ImageInit();
}
15. Add code for the IDC_DELETE button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_DELETE.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnDelete).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnDelete()
{
CWaitCursor wait; // display hourglass
char buffer[10];
if (m_Lead1.GetDbCanUpdate() == FALSE)
{
MessageBox("You cannot update this database.", "ERROR");
return;
}
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->GetWindowText(buffer, 7);
// Update button's text will be UPDATE if an image has been loaded but
// not updated.
if (isupper(buffer[1]))
{
MessageBox("Viewed image is not in database", "ERROR", MB_OK);
// User can do a MoveNext and MovePrev to see what the current image
// currently is.
return;
}
if (m_Lead1.GetDbIsEOF() && m_Lead1.GetDbIsBOF())
{
MessageBox("The database is empty", "NOTICE", MB_OK);
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
}
else if (!m_Lead1.GetDbIsBOF() && !m_Lead1.GetDbIsEOF())
{
if (m_Lead1.GetDbEditMode() == DB_EDITMODE_ADDNEW)
{
// User selected Add New and then Delete without an Update.
// Update the empty record first before deleting it.
int nRet = m_Lead1.dbUpdate(FILE_CMP, 24, QFACTOR_PQ1);
if (!nRet)
{
m_Lead1.dbMoveLast(); // new records are added to the end
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
}
}
m_Lead1.dbDelete();
if (m_Lead1.GetDbIsDeleted() == FALSE)
{
MessageBox("Unable to delete record!", "ERROR");
return;
}
if (m_Lead1.GetDbCanRestart() == TRUE)
{
m_Lead1.dbRequery(); // moves you to the first image in the database
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
if (m_Lead1.GetDbIsEOF())
{
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
}
else
{
GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
}
}
else
{
m_Lead1.dbMovePrev();
GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
if (m_Lead1.GetDbIsBOF()) // just before first record
{
m_Lead1.dbMoveNext(); // move forward to first record
GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
if (m_Lead1.GetDbIsEOF()) // just past last record
{
// no more records left
MessageBox("The database is empty", "NOTICE", MB_OK);
m_Lead1.ForceRepaint();
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
}
}
}
ImageInit();
// Viewed image is in the database (after repaint)
pButton->SetWindowText("Update");
}
}
16. Add code for the IDC_LOAD button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_LOAD.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnLoad).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnLoad()
{
CFileDialog OpenFile(TRUE, NULL, "*.*");
if (OpenFile.DoModal() == IDOK)
{
CWaitCursor wait; // display hourglass
int nRet = m_Lead1.Load(OpenFile.GetPathName(), 0, 0, 1);
if (!nRet)
{
ImageInit();
// Remind user that the image has not been updated in the database.
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->SetWindowText("UPDATE");
}
else
{
AfxMessageBox("Error loading image.");
if (m_Lead1.GetDbEditMode() == DB_EDITMODE_ADDNEW)
OnLast(); // cancel Add New
}
}
}
17. Add code for the IDC_UPDATE button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_UPDATE.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnUpdate).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnUpdate()
{
CWaitCursor wait; // display hourglass
int nRet, nEditMode;
if (m_Lead1.GetDbCanUpdate() == FALSE)
{
MessageBox("You cannot update this database.", "ERROR");
return;
}
// Save Edit mode in a temp variable since dbUpdate() clears the edit mode.
nEditMode = m_Lead1.GetDbEditMode();
if ((nEditMode == DB_EDITMODE_NONE) &&
!m_Lead1.GetDbIsBOF() && !m_Lead1.GetDbIsEOF())
m_Lead1.dbEdit();
if (m_Lead1.GetDbEditMode() != DB_EDITMODE_NONE)
{
// dbUpdate() clears the EditMode flag.
nRet = m_Lead1.dbUpdate(FILE_CMP, 24, QFACTOR_PQ1);
// Viewed image is in the database (after repaint)
CWnd *pButton = GetDlgItem(IDC_UPDATE);
pButton->SetWindowText("Update");
if (!nRet)
{
if (nEditMode == DB_EDITMODE_ADDNEW)
{
m_Lead1.dbMoveLast();
GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
}
}
else
{
char buffer[50];
sprintf(buffer, "LEAD Error %d saving image to database.", nRet);
MessageBox(buffer, "ERROR", MB_OK);
}
}
else
{
MessageBox("Sequence Error - no record to update.", "ERROR", MB_OK);
}
}
18. Add code for the IDC_ADDNEW button as follows:
a. Press Ctrl-W to go to the MFC Class Wizard.
b. In the Class name combo box, select CTutorDlg.
c. In the Object IDs list box, select IDC_ADDNEW.
d. In the Messages list box, select BN_CLICKED.
e. Click the Add function button. Choose OK for the default function name (OnAddnew).
f. Click the Edit Code button to start entering the code.
g. Edit the function to appear as follows:
void CTutorDlg::OnAddnew()
{
CWaitCursor wait; // display hourglass
// TODO: Add your control notification handler code here
if (m_Lead1.GetDbCanAppend() == FALSE)
{
MessageBox("You cannot append to this database.", "ERROR");
return;
}
m_Lead1.dbAddNew();
m_Lead1.ForceRepaint(); // no image for a new record
wait.Restore();
OnLoad();
}
19. Open the ClassWizard (CTRL-W), and do the following:
a. In the Class name combo box, select CTutorDlg.
b. In the Object IDs list box, select CTutorDlg.
c. In the Messages list box, select WM_DESTROY.
d. Click the Add function button. Choose OK for the default function name (OnDestroy).
e. Click the Edit Code button to start entering the code.
f. Edit the function to appear as follows:
void CTutorDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
m_Lead1.dbClose();
}
20. Rebuild and run the application.