?? onedlg.cpp
字號:
// oneDlg.cpp : implementation file
//
#include "stdafx.h"
#include "one.h"
#include "oneDlg.h"
#include "Picture.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern COneApp theApp;//james???
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// COneDlg dialog
COneDlg::COneDlg(CWnd* pParent /*=NULL*/)
: CDialog(COneDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(COneDlg)
// 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 COneDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COneDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COneDlg, CDialog)
//{{AFX_MSG_MAP(COneDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// COneDlg message handlers
BOOL COneDlg::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
//----------------------------------------------------
try
{
m_pRecordset.CreateInstance("ADODB.Recordset");
m_pRecordset->Open("SELECT * FROM jlab ORDER BY id",_variant_t((IDispatch*)theApp.m_pConnection,true),adOpenStatic,adLockOptimistic,adCmdText);
}
catch(_com_error e)///捕捉異常
{
AfxMessageBox("讀取數據庫失敗!");///顯示錯誤信息
}
//-----------------------------------------------------
return TRUE; // return TRUE unless you set the focus to a control
}
void COneDlg::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 COneDlg::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 COneDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void COneDlg::OnButton1() //-----------jpg存入庫并顯示-------------
{
// TODO: Add your control notification handler code here
CFile f;
CString FilePathName;
CFileException e;
CFileDialog dlg(TRUE,NULL,NULL,0,"jpg Files (*.jpg)|*.jpg||",this);///TRUE為OPEN對話框,FALSE為SAVE AS對話框
if(dlg.DoModal()==IDOK)
{FilePathName=dlg.GetPathName();
if(m_Pic.m_IPicture != NULL) m_Pic.FreePictureData(); // Important - Avoid Leaks...
if(f.Open(FilePathName, CFile::modeRead | CFile::typeBinary, &e)) //打開了一個jpg文件
{
int nSize = f.GetLength(); //先得到jpg文件長度
BYTE * pBuffer = new BYTE [nSize]; //按文件的大小在堆上申請一塊內存
if (f.Read(pBuffer, nSize) > 0 ) //把jpg文件讀到pBuffer(堆上申請一塊內存)
{ // +----------------------------------------------
BYTE *pBuf = pBuffer; ///下面這一大段是把pBuffer里的jpg數據放到庫中
VARIANT varBLOB;
SAFEARRAY *psa;
SAFEARRAYBOUND rgsabound[1];
m_pRecordset->AddNew();
if(pBuf)
{
rgsabound[0].lLbound = 0;
rgsabound[0].cElements = nSize;
psa = SafeArrayCreate(VT_UI1, 1, rgsabound);
for (long i = 0; i < (long)nSize; i++)
SafeArrayPutElement (psa, &i, pBuf++);
varBLOB.vt = VT_ARRAY | VT_UI1;
varBLOB.parray = psa;
m_pRecordset->GetFields()->GetItem("j")->AppendChunk(varBLOB);
}
m_pRecordset->Update();
// +----------------------------------------------
(m_Pic.LoadPictureData(pBuffer, nSize));//接作調用函數讀pBuffer的jpg數據準備顯示
delete [] pBuffer; //刪掉堆上申請的那一塊內存
pBuf=0; //以防二次亂用
}
f.Close();
}
CClientDC dc(this);
m_Pic.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels
m_Pic.Show(&dc, CRect(200,0,200+m_Pic.m_Width,m_Pic.m_Height) );//顯示出來看看
}
}
void COneDlg::OnButton2() //----------------從庫中提取并顯示------------
{
try
{
long nSize = m_pRecordset->GetFields()->GetItem("j")->ActualSize;
if(nSize > 0)
{
_variant_t varBLOB;
varBLOB = m_pRecordset->GetFields()->GetItem("j")->GetChunk(nSize);
if(varBLOB.vt == (VT_ARRAY | VT_UI1))
{
if(BYTE *pBuffer = new BYTE [nSize+1]) ///重新申請必要的存儲空間
{
char *pBuf = NULL;
SafeArrayAccessData(varBLOB.parray,(void **)&pBuf);
memcpy(pBuffer,pBuf,nSize); ///復制數據到緩沖區m_pBMPBuffer
SafeArrayUnaccessData (varBLOB.parray);
// int nSize = lDataSize;
(m_Pic.LoadPictureData(pBuffer, nSize));
delete [] pBuffer;
pBuf=0;
///生成BITMAP對象
CClientDC dc(this);
m_Pic.UpdateSizeOnDC(&dc); // Get Picture Dimentions In Pixels
m_Pic.Show(&dc, CRect(0,50,m_Pic.m_Width,50+m_Pic.m_Height) );
}
}
}
}
catch (_com_error e) {
}
}
void COneDlg::OnButton3()
{
try{
m_pRecordset->MovePrevious(); //選上一個jpg
OnButton2();
}
catch (_com_error e) {
}
}
void COneDlg::OnButton4()
{
try{
m_pRecordset->MoveNext(); //選下一個jpg
OnButton2();
}
catch (_com_error e) {
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -