⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camtooldlg.cpp

📁 CAM-TOOL 是高效率高质量的模具制造的最好CAM解决方案。在当今的 Windows 操作环境里
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// CamToolDlg.cpp : implementation file
//

#include "stdafx.h"
#include "dbt.h"
#include "CamTool.h"
#include "CamToolDlg.h"
#include "Jpegfile.h"
#include "jpeglib.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

BOOL DefaultFlag;
BOOL ApplyEn;
//Download File Path
CString DLFilePath;

#define SAFE_RELEASE(x) { if (x) x->Release(); x = NULL; }
// Application-defined message to notify app of filtergraph events
#define WM_GRAPHNOTIFY  (WM_APP+1)
#define WM_DLFINISH		(WM_USER+1)

//Thread Procedure
UINT DownloadThreadProc(LPVOID pParam);
CEvent StartProcedureEvent;
CEvent EndProcedureEvent;
BOOL DLLastOne;
BOOL DLError;
BYTE *m_buf;
BYTE bmp[IMAGE_WIDTH * IMAGE_HEIGHT *3];
BYTE *tmp;

// Global data
//
HWND PreviewHwnd = NULL;
HWND AppHwnd = NULL;

/////////////////////////////////////////////////////////////////////////////
// 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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCamToolDlg dialog

CCamToolDlg::CCamToolDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCamToolDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCamToolDlg)
		// 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);

	g_pVW = NULL;
	g_pMC = NULL;
	g_pME = NULL;
	g_pGraph = NULL;
	g_pCapture = NULL;
	g_psCurrent = Stopped;
}

void CCamToolDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCamToolDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCamToolDlg, CDialog)
	//{{AFX_MSG_MAP(CCamToolDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OK, OnClickOk)
	ON_BN_CLICKED(IDC_CANCEL, OnClickCancel)
	ON_BN_CLICKED(IDC_CAPTURE, OnCapture)
	ON_BN_CLICKED(IDC_BROWSE, OnBrowse)
	ON_BN_CLICKED(IDC_CONNECT, OnConnect)
	ON_BN_CLICKED(IDC_DEFAULT, OnDefault)
	ON_BN_CLICKED(IDC_DOWNLOAD, OnDownload)
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	ON_WM_HSCROLL()
	ON_MESSAGE(WM_DLFINISH, OnDownloadFinish)
	ON_MESSAGE(WM_GRAPHNOTIFY, HandleGraphEvent)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCamToolDlg message handlers
BOOL CCamToolDlg::InitPropertySheetSetting()
{
	BOOL flag;
	unsigned short Version;
	unsigned long CapNum;

	sprintf(InfoStr[0],"-----");
	sprintf(InfoStr[1],"-----");

	ReadDefaultFromIniFile();
	flag = method.Open();
	if(!flag) {
		method.LoadCameraDefaultValue();

		m_ctrlsheet.m_flashctrl.m_flash = CapImgInfo[CAP_MANUAL_FLASH_MODE];
		m_ctrlsheet.m_focusctrl.m_focus = CapImgInfo[CAP_FOCUS_MODE];
		m_ctrlsheet.m_imagectrl.m_compress = CapImgInfo[CAP_MANUAL_COMPRESSION];
		m_ctrlsheet.m_imagectrl.m_resolution = CapImgInfo[CAP_MANUAL_RESOLUTION];
		m_ctrlsheet.m_wbctrl.m_wb = CapImgInfo[CAP_MANUAL_WB_MODE];
		m_ctrlsheet.m_wbctrl.m_RRatio = RRatio;
		m_ctrlsheet.m_wbctrl.m_GRatio = GRatio;
		m_ctrlsheet.m_wbctrl.m_BRatio = BRatio;
		CapImgInfo[CAP_MANUAL_R_GAIN] = CapImgInfo[CAP_DAYLIGHT_R_GAIN] * RRatio / 1000;
		CapImgInfo[CAP_MANUAL_G_GAIN] = CapImgInfo[CAP_DAYLIGHT_G_GAIN] * GRatio / 1000;
		CapImgInfo[CAP_MANUAL_B_GAIN] = CapImgInfo[CAP_DAYLIGHT_B_GAIN] * BRatio / 1000;

		m_ctrlsheet.m_expctrl.m_exp = CapImgInfo[CAP_MANUAL_EXPOSURE_BIAS];
		m_ctrlsheet.m_expctrl.m_aemode = CapImgInfo[CAP_AE_MODE];
		m_ctrlsheet.m_expctrl.m_shutter = CapImgInfo[CAP_SHUTTER_TIME];
		m_ctrlsheet.m_expctrl.m_aperture = CapImgInfo[CAP_APERTURE];
		m_ctrlsheet.m_isoctrl.m_iso = CapImgInfo[CAP_MANUAL_ISO_SETTING];
		m_ctrlsheet.m_flashctrl.m_camflash = CapImgInfo[CAP_INTERNAL_FLASH];
		
		Version = method.GetHceVersion();
		sprintf(InfoStr[0],"0x%4X", Version);
		CapNum = method.GetCaptureCount();
		sprintf(InfoStr[1],"%d", CapNum);

		method.SetCurTimeToRTC();
		method.DoFlashCharge(CapImgInfo[CAP_MANUAL_FLASH_MODE]);
		method.Close();
	}
	m_ctrlsheet.m_infoctrl.ReDrawItem(InfoStr);
	m_ctrlsheet.m_expctrl.RefreshUI();
	m_ctrlsheet.m_wbctrl.RefreshUI();

	UpdateData();

	if(flag) return 1;
	return 0;
}

