📄 lobsampledlg.cpp
字号:
/************************************************************
@author Chandar
@version 1.0
Development Environment : Visual C++ 6.0
Name of the File : LOBSampleDlg.cpp
Creation/Modification History :
01-Aug-2001 Created
File Overview
This file implements the class CLOBSampleDlg used to handle the
dialog box function and events
***************************************************************/
#include "stdafx.h"
#include "LOBSample.h"
#include "LOBSampleDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLOBSampleDlg dialog
/*********************
Constructor definition
**********************/
CLOBSampleDlg::CLOBSampleDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLOBSampleDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLOBSampleDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
//Set the variables for existing and new picture to empty string
m_ExistingPicture="";
m_NewPicture="";
}
void CLOBSampleDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLOBSampleDlg)
DDX_Control(pDX, IDC_COMBOPRODUCT, m_product);
// DDX_Control(pDX, IDC_NEWPICTURE, m_newPicture);
// DDX_Control(pDX, IDC_EXISTPICTURE, m_ExistPicture);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLOBSampleDlg, CDialog)
//{{AFX_MSG_MAP(CLOBSampleDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_BROWSE, OnButtonBrowse)
ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate)
ON_CBN_SELCHANGE(IDC_COMBOPRODUCT, OnSelchangeComboproduct)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLOBSampleDlg message handlers
/********************************************************************
This function is called just before displaying the dialog box to
initialize dialog box
It calls the functions to retrieve the product names
from database and displays them in list box on dialog box
********************************************************************/
BOOL CLOBSampleDlg::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
// TODO: Add extra initialization here
//Open the connection to database using ADODBHandler object
BOOL check=dbObj.OpenDatabase();
if (check==TRUE) //if connected
{
try
{
//Get the database function to get product names and ids
_RecordsetPtr pnames= dbObj.GetProductNames();
if (pnames!=NULL)
//Loop till the end of recordset
while (!pnames->GetadoEOF())
{
//Get the product name. It is string type
_bstr_t name=pnames->Fields->GetItem("product_name")->GetValue().bstrVal;
//Get the product id. It is integer type
long productId=pnames->Fields->GetItem("product_id")->GetValue().lVal;
//Add the product name to the list box on dialog box
int index=m_product.AddString(name);
//Add the product id for above name as item data of list box item
//This id is returned when list box item is selected
m_product.SetItemData(index,productId);
//Move to next row in the recordset
pnames->MoveNext();
}
//Close the database connection
dbObj.CloseDatabase();
}
//Catch COM errors
catch( _com_error &e)
{
// get info from _com_error
_bstr_t error="Source: " + e.Source() + "Description: "+ e.Description();
MessageBox(error,"Error");
EndDialog(0); //End the dialog on error
}
//Catch unhandled exception
catch(...)
{
MessageBox("Unhandled Exception","Error");
EndDialog(0);
}
}
//if cannot connect to database, display message
else
{
MessageBox("Cannot Connect to Database","Connection Error");
EndDialog(0);
}
return TRUE; // return TRUE unless you set the focus to a control
}
/***************************************************************
This function is called whenever dialog box needs repainting
ie whenever it is shown or resized or uncovered by other windows
It is used to paint the existing and new image of the
product on the dialog box
****************************************************************/
void CLOBSampleDlg::OnPaint()
{
CPaintDC dc(this);
if (IsIconic())
{
// 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);
}
//Redraw the dialog box to show the image on it
else
{
//check is existing picture is not empty
if (m_ExistingPicture!=(_bstr_t)"")
{
//Set the rectangular area of dialog box foe existing image
bounds.SetRect(213,92,420,280);
//Load the image from image file
image.Open(m_ExistingPicture);
//Draw the image on dialog box
image.DrawImage(&dc, bounds);
}
//check if new picture is selected
if (m_NewPicture!=(_bstr_t)"" && m_flag==FALSE)
{
//Load the new picture from file
image.Open(m_NewPicture);
//Set the rectangular area for drawing new image
bounds.SetRect(213,305,420,496);
//Draw the image
image.DrawImage(&dc, bounds);
}
CDialog::OnPaint();
}
}
/****************************************************************************
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
*****************************************************************************/
HCURSOR CLOBSampleDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
/*****************************************************************************
This function defines the message handler for click event of the Browse button
on dialog box.
******************************************************************************/
void CLOBSampleDlg::OnButtonBrowse()
{
//create a common dialog box object. The parameter TRUE is for Open dialog
// box. FALSE indicates SAVE dialog box
//set the files to be shown by Open File Dialog box
static char BASED_CODE filter[] =
"Image Files (*.bmp, *.jpg,*.gif)"\
"|*.bmp;*.jpg;*.gif||";
CFileDialog cfdlg(TRUE,0, 0,0,filter, this);
//Display the dialog box and get return value
if (cfdlg.DoModal()==IDOK)
{
//Get the name of selected file into m_NewPicture variable
m_NewPicture =cfdlg.GetPathName();
//Set m_flag to false
m_flag=FALSE;
//Call the paint function of dialog box. FALSE indicates only some areas
//need repainting. TRUE repaints whole dialog box
Invalidate(FALSE);
}
}
/****************************************************************************
This function defines the message handler for the Update button on the dialog
box. It updates the new image for the selected product into the database
****************************************************************************/
void CLOBSampleDlg::OnButtonUpdate()
{
//Check if user has selected a product and new image file
if(m_product.GetCurSel()==CB_ERR || m_NewPicture==(_bstr_t)"")
MessageBox("Select a Product and New Image");
else
{
//Get the product id of the selected product
int productid = m_product.GetItemData(m_product.GetCurSel());
//Open database connection
BOOL check=dbObj.OpenDatabase();
if (check==TRUE) //if connected
{
//update the image for the selected product
BOOL update=dbObj.SetImage(productid,m_NewPicture);
}
//if cannot connect to database display message
else
MessageBox("Cannot Connect to Database","Connection Error");
}
m_NewPicture="";
}
/**************************************************************************
This function defines the message handler for selection change event of the
list box. It gets the product from list box and displays the existing image
from database onto the dialog box
**************************************************************************/
void CLOBSampleDlg::OnSelchangeComboproduct()
{
if(m_ExistingPicture!=(_bstr_t)"")
{
//Delete the temporary file created to dislay the image if it exists
CFile::Remove(m_ExistingPicture);
//Set existing picture file string to empty string
m_ExistingPicture="";
}
//Open database connection
BOOL check=dbObj.OpenDatabase();
if (check==TRUE) //if connected
{
BOOL getimagecheck;
//Get the product id of the selected product
int productid=m_product.GetItemData(m_product.GetCurSel());
try
{
//Get the image for seleted product from database
getimagecheck= dbObj.GetImage(productid);
if (!getimagecheck) //if false
{
MessageBox("Image does not exists");
m_ExistingPicture="";
}
else
m_ExistingPicture = dbObj.strCurAppPath +"image.bmp";
//Set the flag for painting the dialog box
m_flag=TRUE;
}
//Catch COM errors
catch( _com_error &e)
{
// get info from _com_error
_bstr_t error="Source: " + e.Source() + "Description: "+ e.Description();
MessageBox(error,"Error");
EndDialog(0);
}
//Catch Unhandled exceptions
catch(...)
{
MessageBox("Unhandled Exception","Error");
EndDialog(0);
}
//Close database connection
dbObj.CloseDatabase();
if (m_NewPicture==(_bstr_t("")))
{
//define a rectangular area covering new and old image frames
bounds.SetRect(212,92,421,494);
}
else
{
//Define a rectangular area covering old image frame
bounds.SetRect(212,92,421,280);
}
//Paint the defined rect on dialog box
InvalidateRect(bounds);
}
//if not connected to database, display message
else
{
MessageBox("Cannot Connect to Database","Connection Error");
}
}
/*********************************************************
This message handler is called when Exit button on dialog
box is clicked.
*********************************************************/
void CLOBSampleDlg::OnCancel()
{
// TODO: Add extra cleanup here
if(m_ExistingPicture!=(_bstr_t)"")
{
//Delete the temporary file created to dislay the image if it exists
CFile::Remove(m_ExistingPicture);
//Set existing picture file string to empty string
m_ExistingPicture="";
}
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -