DlgGuessWidth.cpp
上传用户:gzboli
上传日期:2013-04-10
资源大小:471k
文件大小:7k
- // DlgGuessWidth.cpp : implementation file
- //
- #include "stdafx.h"
- #include "QuickImage.h"
- #include "DlgGuessWidth.h"
- #include "guesswidth.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDlgGuessWidth dialog
- CDlgGuessWidth::CDlgGuessWidth(CWnd* pParent /*=NULL*/)
- : CDialog(CDlgGuessWidth::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDlgGuessWidth)
- m_iDepth = 0;
- m_iHeight = 0;
- m_iWidth = 0;
- m_iHeaderSize = 0;
- //}}AFX_DATA_INIT
- m_dCorr=-1.0;
- }
- void CDlgGuessWidth::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgGuessWidth)
- DDX_Radio(pDX, IDC_8BITS, m_iDepth);
- DDX_Text(pDX, IDC_EDIT_HEIGHT, m_iHeight);
- DDX_Text(pDX, IDC_EDIT_WIDTH, m_iWidth);
- DDX_Text(pDX, IDC_EDIT_HEADER_SIZE, m_iHeaderSize);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgGuessWidth, CDialog)
- //{{AFX_MSG_MAP(CDlgGuessWidth)
- ON_EN_KILLFOCUS(IDC_EDIT_WIDTH, OnKillfocusEditWidth)
- ON_EN_SETFOCUS(IDC_EDIT_WIDTH, OnSetfocusEditWidth)
- ON_EN_KILLFOCUS(IDC_EDIT_HEIGHT, OnKillfocusEditHeight)
- ON_EN_SETFOCUS(IDC_EDIT_HEIGHT, OnSetfocusEditHeight)
- ON_EN_KILLFOCUS(IDC_EDIT_HEADER_SIZE, OnKillfocusEditHeaderSize)
- ON_EN_SETFOCUS(IDC_EDIT_HEADER_SIZE, OnSetfocusEditHeaderSize)
- ON_BN_CLICKED(IDC_GUESS, OnGuess)
- ON_BN_CLICKED(IDC_SWAP, OnSwap)
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- ON_COMMAND_RANGE(IDC_8BITS, IDC_64BITS, OnChannels)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgGuessWidth message handlers
- void CDlgGuessWidth::OnKillfocusEditWidth()
- {
- UpdateData(TRUE);
- UINT iByte;
- if(m_iDepth == 4)
- {
- iByte = 8;
- }
- else
- {
- iByte = (UINT)(1 + m_iDepth);
- }
- if(m_iWidth != 0)
- {
- m_iHeight = (FILELENGTH - m_iHeaderSize)/iByte/m_iWidth;
- }
- UpdateData(FALSE);
- }
- void CDlgGuessWidth::OnSetfocusEditWidth()
- {
- CEdit *pEdit=(CEdit*)GetDlgItem(IDC_EDIT_WIDTH);
- ASSERT(IsWindow(pEdit->m_hWnd));
- // ASSERT_KINDOF(CEdit,pEdit);
- pEdit->SetSel(0,-1);
- }
- void CDlgGuessWidth::OnKillfocusEditHeight()
- {
- }
- void CDlgGuessWidth::OnSetfocusEditHeight()
- {
- CEdit *pEdit=(CEdit*)GetDlgItem(IDC_EDIT_HEIGHT);
- ASSERT(IsWindow(pEdit->m_hWnd));
- // ASSERT_KINDOF(CEdit,pEdit);
- pEdit->SetSel(0,-1);
- }
- void CDlgGuessWidth::OnKillfocusEditHeaderSize()
- {
- UpdateData(TRUE);
- if(m_iHeaderSize > FILELENGTH)
- {
- MessageBox("ERROR: Header size must smaller then the file size!",
- NULL,MB_OK | MB_ICONERROR);
- m_iHeaderSize = FILELENGTH;
- UpdateData(FALSE);
-
- CWnd *pWnd = GetDlgItem(IDC_EDIT_HEADER_SIZE);
- ASSERT(IsWindow(pWnd->m_hWnd));
- pWnd->SetFocus();
- }
- else
- {
- UINT iByte;
- if(m_iDepth == 4)
- {
- iByte = 8;
- }
- else
- {
- iByte = (UINT)(1 + m_iDepth);
- }
- if(m_iWidth != 0)
- {
- m_iHeight = (FILELENGTH - m_iHeaderSize)/iByte/m_iWidth;
- }
- UpdateData(FALSE);
- }
- }
- void CDlgGuessWidth::OnSetfocusEditHeaderSize()
- {
- CEdit *pEdit=(CEdit*)GetDlgItem(IDC_EDIT_HEADER_SIZE);
- ASSERT(IsWindow(pEdit->m_hWnd));
- // ASSERT_KINDOF(CEdit,pEdit);
- pEdit->SetSel(0,-1);
- }
- void CDlgGuessWidth::OnGuess()
- {
- UpdateData(TRUE);
- CGuessWidth guess;
- AfxGetApp()->BeginWaitCursor();
-
- short iByte;
- if(m_iDepth == 4)
- {
- iByte = 8;
- }
- else
- {
- iByte = (UINT)(1 + m_iDepth);
- }
- m_dCorr = guess.WidthDetect(m_iWidth, m_iHeight, PATHNAME, iByte, m_iHeaderSize);
- AfxGetApp()->EndWaitCursor();
- UpdateData(FALSE);
-
- }
- void CDlgGuessWidth::OnSwap()
- {
- UpdateData(TRUE);
- DWORD iTemp = m_iWidth;
- m_iWidth = m_iHeight;
- m_iHeight = iTemp;
- UpdateData(FALSE);
- }
- void CDlgGuessWidth::OnOK()
- {
- UpdateData(TRUE);
- UINT iByte;
- if(m_iDepth == 4)
- {
- iByte = 8;
- }
- else
- {
- iByte = (UINT)(1 + m_iDepth);
- }
- DWORD iTotalSize = m_iWidth*m_iHeight*iByte + m_iHeaderSize;
- if(iTotalSize > FILELENGTH)
- {
- MessageBox("ERROR: Given size is larger then the file size!",
- NULL,MB_OK | MB_ICONERROR);
-
- CWnd *pWnd = GetDlgItem(IDC_EDIT_HEIGHT);
- ASSERT(IsWindow(pWnd->m_hWnd));
- pWnd->SetFocus();
- return;
- }
- else if(iTotalSize < FILELENGTH)
- {
- if(MessageBox(
- "ALERT: Given size is smaller then the file size,Open anyway?",
- NULL,MB_YESNO | MB_ICONWARNING) != IDYES)
- {
- CWnd *pWnd = GetDlgItem(IDC_EDIT_HEIGHT);
- ASSERT(IsWindow(pWnd->m_hWnd));
- pWnd->SetFocus();
- return;
- }
- }
- CDialog::OnOK();
- }
- BOOL CDlgGuessWidth::OnInitDialog()
- {
- CDialog::OnInitDialog();
- /* m_btOK.SubclassDlgItem(IDOK, this);
- m_btOK.SetIcon(IDI_OKAC, IDI_OKUN);
- m_btOK.SetActiveBgColor(RGB(220,220,220));
- m_btOK.SetActiveFgColor(RGB(255,0,0));
- m_btOK.SetBtnCursor(IDC_GREEN);
-
- m_btCancel.SubclassDlgItem(IDCANCEL, this);
- m_btCancel.SetIcon(IDI_CANCELAC, IDI_CANCELUN);
- m_btCancel.SetActiveBgColor(RGB(220,220,220));
- m_btCancel.SetActiveFgColor(RGB(255,0,0));
- m_btCancel.SetBtnCursor(IDC_GREEN);
- */
- SetWindowText(TITLE);
-
- CFile file;
- if(!file.Open(PATHNAME,CFile::modeRead | CFile::shareExclusive))
- {
- return FALSE;
- }
- FILELENGTH = file.GetLength();
- file.Close();
- UINT iByte;
- if(m_iDepth == 4)
- {
- iByte = 8;
- }
- else
- {
- iByte = (UINT)(1 + m_iDepth);
- }
- iByte = (FILELENGTH - m_iHeaderSize) / iByte;
- m_iWidth = m_iHeight = (DWORD)sqrt(iByte);
- while((iByte % m_iWidth) != 0) m_iWidth--;
- m_iHeight = iByte / m_iWidth;
- CWnd* pWnd = GetDlgItem(IDC_FILE_DESCRIBE);
- ASSERT_VALID(pWnd);
- if(IsWindow(pWnd->m_hWnd))
- {
- char szDes[MAX_PATH+40];
- char *pExt=strrchr(PATHNAME,'\');
-
- sprintf(szDes,"Describe: " %s "n%d bytes",++pExt,FILELENGTH);
- pWnd->SetWindowText((LPCTSTR)szDes);
- }
- UpdateData(FALSE);
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- HBRUSH CDlgGuessWidth::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
-
- if(pWnd->GetDlgCtrlID() == IDC_EDIT_HEADER_SIZE)
- {
- if(IsWindow(pWnd->m_hWnd))
- {
- CBrush b;
- b.CreateSolidBrush(RGB(0,96,249));
- CRect rc;
- pWnd->GetClientRect(&rc);
- pDC->FillRect(&rc,&b);
- pDC->SetBkColor(RGB(0,96,249));
- }
- }
- // TODO: Change any attributes of the DC here
-
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
- void CDlgGuessWidth::OnChannels(UINT uID)
- {
- UpdateData(TRUE);
- UINT iByte;
- if(m_iDepth == 4)
- {
- iByte = 8;
- }
- else
- {
- iByte = (UINT)(1 + m_iDepth);
- }
- if(m_iWidth != 0)
- {
- m_iHeight = (FILELENGTH - m_iHeaderSize)/iByte/m_iWidth;
- }
- UpdateData(FALSE);
- }