UINT DownloadThreadProc(LPVOID pParam)
{
	unsigned char *filebuf;
	FILE_ATTRIB pAttribute;
	unsigned short pwSuccess;
	unsigned short index;
	unsigned short DirNum, FileNum, ItemTypes, Locked;
	char str[255];
	unsigned short index_temp;
	unsigned short FileProgress;
	unsigned short count;
	CCamFun method;
	CWnd *wnd;
	CFile f;
	CFileException e;

	CProgressCtrl *progress;
	CStatic *percentage;
	CEdit *path;

	::WaitForSingleObject(StartProcedureEvent.m_hObject, INFINITE);
	wnd = AfxGetMainWnd();
	progress = (CProgressCtrl*)wnd->GetDlgItem(IDC_PROGRESS);
	percentage = (CStatic*)wnd->GetDlgItem(IDC_PERCENTAGE);
	path = (CEdit*)wnd->GetDlgItem(IDC_PATH);

	if(!method.Open()) {

		I43_GetCurrentImage(&index);
		index_temp = index;
		if(index == 0) {
			method.Close();
			::SendMessage((HWND)pParam, WM_DLFINISH, 0, 0);
			return 1;
		}
		percentage->SetWindowText("0 %");

		count = 0;
		if(DLLastOne) FileProgress = 2;
		else FileProgress = index * 2;
		progress->SetRange(0,FileProgress);

		do {
			I43_SetCurrentImage(index);

			I43_GetDCFInfo(index, &DirNum, &FileNum, &ItemTypes, &Locked);
			I43_SetFileType(ItemTypes);

			I43_GetFileAttributes ((I43_BOOL)0, &pwSuccess, &pAttribute );

			filebuf = (unsigned char *) malloc(sizeof(char)*(pAttribute.fileSize + 2));

			path->GetWindowText(DLFilePath);
			if(DLLastOne) {
				sprintf(str,"DSC%05d.JPG", method.GetCaptureCount()%99999L);
				DLFilePath += str;
			}
			else {
				DLFilePath += pAttribute.name;
			}

			if( !f.Open( DLFilePath, CFile::modeCreate | CFile::modeWrite, &e ) )
			{
				#ifdef _DEBUG
				   afxDump << "File could not be opened " << e.m_cause << "\n";
				#endif
			}

			sprintf(str,"%3d%s",++count*100/FileProgress,"%");

			percentage->SetWindowText(str);
			progress->SetPos(count);

			method.IdleMode();

			if(method.DownloadFileToBuf(filebuf, (unsigned char *)pAttribute.name, pAttribute.fileSize + 2)) {
				if(f.m_hFile != CFile::hFileNull) f.Close();
				free(filebuf);

				method.Close();
				::SendMessage((HWND)pParam, WM_DLFINISH, 0, 0);
				return 1;
			}

			if(DLLastOne) {
				DLError = 0;
				if(!strncmp((char*)&pAttribute.name[9], "JPG", 3) || !strncmp((char*)&pAttribute.name[9], "jpg", 3)) {
					if(filebuf[0] != 0xff || filebuf[1] != 0xd8) DLError = 1;
				}
				else {
					if(filebuf[0] != 0x52 || filebuf[1] != 0x49 || filebuf[2] != 0x46 || filebuf[3] != 0x46) DLError = 1;
				}
			}

			sprintf(str,"%3d%s",++count*100/FileProgress,"%");
			percentage->SetWindowText(str);
			progress->SetPos(count);

			f.WriteHuge(filebuf, sizeof(char)*pAttribute.fileSize);
			if(f.m_hFile != CFile::hFileNull) f.Close();
			free(filebuf);

			if(::WaitForSingleObject(EndProcedureEvent.m_hObject, 0) == WAIT_OBJECT_0) break;
		}while(--index && !DLLastOne);

		index = index_temp;
		I43_SetCurrentImage(index);
	}

	method.Close();

	::SendMessage((HWND)pParam, WM_DLFINISH, 0, 0);
	return 0;
}


