Permalink
Browse files

Add the function of zoom image

Signed-off-by: kkfnui <kkfnui@gmail.com>
  • Loading branch information...
1 parent ea8608c commit d6df6479dd9fcc793c464229941335c4cb29ff35 @kkfnui committed Apr 7, 2012
Binary file not shown.
@@ -6,6 +6,7 @@
#include "resource.h"
#include "MainDlg.h"
+#include "MouseWheelFilter.h"
CAppModule _Module;
@@ -18,21 +19,26 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPTSTR /*
ATLASSERT(SUCCEEDED(hRes));
// this resolves ATL window thunking problem when Microsoft Layer for Unicode (MSLU) is used
- ::DefWindowProc(NULL, 0, 0, 0L);
+ //::DefWindowProc(NULL, 0, 0, 0L);
AtlInitCommonControls(ICC_BAR_CLASSES); // add flags to support other controls
hRes = _Module.Init(NULL, hInstance);
ATLASSERT(SUCCEEDED(hRes));
- int nRet = 0;
- // BLOCK: Run application
- {
- CMainDlg dlgMain;
- nRet = dlgMain.DoModal();
- }
-
- _Module.Term();
+ CMessageLoop theLoop;
+ CMessageFilter * filter = new CMouseWheelFilter();
+ theLoop.AddMessageFilter(filter);
+ _Module.AddMessageLoop(&theLoop);
+
+
+ CMainDlg dlgMain;
+ dlgMain.Create(NULL);
+ dlgMain.ShowWindow(SW_NORMAL);
+ int nRet = theLoop.Run();
+ delete filter;
+ _Module.RemoveMessageLoop();
+ //_Module.Term();
::CoUninitialize();
return nRet;
@@ -13,7 +13,7 @@
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
-// Chinese (P.R.C.) resources
+// ����(�л����񹲺͹�) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
#ifdef _WIN32
@@ -43,10 +43,10 @@ CAPTION "ImageLayers"
MENU IDR_MENU_BAR
FONT 8, "MS Sans Serif", 0, 0, 0x0
BEGIN
- DEFPUSHBUTTON "����",IDOK,7,7,50,14
- PUSHBUTTON "Cancel",IDCANCEL,65,7,50,14
- PUSHBUTTON "Save",ID_SAVE,127,7,50,14
- CONTROL "",IDC_LIST_IMAGE_LAYERS,"SysListView32",LVS_LIST | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER | WS_TABSTOP,522,7,102,414
+ DEFPUSHBUTTON "����",IDOK,7,7,50,14,NOT WS_TABSTOP
+ PUSHBUTTON "Cancel",IDCANCEL,65,7,50,14,NOT WS_TABSTOP
+ PUSHBUTTON "Save",ID_SAVE,127,7,50,14,NOT WS_TABSTOP
+ CONTROL "",IDC_LIST_IMAGE_LAYERS,"SysListView32",LVS_LIST | LVS_SHOWSELALWAYS | LVS_ALIGNLEFT | WS_BORDER,522,7,102,414
END
IDD_OPERATE DIALOGEX 0, 0, 386, 269
@@ -139,12 +139,12 @@ BEGIN
END
END
-#endif // Chinese (P.R.C.) resources
+#endif // ����(�л����񹲺͹�) resources
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
-// English (U.S.) resources
+// Ӣ��(���) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
@@ -336,7 +336,7 @@ BEGIN
ATL_IDS_MRU_FILE "Open this document"
END
-#endif // English (U.S.) resources
+#endif // Ӣ��(���) resources
/////////////////////////////////////////////////////////////////////////////
@@ -203,6 +203,10 @@
>
</File>
<File
+ RelativePath=".\MouseWheelFilter.cpp"
+ >
+ </File>
+ <File
RelativePath=".\OperateDlg.cpp"
>
</File>
@@ -244,6 +248,10 @@
>
</File>
<File
+ RelativePath=".\MouseWheelFilter.h"
+ >
+ </File>
+ <File
RelativePath=".\OperateDlg.h"
>
</File>
@@ -1,13 +1,16 @@
#include "StdAfx.h"
#include "Layer.h"
-CLayer::CLayer(void)
+CLayer::CLayer(void):
+m_dZoom(1.0)
{
}
-CLayer::CLayer(int nZIndex, HBITMAP hBitmap ):m_nZIndex(nZIndex),m_hBitmap(hBitmap)
+CLayer::CLayer(int nZIndex, HBITMAP hBitmap ):m_nZIndex(nZIndex)
+,m_hBitmap(hBitmap)
+,m_dZoom(1.0)
{
-
+ m_dZoom = 1.0;
}
CLayer::~CLayer(void)
@@ -27,3 +30,16 @@ CRect CLayer::GetArea() const
{
return m_rcArea;
}
+
+void CLayer::SetZoom( const float& zoom )
+{
+ m_dZoom = zoom;
+}
+
+float CLayer::GetZoom()
+{
+ return m_dZoom;
+}
+
+
+
@@ -7,11 +7,13 @@ class CLayer
CLayer(int nZIndex, HBITMAP hBitmap);
~CLayer(void);
void SetArea(const CRect& rect);
+ void SetZoom(const float& zoom);
+ float GetZoom();
CRect GetArea() const;
-
HBITMAP m_hBitmap;
private:
int m_nZIndex;
CRect m_rcArea;
+ double m_dZoom;
};
@@ -101,13 +101,19 @@ LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /
CRect rect;
rect.left = 50;
rect.top = 150;
- rect.bottom = 500;
- rect.right = 400;
+ int iHeight = lpBitmapInfo->bmiHeader.biHeight;
+ if (lpBitmapInfo->bmiHeader.biHeight < 0)
+ {
+ iHeight = -lpBitmapInfo->bmiHeader.biHeight;
+ }
+ rect.bottom = rect.top + iHeight ;
+ rect.right = rect.left + lpBitmapInfo->bmiHeader.biWidth;
layer->SetArea(rect);
- Invalidate();
m_Layers.push_back(layer);
+ Invalidate();
+
CString strTitle;
strTitle.Format(L"图片%d", m_Layers.size()-1);
int nId = m_list.InsertItem(m_Layers.size()-1, strTitle);
@@ -117,8 +123,9 @@ LRESULT CMainDlg::OnOK(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /
LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
- EndDialog(wID);
- return 0;
+ DestroyWindow();
+ ::PostQuitMessage(wID);
+ return S_OK;
}
HBITMAP CMainDlg::ConvertDIB2DDB( CDCHandle dc, LPBITMAPINFO bitmapInfo )
@@ -0,0 +1,23 @@
+#include "StdAfx.h"
+#include <WinUser.h>
+#include "MouseWheelFilter.h"
+
+CMouseWheelFilter::CMouseWheelFilter(void)
+{
+}
+
+CMouseWheelFilter::~CMouseWheelFilter(void)
+{
+}
+
+BOOL CMouseWheelFilter::PreTranslateMessage( MSG* pMsg )
+{
+ if (pMsg->message == WM_MOUSEWHEEL)
+ {
+ POINT pos;
+ GetCursorPos (&pos);
+ pMsg->hwnd = WindowFromPoint (pos);
+ pMsg->message = WM_MOUSEWHEEL;
+ }
+ return FALSE;
+}
@@ -0,0 +1,12 @@
+#pragma once
+#include "stdafx.h"
+
+class CMouseWheelFilter:public CMessageFilter
+{
+public:
+ CMouseWheelFilter(void);
+ ~CMouseWheelFilter(void);
+
+ virtual BOOL PreTranslateMessage( MSG* pMsg );
+
+};
@@ -4,7 +4,7 @@
#include "MainDlg.h"
#include "PropertyDlg.h"
-COperateDlg::COperateDlg(void)
+COperateDlg::COperateDlg(void):m_pSelectedLayer(NULL)
{
}
@@ -14,7 +14,7 @@ COperateDlg::~COperateDlg(void)
LRESULT COperateDlg::OnInitDialog( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
{
-
+ SetFocus();
return S_OK;
}
@@ -35,17 +35,29 @@ LRESULT COperateDlg::OnPaint( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*
bmp.CreateCompatibleBitmap(dc, rcClient.Width(), rcClient.Height());
bmpOld = SelectObject(dcMemory, bmp);
CDC cdc(dcMemory);
-
- cdc.DrawFocusRect(&rcClient);
+ cdc.DrawFocusRect(&rcClient);
cdc.FillSolidRect(&rcCaven, RGB(240, 240, 240));
std::vector<CLayer*>::iterator it = m_DlgMain->m_Layers.begin();
HDC hdc = CreateCompatibleDC(dc);
for (;it != m_DlgMain->m_Layers.end(); ++it)
{
- HGDIOBJ hOld =SelectObject(hdc, (*it)->m_hBitmap);
- CRect rect = (*it)->GetArea();
- BitBlt(dcMemory, rect.left, rect.top, rect.Width(), rect.Height(), hdc, 0, 0, SRCCOPY);
- SelectObject(hdc, hOld);
+ HGDIOBJ hOld =SelectObject(hdc, (*it)->m_hBitmap);
+ CRect rect = (*it)->GetArea();
+
+ float zoom = (*it)->GetZoom();
+ if(zoom != 1.0)
+ {
+ CRect rcDes = ZoomRect(rect, zoom);
+ //SetStretchBltMode(hdc, COLORONCOLOR);
+ StretchBlt(dcMemory, rcDes.left, rcDes.top, rcDes.Width(), rcDes.Height()
+ , hdc, 0, 0, rect.Width(), rect.Height(), SRCCOPY);
+ }
+ else
+ {
+ BitBlt(dcMemory, rect.left, rect.top, rect.Width(), rect.Height(), hdc, 0, 0, SRCCOPY);
+ }
+ SelectObject(hdc, hOld);
+
}
DeleteObject(hdc);
BitBlt(dc, rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), dcMemory, 0, 0, SRCCOPY);
@@ -59,6 +71,7 @@ LRESULT COperateDlg::OnPaint( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*
LRESULT COperateDlg::OnLeftButtonDown( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM lParam, BOOL& /*bHandled*/ )
{
+ SetFocus();
CPoint pt(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
if (m_Statu == PREMOVE)
{
@@ -288,3 +301,33 @@ BOOL COperateDlg::SaveBitmapFile( CString strFileName, CBitmap &bmp, CDC *pdc )
return TRUE;
}
+
+LRESULT COperateDlg::OnMouseWheel(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
+{
+ WORD fwKeys = GET_KEYSTATE_WPARAM(wParam);
+ WORD zDelta = GET_WHEEL_DELTA_WPARAM(wParam);
+ if (fwKeys == MK_CONTROL && m_pSelectedLayer != NULL)
+ {
+ float fZoom =m_pSelectedLayer->GetZoom();
+ fZoom = zDelta == 120 ? fZoom / 0.9 : fZoom * 0.9;
+ m_pSelectedLayer->SetZoom(fZoom);
+ Invalidate(FALSE);
+ }
+
+ return 0;
+}
+
+CRect COperateDlg::ZoomRect( const CRect& oldRect, const float& zoom )
+{
+ long x = oldRect.left + oldRect.Width()/2;
+ long y = oldRect.top + oldRect.Height()/2;
+ CPoint middle(x, y);
+ int iWidth = oldRect.Width()*zoom;
+ int iHeght = oldRect.Height()*zoom;
+ CRect rcNew;
+ rcNew.left = x - iWidth/2;
+ rcNew.top = y - iHeght/2;
+ rcNew.right = rcNew.left + iWidth;
+ rcNew.bottom = rcNew.top + iHeght;
+ return rcNew;
+}
@@ -19,7 +19,8 @@ class COperateDlg : public CDialogImpl<COperateDlg>
MESSAGE_HANDLER(ID_SELECT, OnSelect)
MESSAGE_HANDLER(ID_SAVE, OnSave)
COMMAND_ID_HANDLER(ID_PROPERTY, OnSetProperty)
- END_MSG_MAP()
+ MESSAGE_HANDLER(WM_MOUSEWHEEL, OnMouseWheel)
+ END_MSG_MAP()
LRESULT OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
@@ -33,10 +34,14 @@ class COperateDlg : public CDialogImpl<COperateDlg>
void SetMainDlg(CMainDlg * dlg);
void SetStatus(Status statu);
BOOL SaveBitmapFile(CString strFileName, CBitmap &bmp, CDC *pdc);
+private:
+ CRect ZoomRect(const CRect& oldRect, const float& zoom);
private:
CPoint m_ptLastClicked;
CMainDlg * m_DlgMain;
Status m_Statu;
CLayer* m_pSelectedLayer;
+public:
+ LRESULT OnMouseWheel(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
};
Binary file not shown.

0 comments on commit d6df647

Please sign in to comment.