// CKfmnRgnDlg.cpp : implementation file // #include "stdafx.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif extern CIMGPFApp theApp; ///////////////////////////////////////////////////////////////////////////// // CKfmnRgnDlg dialog const int INNER_RGN = 0; const int OUTER_RGN = 1; CKfmnRgnDlg* CKfmnRgnDlg::m_pKfmnRgnDlg = NULL; CKfmnRgnDlg::~CKfmnRgnDlg() { UnhookWindowsHookEx(m_hMouseHook); if(m_pKfmnRgnDlg) m_pKfmnRgnDlg = NULL; if(m_pOrgRaster != NULL) { m_pOrgRaster->Release(); m_pOrgRaster = NULL; } } CKfmnRgnDlg::CKfmnRgnDlg(CWnd* pParent /*=NULL*/) : CDialog(IDD_KFMNRGNDLG, pParent) { m_pOrgRaster = NULL; CoCreateInstance( CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&m_pOrgRaster ); m_nSelectedRegion = INNER_RGN; m_bInnerRegion = TRUE; m_uFrameType = 0; m_bRemoveHoles[INNER_RGN] = m_bRemoveHoles[OUTER_RGN] = TRUE; m_uPixelCount[INNER_RGN] = m_uPixelCount[OUTER_RGN] = 0; m_crFrame = RGB(255, 0, 0); m_fAcceptNewValues = TRUE; m_pKfmnRgnDlg = NULL; m_bInProcess = FALSE; m_bKillProgress = FALSE; m_bFirstTime = TRUE; m_bShowTrackFrame = TRUE; m_bShowTemplate = FALSE; } void CKfmnRgnDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); DDX_Control(pDX, IDRB_INNERREGION, m_rbInnerRegion); DDX_Control(pDX, IDRB_OUTERREGION, m_rbOuterRegion); DDX_Control(pDX, IDCHK_SHOWTEMPLATE, m_chkShowTemplate); DDX_Control(pDX, IDCHK_SHOWTRACKFRAME, m_chkShowTrackFrame); DDX_Control(pDX, IDSLDR_THRESHOLD, m_sldrThreshold); DDX_Control(pDX, IDEDT_THRESHOLD, m_edtThreshold); DDX_Control(pDX, IDSLDR_BLURRADIUS, m_sldrBlurRadius); DDX_Control(pDX, IDEDT_BLURRADIUS, m_edtBlurRadius); DDX_Control(pDX, IDSLDR_MINTHRESHOLD, m_sldrMinThreshold); DDX_Control(pDX, IDEDT_MINTHRESHOLD, m_edtMinThreshold); DDX_Control(pDX, IDSLDR_MAXTHRESHOLD, m_sldrMaxThreshold); DDX_Control(pDX, IDEDT_MAXTHRESHOLD, m_edtMaxThreshold); DDX_Control(pDX, IDCHK_REMOVEHOLES, m_chkRemoveHoles); DDX_Control(pDX, IDSTC_INNERTEXT, m_stcInnerText); DDX_Control(pDX, IDSTC_OUTERTEXT, m_stcOuterText); DDX_Control(pDX, IDSTC_AREA, m_stcArea); DDX_Control(pDX, IDBTN_HELP, m_btnHelp); DDX_Control(pDX, IDPROG, m_progress); DDX_Check(pDX, IDCHK_SHOWTRACKFRAME, m_bShowTrackFrame); DDX_Check(pDX, IDCHK_SHOWTEMPLATE, m_bShowTemplate); } BEGIN_MESSAGE_MAP(CKfmnRgnDlg, CDialog) ON_BN_CLICKED(IDCHK_REMOVEHOLES, OnRemoveHoles) ON_BN_CLICKED(IDRB_INNERREGION, OnInnerRgn) ON_BN_CLICKED(IDRB_OUTERREGION, OnOuterRgn) ON_BN_CLICKED(IDCHK_SHOWTEMPLATE, OnShowTemplate) ON_BN_CLICKED(IDCHK_SHOWTRACKFRAME, OnShowTrackFrame) ON_EN_CHANGE(IDEDT_THRESHOLD, OnThresholdChanged) ON_EN_KILLFOCUS(IDEDT_THRESHOLD, OnKillFocusThresholdEdit) ON_EN_CHANGE(IDEDT_BLURRADIUS, OnBlurRadiusChanged) ON_EN_KILLFOCUS(IDEDT_BLURRADIUS, OnKillFocusBlurRadiusEdit) ON_EN_CHANGE(IDEDT_MINTHRESHOLD, OnMinThresholdChanged) ON_EN_KILLFOCUS(IDEDT_MINTHRESHOLD, OnKillFocusMinThresholdEdit) ON_EN_CHANGE(IDEDT_MAXTHRESHOLD, OnMaxThresholdChanged) ON_EN_KILLFOCUS(IDEDT_MAXTHRESHOLD, OnKillFocusMaxThresholdEdit) ON_WM_HSCROLL() ON_WM_CLOSE() ON_MESSAGE(WM_PASTE, OnPaste) ON_BN_CLICKED(IDBTN_HELP, OnHelp) ON_BN_CLICKED(IDOK, OnOK) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CKfmnRgnDlg message handlers BOOL CKfmnRgnDlg::OnInitDialog() { CDialog::OnInitDialog(); m_fAcceptNewValues = FALSE; m_rbInnerRegion.SetCheck(m_bInnerRegion); m_sldrThreshold.SetRange(m_SlidersControlsInfo.uMinRange[0], m_SlidersControlsInfo.uMaxRange[0]); m_sldrThreshold.SetPos(m_SlidersControlsInfo.uDefaultValue[0]); m_edtThreshold.SetWindowText(m_SlidersControlsInfo.strDefaultValue[0]); m_edtThreshold.SetLimitText(m_SlidersControlsInfo.uMaxLimitText[0]); m_sldrBlurRadius.SetRange(m_SlidersControlsInfo.uMinRange[1], m_SlidersControlsInfo.uMaxRange[1]); m_sldrBlurRadius.SetPos(m_SlidersControlsInfo.uDefaultValue[1]); m_edtBlurRadius.SetWindowText(m_SlidersControlsInfo.strDefaultValue[1]); m_edtBlurRadius.SetLimitText(m_SlidersControlsInfo.uMaxLimitText[1]); m_sldrMinThreshold.SetRange(m_SlidersControlsInfo.uMinRange[2], m_SlidersControlsInfo.uMaxRange[2]); m_sldrMinThreshold.SetPos(m_SlidersControlsInfo.uDefaultValue[2]); m_edtMinThreshold.SetWindowText(m_SlidersControlsInfo.strDefaultValue[2]); m_edtMinThreshold.SetLimitText(m_SlidersControlsInfo.uMaxLimitText[2]); m_sldrMaxThreshold.SetRange(m_SlidersControlsInfo.uMinRange[3], m_SlidersControlsInfo.uMaxRange[3]); m_sldrMaxThreshold.SetPos(m_SlidersControlsInfo.uDefaultValue[3]); m_edtMaxThreshold.SetWindowText(m_SlidersControlsInfo.strDefaultValue[3]); m_edtMaxThreshold.SetLimitText(m_SlidersControlsInfo.uMaxLimitText[3]); m_chkRemoveHoles.SetCheck(m_bRemoveHoles[m_nSelectedRegion]); m_progress.SetRange(0, 100); m_fAcceptNewValues = TRUE; m_hRgn = NULL; m_pKfmnRgnDlg = this; m_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseHookProc, AfxGetInstanceHandle(), GetCurrentThreadId()); ApplyFunction(); if(m_bInnerRegion) { m_bInnerRegion = FALSE; OnInnerRgn(); m_bInnerRegion = TRUE; } return TRUE; } void CKfmnRgnDlg::OnHelp() { UnhookWindowsHookEx(m_hMouseHook); CHelpMsgDlg dlgHelpMsg; dlgHelpMsg.DoModal(); m_hMouseHook = SetWindowsHookEx(WH_MOUSE, MouseHookProc, AfxGetInstanceHandle(), GetCurrentThreadId()); } void CKfmnRgnDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { if(m_fAcceptNewValues) { if(pScrollBar->GetSafeHwnd() == m_sldrThreshold.GetSafeHwnd()) { int nValue = (int)m_sldrThreshold.GetPos(); if(nValue != (int)m_uThreshold[m_nSelectedRegion]) { CString strValue = _T(""); strValue.Format(_T("%d"), nValue); m_fAcceptNewValues = FALSE; m_edtThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_uThreshold[m_nSelectedRegion] = nValue; ApplyFunction(); } } else if(pScrollBar->GetSafeHwnd() == m_sldrBlurRadius.GetSafeHwnd()) { int nValue = (int)m_sldrBlurRadius.GetPos(); if(nValue != (int)m_uBlurRadius[m_nSelectedRegion]) { CString strValue = _T(""); strValue.Format(_T("%d"), nValue); m_fAcceptNewValues = FALSE; m_edtBlurRadius.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_uBlurRadius[m_nSelectedRegion] = nValue; ApplyFunction(); } } else if(pScrollBar->GetSafeHwnd() == m_sldrMinThreshold.GetSafeHwnd()) { int nValue = m_sldrMinThreshold.GetPos(); if(nValue != (int)m_uMaxThreshold[m_nSelectedRegion]) { if((nValue + 2) < (int)m_uMaxThreshold[m_nSelectedRegion]) { CString strValue = _T(""); strValue.Format(_T("%d"), nValue); m_fAcceptNewValues = FALSE; m_edtMinThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_uMinThreshold[m_nSelectedRegion] = nValue; } else { m_uMinThreshold[m_nSelectedRegion] = m_uMaxThreshold[m_nSelectedRegion]-2; m_sldrMinThreshold.SetPos(m_uMinThreshold[m_nSelectedRegion]); CString strValue = _T(""); strValue.Format(_T("%d"), m_uMinThreshold[m_nSelectedRegion]); m_fAcceptNewValues = FALSE; m_edtMinThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; } ApplyFunction(); } } else if(pScrollBar->GetSafeHwnd() == m_sldrMaxThreshold.GetSafeHwnd()) { int nValue = m_sldrMaxThreshold.GetPos(); if(nValue != (int)m_uMinThreshold[m_nSelectedRegion]) { if((nValue - 2) > (int)m_uMinThreshold[m_nSelectedRegion]) { CString strValue = _T(""); strValue.Format(_T("%d"), nValue); m_fAcceptNewValues = FALSE; m_edtMaxThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_uMaxThreshold[m_nSelectedRegion] = nValue; } else { m_uMaxThreshold[m_nSelectedRegion] = m_uMinThreshold[m_nSelectedRegion]+2; m_sldrMaxThreshold.SetPos(m_uMaxThreshold[m_nSelectedRegion]); CString strValue; strValue.Format(_T("%d"), m_uMaxThreshold[m_nSelectedRegion]); m_fAcceptNewValues = FALSE; m_edtMaxThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; } ApplyFunction(); } } } } void CKfmnRgnDlg::OnThresholdChanged() { CString strValue = _T(""); m_edtThreshold.GetWindowText(strValue); unsigned int nValue = (int)_tcstod(strValue, NULL); if(nValue < m_SlidersControlsInfo.uMinRange[0]) { return; } else if(nValue > m_SlidersControlsInfo.uMaxRange[0]) { return; } m_uThreshold[m_nSelectedRegion] = nValue; m_sldrThreshold.SetPos(m_uThreshold[m_nSelectedRegion]); ApplyFunction(); } void CKfmnRgnDlg::OnBlurRadiusChanged() { CString strValue = _T(""); m_edtBlurRadius.GetWindowText(strValue); unsigned int nValue = (int)_tcstod(strValue, NULL); if(nValue < m_SlidersControlsInfo.uMinRange[1]) { return; } else if(nValue > m_SlidersControlsInfo.uMaxRange[1]) { return; } m_uBlurRadius[m_nSelectedRegion] = nValue; m_sldrBlurRadius.SetPos(m_uBlurRadius[m_nSelectedRegion]); ApplyFunction(); } void CKfmnRgnDlg::OnMinThresholdChanged() { CString strValue = _T(""); m_edtMinThreshold.GetWindowText(strValue); unsigned int nValue = (int)_tcstod(strValue, NULL); if(nValue < m_SlidersControlsInfo.uMinRange[2]) { return; } else if((nValue + 2) > m_uMaxThreshold[m_nSelectedRegion]) { nValue = m_uMaxThreshold[m_nSelectedRegion] - 2; CString strValue = _T(""); strValue.Format(_T("%d"), nValue); m_edtMinThreshold.SetWindowText(strValue); } m_uMinThreshold[m_nSelectedRegion] = nValue; m_sldrMinThreshold.SetPos(m_uMinThreshold[m_nSelectedRegion]); ApplyFunction(); } void CKfmnRgnDlg::OnMaxThresholdChanged() { CString strValue = _T(""); m_edtMaxThreshold.GetWindowText(strValue); unsigned int nValue = (int)_tcstod(strValue, NULL); if(nValue < m_SlidersControlsInfo.uMinRange[3]) { return; } else if((nValue - 2) < m_uMinThreshold[m_nSelectedRegion]) { nValue = m_uMinThreshold[m_nSelectedRegion] + 2; CString strValue = _T(""); strValue.Format(_T("%d"), nValue); m_edtMaxThreshold.SetWindowText(strValue); } m_uMaxThreshold[m_nSelectedRegion] = nValue; m_sldrMaxThreshold.SetPos(m_uMaxThreshold[m_nSelectedRegion]); ApplyFunction(); } void CKfmnRgnDlg::OnPaste() { //return; } void CKfmnRgnDlg::OnKillFocusThresholdEdit() { CString strValue; GetDlgItemText(IDEDT_THRESHOLD, strValue); if(strValue.GetLength() == 0) { SetDlgItemInt(IDEDT_THRESHOLD, m_uThreshold[m_nSelectedRegion]); return; } else { unsigned int nValue = GetDlgItemInt(IDEDT_THRESHOLD); if(nValue > m_SlidersControlsInfo.uMaxRange[0]) { m_uThreshold[m_nSelectedRegion] = m_SlidersControlsInfo.uMaxRange[0]; SetDlgItemInt(IDEDT_THRESHOLD, m_uThreshold[m_nSelectedRegion]); return; } } ApplyFunction(); return; } void CKfmnRgnDlg::OnKillFocusBlurRadiusEdit() { CString strValue; GetDlgItemText(IDEDT_BLURRADIUS, strValue); if(strValue.GetLength() == 0) { SetDlgItemInt(IDEDT_BLURRADIUS, m_uBlurRadius[m_nSelectedRegion]); return; } else { unsigned int nValue = GetDlgItemInt(IDEDT_BLURRADIUS); if(nValue > m_SlidersControlsInfo.uMaxRange[1]) { m_uBlurRadius[m_nSelectedRegion] = m_SlidersControlsInfo.uMaxRange[1]; SetDlgItemInt(IDEDT_BLURRADIUS, m_uBlurRadius[m_nSelectedRegion]); return; } } ApplyFunction(); return; } void CKfmnRgnDlg::OnKillFocusMinThresholdEdit() { CString strValue; GetDlgItemText(IDEDT_MINTHRESHOLD, strValue); if(strValue.GetLength() == 0) { SetDlgItemInt(IDEDT_MINTHRESHOLD, m_uMinThreshold[m_nSelectedRegion]); return; } else { unsigned int nValue = GetDlgItemInt(IDEDT_MINTHRESHOLD); if(nValue > m_SlidersControlsInfo.uMaxRange[2]) { m_uMinThreshold[m_nSelectedRegion] = m_SlidersControlsInfo.uMaxRange[2]; SetDlgItemInt(IDEDT_MINTHRESHOLD, m_uThreshold[m_nSelectedRegion]); return; } } ApplyFunction(); return; } void CKfmnRgnDlg::OnKillFocusMaxThresholdEdit() { CString strValue; GetDlgItemText(IDEDT_MAXTHRESHOLD, strValue); if(strValue.GetLength() == 0) { SetDlgItemInt(IDEDT_MAXTHRESHOLD, m_uMaxThreshold[m_nSelectedRegion]); return; } else { unsigned int nValue = GetDlgItemInt(IDEDT_MAXTHRESHOLD); if(nValue > m_SlidersControlsInfo.uMaxRange[3]) { m_uMaxThreshold[m_nSelectedRegion] = m_SlidersControlsInfo.uMaxRange[3]; SetDlgItemInt(IDEDT_MAXTHRESHOLD, m_uMaxThreshold[m_nSelectedRegion]); return; } } ApplyFunction(); return; } void CKfmnRgnDlg::ApplyFunction() { int nRet = 0; ILEADRaster *pProcessedRaster = NULL; if(!m_fAcceptNewValues) return; if(m_bInProcess) { m_bKillProgress = TRUE; return; } CoCreateInstance( CLSID_LEADRaster, NULL, CLSCTX_ALL, IID_ILEADRaster, (void**)&pProcessedRaster ); m_bInProcess = TRUE; theApp.m_pRasterProcess->PutEnableProgressEvent( TRUE ); nRet = theApp.m_pRasterProcess->SetKaufmannRgn(m_pOrgRaster, pProcessedRaster, m_uBlurRadius[m_nSelectedRegion], m_uMinThreshold[m_nSelectedRegion], m_uMaxThreshold[m_nSelectedRegion], m_uThreshold[m_nSelectedRegion], m_ptCenter[m_nSelectedRegion].x, m_ptCenter[m_nSelectedRegion].y, m_bRemoveHoles[m_nSelectedRegion], L_RGN_SET); theApp.m_pRasterProcess->PutEnableProgressEvent( FALSE ); m_uPixelCount[m_nSelectedRegion] = theApp.m_pRasterProcess->GetKaufmannRgnPixelsCount(); UpdateRgnTextInfo(); if (nRet != 0) { m_progress.SetPos(100); return; } if(m_bShowTemplate) { m_pMainView->m_ltRasView.GetRaster().SetBitmap(pProcessedRaster->GetBitmap()); } else { m_pMainView->m_ltRasView.GetRaster().SetBitmap(m_pOrgRaster->GetBitmap()); } if(m_bFirstTime) { m_uPixelCount[OUTER_RGN] = m_uPixelCount[INNER_RGN]; m_bFirstTime = FALSE; } if (nRet == 0) CalculatePercentage(); m_bInProcess = FALSE; if(pProcessedRaster) { pProcessedRaster->Release(); pProcessedRaster = NULL; } } void CKfmnRgnDlg::SetDlgParams(CIMGPFView * pMainView) { m_pMainView = pMainView; if(m_pOrgRaster) { m_pOrgRaster->RefBitmap = FALSE; m_pOrgRaster->Bitmap = m_pMainView->m_ltRasView.GetRaster().GetBitmap(); m_ptCenter[INNER_RGN] = m_ptCenter[OUTER_RGN] = m_ptCenter[INNER_RGN].x = (long)m_ptCenter[OUTER_RGN].x = (long)m_pOrgRaster->GetBitmapWidth() / 2; m_ptCenter[INNER_RGN].y = (long)m_ptCenter[OUTER_RGN].y = (long)m_pOrgRaster->GetBitmapHeight() / 2; m_hRgn = (HRGN)m_pMainView->m_ltRasView.GetRaster().GetRgnHandle(); m_SlidersControlsInfo.uMinRange[0] = 0; m_SlidersControlsInfo.uMinRange[1] = 1; m_SlidersControlsInfo.uMinRange[2] = 0; m_SlidersControlsInfo.uMinRange[3] = 0; m_SlidersControlsInfo.uMaxRange[0] = 255; m_SlidersControlsInfo.uMaxRange[1] = 100; m_SlidersControlsInfo.uMaxRange[2] = 255; m_SlidersControlsInfo.uMaxRange[3] = 255; m_SlidersControlsInfo.uDefaultValue[0] = 25; m_SlidersControlsInfo.uDefaultValue[1] = 5; m_SlidersControlsInfo.uDefaultValue[2] = 5; m_SlidersControlsInfo.uDefaultValue[3] = 255; m_SlidersControlsInfo.uMaxLimitText[0] = 3; m_SlidersControlsInfo.uMaxLimitText[1] = 3; m_SlidersControlsInfo.uMaxLimitText[2] = 3; m_SlidersControlsInfo.uMaxLimitText[3] = 3; m_SlidersControlsInfo.strDefaultValue[0] = _T("25"); m_SlidersControlsInfo.strDefaultValue[1] = _T("5"); m_SlidersControlsInfo.strDefaultValue[2] = _T("5"); m_SlidersControlsInfo.strDefaultValue[3] = _T("255"); if(m_pOrgRaster->GetBitmapBits() == 12) { m_SlidersControlsInfo.uMaxRange[0] = 4095; m_SlidersControlsInfo.uMaxRange[2] = 4093; m_SlidersControlsInfo.uMaxRange[3] = 4095; m_SlidersControlsInfo.uMaxLimitText[0] = 4; m_SlidersControlsInfo.uMaxLimitText[2] = 4; m_SlidersControlsInfo.uMaxLimitText[3] = 4; m_SlidersControlsInfo.uDefaultValue[0] = 400; m_SlidersControlsInfo.uDefaultValue[2] = 0; m_SlidersControlsInfo.uDefaultValue[3] = 4095; m_SlidersControlsInfo.strDefaultValue[0] = _T("400"); m_SlidersControlsInfo.strDefaultValue[2] = _T("0"); m_SlidersControlsInfo.strDefaultValue[3] = _T("4095"); } else if(m_pOrgRaster->GetBitmapBits() == 16 && m_pOrgRaster->GetIsGrayscale()) { m_SlidersControlsInfo.uMaxRange[0] = 65535; m_SlidersControlsInfo.uMaxRange[2] = 65533; m_SlidersControlsInfo.uMaxRange[3] = 65535; m_SlidersControlsInfo.uMaxLimitText[0] = 5; m_SlidersControlsInfo.uMaxLimitText[2] = 5; m_SlidersControlsInfo.uMaxLimitText[3] = 5; m_SlidersControlsInfo.uDefaultValue[0] = 6400; m_SlidersControlsInfo.uDefaultValue[2] = 0; m_SlidersControlsInfo.uDefaultValue[3] = 65535; m_SlidersControlsInfo.strDefaultValue[0] = _T("6400"); m_SlidersControlsInfo.strDefaultValue[2] = _T("0"); m_SlidersControlsInfo.strDefaultValue[3] = _T("65535"); } m_uThreshold[INNER_RGN] = m_uThreshold[OUTER_RGN] = m_SlidersControlsInfo.uDefaultValue[0]; m_uBlurRadius[INNER_RGN] = m_uBlurRadius[OUTER_RGN] = m_SlidersControlsInfo.uDefaultValue[1]; m_uMinThreshold[INNER_RGN] = m_uMinThreshold[OUTER_RGN] = m_SlidersControlsInfo.uDefaultValue[2]; m_uMaxThreshold[INNER_RGN] = m_uMaxThreshold[OUTER_RGN] = m_SlidersControlsInfo.uDefaultValue[3]; } } void CKfmnRgnDlg::UpdateRgnTextInfo() { CString strText = _T(""); strText.Format(_T("Threshold: %d\nBlur Radius: %d\nMin: %d\nMax: %d\nRemove Hole: %s\n(X, Y): %d, %d\nPixel Count: %d"), m_uThreshold[m_nSelectedRegion], m_uBlurRadius[m_nSelectedRegion], m_uMinThreshold[m_nSelectedRegion], m_uMaxThreshold[m_nSelectedRegion], (m_bRemoveHoles[m_nSelectedRegion] ? _T("True") : _T("False")), m_ptCenter[m_nSelectedRegion].x, m_ptCenter[m_nSelectedRegion].y, m_uPixelCount[m_nSelectedRegion]); if(m_nSelectedRegion == INNER_RGN) m_stcInnerText.SetWindowText(strText); else m_stcOuterText.SetWindowText(strText); } void CKfmnRgnDlg::UpdateControls() { CString strValue = _T(""); m_sldrThreshold.SetPos(m_uThreshold[m_nSelectedRegion]); strValue.Format(_T("%d"), m_uThreshold[m_nSelectedRegion]); m_fAcceptNewValues = FALSE; m_edtThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_sldrBlurRadius.SetPos(m_uBlurRadius[m_nSelectedRegion]); strValue.Format(_T("%d"), m_uBlurRadius[m_nSelectedRegion]); m_fAcceptNewValues = FALSE; m_edtBlurRadius.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_sldrMinThreshold.SetPos(m_uMinThreshold[m_nSelectedRegion]); strValue.Format(_T("%d"), m_uMinThreshold[m_nSelectedRegion]); m_fAcceptNewValues = FALSE; m_edtMinThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_sldrMaxThreshold.SetPos(m_uMaxThreshold[m_nSelectedRegion]); strValue.Format(_T("%d"), m_uMaxThreshold[m_nSelectedRegion]); m_fAcceptNewValues = FALSE; m_edtMaxThreshold.SetWindowText(strValue); m_fAcceptNewValues = TRUE; m_fAcceptNewValues = FALSE; m_chkRemoveHoles.SetCheck(m_bRemoveHoles[m_nSelectedRegion]); m_fAcceptNewValues = TRUE; UpdateRgnTextInfo(); } void CKfmnRgnDlg::OnInnerRgn() { if(!m_bInnerRegion) { m_bInnerRegion = TRUE; m_pMainView->m_ltRasView.SetAutoRepaint((TRUE)); m_nSelectedRegion = INNER_RGN; m_crFrame = RGB(255, 0, 0); HRGN hTmpRgn = NULL; if(m_hRgn) { hTmpRgn = CreateRectRgn(0, 0, 0, 0); CombineRgn(hTmpRgn, m_hRgn, 0, RGN_COPY); DeleteObject(m_hRgn); } m_hRgn = (HRGN)m_pMainView->m_ltRasView.GetRaster().GetRgnHandle(); m_pMainView->m_ltRasView.GetRaster().SetRgnHandle((long)hTmpRgn, 0, 0, L_RGN_SET); DeleteObject(hTmpRgn); UpdateControls(); ApplyFunction(); m_pMainView->m_ltRasView.SetAutoRepaint((TRUE)); } } void CKfmnRgnDlg::OnOuterRgn() { if(m_bInnerRegion) { m_bInnerRegion = FALSE; m_pMainView->m_ltRasView.SetAutoRepaint(FALSE); m_nSelectedRegion = OUTER_RGN; m_crFrame = RGB(0, 0, 255); HRGN hTmpRgn = NULL; if(m_hRgn) { hTmpRgn = CreateRectRgn(0, 0, 0, 0); CombineRgn(hTmpRgn, m_hRgn, 0, RGN_COPY); DeleteObject(m_hRgn); } m_hRgn = (HRGN)m_pMainView->m_ltRasView.GetRaster().GetRgnHandle(); m_pMainView->m_ltRasView.GetRaster().SetRgnHandle((long)hTmpRgn, 0, 0, L_RGN_SET); DeleteObject(hTmpRgn); UpdateControls(); ApplyFunction(); m_pMainView->m_ltRasView.SetAutoRepaint(true); } } void CKfmnRgnDlg::OnShowTemplate() { m_bShowTemplate = !m_bShowTemplate; ApplyFunction(); } void CKfmnRgnDlg::OnShowTrackFrame() { m_bShowTrackFrame = !m_bShowTrackFrame; ApplyFunction(); } void CKfmnRgnDlg::ConvertFloatToString(int flt, TCHAR szBuff[], int nPrecise) { int left, right; TCHAR lChar[10] = _T("\0"); TCHAR rChar[10] = _T("\0"); int nPercentage; nPercentage = (int)pow(10, nPrecise); left = flt / nPercentage; if (left) right = flt % (left * nPercentage); else right = flt; wsprintf(lChar, _T("%d"), left); if (right > 9) wsprintf(rChar, _T("%d"), right); else wsprintf(rChar, _T("0%d"), right); wsprintf(szBuff, _T("%s.%s"), lChar, rChar); } void CKfmnRgnDlg::CalculatePercentage() { TCHAR szStr[200]; TCHAR szRes[20]; wsprintf(szRes, _T("0.00")); if(m_uPixelCount[OUTER_RGN]) ConvertFloatToString(m_uPixelCount[INNER_RGN] * 10000 / m_uPixelCount[OUTER_RGN], szRes, 2); wsprintf(szStr, TEXT(" Area: %s %% (In / Out) - "), szRes); wsprintf(szRes, TEXT("0.00")); if (m_uPixelCount[INNER_RGN]) ConvertFloatToString(m_uPixelCount[OUTER_RGN] * 10000 / m_uPixelCount[INNER_RGN], szRes, 2); wsprintf(szStr, TEXT("%s%s %% (Out / In)"), szStr, szRes); m_stcArea.SetWindowText(szStr); } void CKfmnRgnDlg::OnClose() { KillTimer(m_nTimerID); UnhookWindowsHookEx(m_hMouseHook); if(m_pKfmnRgnDlg) m_pKfmnRgnDlg = NULL; CDialog::OnClose(); } LRESULT CKfmnRgnDlg::MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam) { if(m_pKfmnRgnDlg != NULL) { LPMOUSEHOOKSTRUCT pMouseHook; RECT rcRect; POINT TmpPoint; pMouseHook = (LPMOUSEHOOKSTRUCT)lParam; m_pKfmnRgnDlg->GetWindowRect(&rcRect); if (PtInRect(&rcRect, pMouseHook->pt)) return 0; switch(LOWORD(wParam)) { case WM_NCMOUSEMOVE: { TmpPoint = pMouseHook->pt; if(PtInRect(&rcRect, TmpPoint)) break; m_pKfmnRgnDlg->m_pMainView->ScreenToClient(&TmpPoint); CRect rcDst; rcDst.top = (long)m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstTop(); rcDst.left = (long)m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstLeft(); rcDst.bottom = (long)(m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstTop() + m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstHeight()); rcDst.right = (long)(m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstLeft() + m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstWidth()); (NULL, NULL, &rcDst, NULL); if(PtInRect(&rcDst, TmpPoint)) { m_pKfmnRgnDlg->m_pMainView->OnChangeCursor(IDC_CROSS); } else { m_pKfmnRgnDlg->m_pMainView->OnChangeCursor(IDC_ARROW); } } break; case WM_NCLBUTTONDOWN: { TmpPoint = pMouseHook->pt; if (PtInRect(&rcRect, TmpPoint)) break; if(m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetRaster().GetHasRgn()) { m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetRaster().FreeRgn(); } m_pKfmnRgnDlg->m_pMainView->OnChangeCursor(IDC_CROSS); m_pKfmnRgnDlg->m_pMainView->ScreenToClient(&pMouseHook->pt); CRect rcDst; rcDst.top = (long)m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstTop(); rcDst.left = (long)m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstLeft(); rcDst.bottom = (long)(m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstTop() + m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstHeight()); rcDst.right = (long)(m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstLeft() + m_pKfmnRgnDlg->m_pMainView->m_ltRasView.GetDstWidth()); if(PtInRect(&rcDst, pMouseHook->pt)) { m_pKfmnRgnDlg->m_ptCenter[m_pKfmnRgnDlg->m_nSelectedRegion].x = pMouseHook->pt.x - rcDst.left; m_pKfmnRgnDlg->m_ptCenter[m_pKfmnRgnDlg->m_nSelectedRegion].y = pMouseHook->pt.y - rcDst.top; m_pKfmnRgnDlg->ApplyFunction(); m_pKfmnRgnDlg->m_pMainView->OnChangeCursor(IDC_CROSS); } else { m_pKfmnRgnDlg->m_pMainView->OnChangeCursor(IDC_ARROW); } } break; case WM_LBUTTONUP: case WM_NCLBUTTONUP: { TmpPoint = pMouseHook->pt; if (PtInRect(&rcRect, TmpPoint)) break; m_pKfmnRgnDlg->m_pMainView->OnChangeCursor(IDC_ARROW); } break; } if (LOWORD(wParam) >= WM_NCMOUSEMOVE && LOWORD(wParam) <= WM_NCMBUTTONDBLCLK) return TRUE; else return CallNextHookEx(m_pKfmnRgnDlg->m_hMouseHook , nCode, wParam, lParam); } return FALSE; } void CKfmnRgnDlg::OnRemoveHoles() { m_bRemoveHoles[INNER_RGN] = !m_bRemoveHoles[INNER_RGN]; m_bRemoveHoles[OUTER_RGN] = !m_bRemoveHoles[OUTER_RGN]; ApplyFunction(); } void CKfmnRgnDlg::OnOK() { if(m_bShowTemplate) { m_pMainView->m_ltRasView.GetRaster().SetBitmap(m_pOrgRaster->GetBitmap()); } CDialog::OnOK(); }