BOOL CCamToolDlg::OnInitDialog()
{
	BOOL flag;

	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

	PreviewHwnd = GetDlgItem(IDC_PREVIEW)->GetSafeHwnd();
	AppHwnd = GetSafeHwnd();

	InitUserInterface();
	CoInitialize(NULL);	//init Graph filter

	flag = InitPropertySheetSetting();
	if(!flag) {
		DefaultFlag = 1;
		method.Open();
		method.TurnLCD(0);
		method.Close();
		ShowCurrentState("Initial the camera successfully.", 0);
	}
	else {
		DefaultFlag = 0;
		Error( TEXT("No video device was detected on your system.\r\n\r\n")
			   TEXT("Please connect the camera to PC, and press\r\n")
			   TEXT("[Preview On] button again.") );
		ShowCurrentState("No video device was detected on your system.", 1);
	}
	ApplyEn = 0;

	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CCamToolDlg::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 CCamToolDlg::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
	{
		DrawPics(1);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCamToolDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCamToolDlg::OnOK() 
{
	// TODO: Add extra validation here
	
//	CDialog::OnOK();
}

void CCamToolDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
//	CDialog::OnCancel();
}

void CCamToolDlg::OnClickOk() 
{
	// TODO: Add your control notification handler code here
	CDialog::OnOK();
}

void CCamToolDlg::OnClickCancel() 
{
	// TODO: Add your control notification handler code here
//	CDialog::OnCancel();
}

/////////////////////////////////////////////////////////////////////////////
// EkCreateEmbeddedPropertySheet

BOOL EkCreateEmbeddedPropertySheet(	CWnd* pParent, CPropertySheet* pPSheet,
									DWORD dwStyle = WS_CHILD | WS_VISIBLE, DWORD dwExStyle = 0 )
{
	ASSERT_VALID( pParent );
	ASSERT_VALID( pPSheet );

	// 1 - Create the embedded property sheet window
	if( !pPSheet->Create( pParent,  dwStyle, dwExStyle ) )
	{
		TRACE0( "Embedded property sheet creation failed\n" );
		return FALSE;
	}

	// 2 - Add WS_TABSTOP and WS_EX_CONTROLPARENT to the property sheet styles
	pPSheet->ModifyStyle( 0, WS_TABSTOP );
	pPSheet->ModifyStyleEx ( 0, WS_EX_CONTROLPARENT );

	// 3 - Add WS_EX_CONTROLPARENT to the parent window styles
	pParent->ModifyStyleEx ( 0, WS_EX_CONTROLPARENT );

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
void EkPositionEmbeddedPropertySheet(	CWnd* pParent, CPropertySheet* pPSheet,
										CRect rcNewPosition )
{
	ASSERT_VALID( pParent );
	ASSERT_VALID( pPSheet );

	// 1 - Get current coordinates of tab control
	// and property sheet window
	CTabCtrl* pTabCtrl = pPSheet->GetTabControl();
	ASSERT( pTabCtrl != NULL );

	CRect rcTabCtrl;
	pTabCtrl->ModifyStyle(0, TCS_BOTTOM);
	pTabCtrl->GetWindowRect( &rcTabCtrl );
	pParent->ScreenToClient( &rcTabCtrl );

	CRect rcPSheet;
	pPSheet->GetWindowRect( &rcPSheet );
	pParent->ScreenToClient( &rcPSheet );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -