?? filesysdlg.cpp
字號:
// FileSysDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FileSys.h"
#include "FileSysDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ChunkSize 200
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileSysDlg dialog
CFileSysDlg::CFileSysDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileSysDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CFileSysDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CFileSysDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFileSysDlg)
DDX_Control(pDX, IDC_LIST_FILENAME, m_ListFileName);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CFileSysDlg, CDialog)
//{{AFX_MSG_MAP(CFileSysDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SAVE, OnButtonSave)
ON_BN_CLICKED(IDC_BUTTON_GET, OnButtonGet)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFileSysDlg message handlers
BOOL CFileSysDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 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
m_pCon.CreateInstance(__uuidof(Connection));
m_pRS.CreateInstance(__uuidof(Recordset));
//初始化數(shù)據(jù)庫連接
//m_pCon->ConnectionString="File Name=my_data4.udl";
m_pCon->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=FileDB;Data Source=(local)";
m_pCon->Open("","","",adModeUnknown);
//初始化List控件
m_ListFileName.InsertColumn(0,"ID",LVCFMT_LEFT,40,-1);
m_ListFileName.InsertColumn(1,"文件名",LVCFMT_LEFT,200,-1);
m_ListFileName.SetExtendedStyle(LVS_EX_FULLROWSELECT| LVS_EX_GRIDLINES);
RefreshList();
return TRUE; // return TRUE unless you set the focus to a control
}
void CFileSysDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// 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 CFileSysDlg::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 CFileSysDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFileSysDlg::OnButtonSave()
{
CFile file;
//BOOL bret;
try{
m_pRS->Open(_variant_t("tbFile"),
m_pCon.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdTable);
if(m_pRS->Fields->GetItem(long(2))->GetAttributes() & adFldLong)
{
CFileDialog fileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,"All Files(*.*)|*.*||",NULL);
if(fileDlg.DoModal()==IDOK)
{
if(file.Open(fileDlg.GetPathName(),CFile::modeRead))
{
long length = (long)file.GetLength();
BYTE *pbuf = new BYTE[length];
if (pbuf != NULL && file.Read(pbuf, length) == (DWORD)length)
{
m_pRS->AddNew();
m_pRS->Fields->GetItem(long(1))->PutValue(_variant_t(fileDlg.GetFileName()));
MyAppendChunk(FieldPtr(m_pRS->GetFields()->GetItem(long(2))), pbuf, length);
m_pRS->Update();
}
if (pbuf != NULL)
delete[] pbuf;
file.Close();
}
}
}
m_pRS->Close();
}
catch (_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
return;
}
RefreshList();
}
BOOL CFileSysDlg::MyAppendChunk(FieldPtr pField, LPVOID lpData, UINT nBytes)
{
SAFEARRAY FAR *pSafeArray = NULL;
SAFEARRAYBOUND rgsabound[1];
try
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nBytes;
pSafeArray = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nBytes; i++)
{
UCHAR &chData = ((UCHAR*)lpData)[i];
HRESULT hr = SafeArrayPutElement(pSafeArray, &i, &chData);
if (FAILED(hr)) return FALSE;
}
_variant_t varChunk;
varChunk.vt = VT_ARRAY | VT_UI1;
varChunk.parray = pSafeArray;
return (pField->AppendChunk(varChunk) == S_OK);
}
catch (_com_error &e)
{
TRACE(_T("Warning: AppendChunk 方法發(fā)生異常. 錯誤信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
return FALSE;
}
}
void CFileSysDlg::RefreshList()
{
_variant_t var;
CString strValue;
int curItem=0;
try
{
m_pRS->Open(_variant_t("tbFile"),
m_pCon.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdTable);
if(m_pRS->GetRecordCount()==0)
{m_pRS->Close();
return;
}
else{
m_ListFileName.DeleteAllItems();
}
while(!m_pRS->adoEOF)
{
var=m_pRS->Fields->GetItem(long(0))->GetValue();
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_ListFileName.InsertItem(curItem,strValue);
var=m_pRS->Fields->GetItem(long(1))->GetValue();
if(var.vt != VT_NULL)
strValue = (LPCSTR)_bstr_t(var);
m_ListFileName.SetItemText(curItem,1,strValue);
m_pRS->MoveNext();
curItem++;
}
m_pRS->Close();
}
catch(_com_error *e)
{
AfxMessageBox(e->ErrorMessage());
m_pRS->Close();
return;
}
}
BOOL CFileSysDlg::MyGetChunk(FieldPtr pField, LPVOID lpData)
{
ASSERT(pField != NULL);
ASSERT(lpData != NULL);
UCHAR chData;
long index = 0;
while (index < pField->ActualSize)
{
try
{
_variant_t varChunk = pField->GetChunk(pField->ActualSize);
if (varChunk.vt != (VT_ARRAY | VT_UI1))
{
return FALSE;
}
for (long i = 0; i < pField->ActualSize; i++)
{
if (SUCCEEDED( SafeArrayGetElement(varChunk.parray, &i, &chData) ))
{
((UCHAR*)lpData)[index] = chData;
index++;
}
else
{
break;
}
}
}
catch (_com_error e)
{
TRACE(_T("Warning: GetChunk 方法發(fā)生異常. 錯誤信息: %s; 文件: %s; 行: %d\n"), e.ErrorMessage(), __FILE__, __LINE__);
return FALSE;
}
}
return TRUE;
}
void CFileSysDlg::OnButtonGet()
{
CString strid,strSQL,fileName;
CFile file;
long size;
int sel=m_ListFileName.GetSelectionMark();
if(sel<0)
{
MessageBox("請選擇要提取的文件.");
return;
}
strid=m_ListFileName.GetItemText(sel,0);
fileName=m_ListFileName.GetItemText(sel,1);
strSQL="select * from tbFile where ID="+strid;
m_pRS->Open(_variant_t(strSQL),
m_pCon.GetInterfacePtr(),
adOpenDynamic,
adLockOptimistic,
adCmdText);
if(!m_pRS->adoEOF)
{
size=m_pRS->Fields->GetItem(long(2))->ActualSize;
if(size>0 && (m_pRS->Fields->GetItem(long(2))->GetAttributes() & adFldLong))
{
BYTE *lpData = new BYTE[size];
if(MyGetChunk(m_pRS->GetFields()->GetItem(long(2)),(LPVOID)lpData))
{
file.Open(fileName,CFile::modeCreate|CFile::modeReadWrite);
file.Write(lpData,size);
MessageBox("提取完成,文件保存在程序運行的目錄下");
}
}
}
m_pRS->Close();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -