15,979
社区成员
发帖
与我相关
我的任务
分享
#include <afxwin.h>
#include <afxext.h> // added
#include <winuser.h>
class CMyWnd:public CFrameWnd //主窗口框架类
{
CBitmap m_bitmapTest;
CRectTracker m_rtBitmap;
public:
CMyWnd(); //构造函数
protected:
//消息相应函数
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
public:
};
BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
class CMyApp:public CWinApp //主程序类
{
public:
BOOL InitInstance();
};
CMyApp ThisApp;
//////////////////////////////////////////////////////
BOOL CMyApp::InitInstance()
{
CMyWnd *pFrame=new CMyWnd;
pFrame->Create(0,_T("Test"));
pFrame->ShowWindow(m_nCmdShow);
this->m_pMainWnd=pFrame;
//
return TRUE;
}
CMyWnd::CMyWnd()
{
BITMAP bmInfo;
//
m_bitmapTest.Attach((HBITMAP)LoadImage(NULL, _T("test.bmp"), IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE));
m_bitmapTest.GetBitmap(&bmInfo);
SetRect(m_rtBitmap.m_rect,0, 0, bmInfo.bmWidth, bmInfo.bmHeight);//
}
BOOL CMyWnd::OnEraseBkgnd(CDC* pDC)
{
return true;
}
//
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
CDC dcMem, dcBmp;
CBitmap bitmapTemp;
CRect rectClient;
GetClientRect(&rectClient);
bitmapTemp.CreateCompatibleBitmap(&dc, rectClient.Width(), rectClient.Height());
dcMem.CreateCompatibleDC(&dc);
dcBmp.CreateCompatibleDC(&dc);
dcMem.SelectObject(&bitmapTemp);
dcBmp.SelectObject(&m_bitmapTest);
//
dcMem.SelectStockObject(WHITE_BRUSH);
dcMem.Rectangle(&rectClient);
//
dcMem.BitBlt(m_rtBitmap.m_rect.left, m_rtBitmap.m_rect.top,
m_rtBitmap.m_rect.Width(), m_rtBitmap.m_rect.Height(),
&dcBmp, 0, 0, SRCCOPY);
dc.BitBlt(0, 0, rectClient.right, rectClient.bottom, &dcMem, 0, 0, SRCCOPY);
}
//
void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rectSave;
m_rtBitmap.GetTrueRect(rectSave);
if (m_rtBitmap.HitTest(point) >= 0) //CRectTracker::hitTopLeft
{// normal tracking action, when tracker is "hit"
if(1)// m_enble
{// can move and resize
m_rtBitmap.Track(this, point);
Invalidate();
}
}
// CFrameWnd::OnLButtonDown(nFlags, point);
}
#include<afxwin.h>
#include<winuser.h>
class CMyWnd:public CFrameWnd //主窗口框架类
{
CBitmap m_bitmapTest;
CRect m_rtBitmap;
BOOL m_bIsPick;
CPoint m_ptPre;
public:
CMyWnd(); //构造函数
protected:
//消息相应函数
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnPaint();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nflags,CPoint point);//鼠标移动
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
BEGIN_MESSAGE_MAP(CMyWnd,CFrameWnd)
ON_WM_MOUSEMOVE()
ON_WM_ERASEBKGND()
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
class CMyApp:public CWinApp //主程序类
{
public:
BOOL InitInstance();
};
CMyApp ThisApp;
//////////////////////////////////////////////////////
BOOL CMyApp::InitInstance()
{
CMyWnd *pFrame=new CMyWnd;
pFrame->Create(0,_T("Test"));
pFrame->ShowWindow(m_nCmdShow);
this->m_pMainWnd=pFrame;
return TRUE;
}
CMyWnd::CMyWnd()
{
BITMAP bmInfo;
m_bIsPick = FALSE;
m_bitmapTest.Attach((HBITMAP)LoadImage(NULL, _T("test.bmp"), IMAGE_BITMAP, NULL, NULL, LR_LOADFROMFILE));
m_bitmapTest.GetBitmap(&bmInfo);
m_rtBitmap.SetRect(0, 0, bmInfo.bmWidth, bmInfo.bmHeight);
}
BOOL CMyWnd::OnEraseBkgnd(CDC* pDC)
{
return true;
}
void CMyWnd::OnPaint()
{
CPaintDC dc(this);
CDC dcMem, dcBmp;
CBitmap bitmapTemp;
CRect rectClient;
GetClientRect(&rectClient);
bitmapTemp.CreateCompatibleBitmap(&dc, rectClient.Width(), rectClient.Height());
dcMem.CreateCompatibleDC(&dc);
dcBmp.CreateCompatibleDC(&dc);
dcMem.SelectObject(&bitmapTemp);
dcBmp.SelectObject(&m_bitmapTest);
dcMem.SelectStockObject(WHITE_BRUSH);
dcMem.Rectangle(&rectClient);
dcMem.BitBlt(m_rtBitmap.left, m_rtBitmap.top, m_rtBitmap.Width(), m_rtBitmap.Height(), &dcBmp, 0, 0, SRCCOPY);
dc.BitBlt(0, 0, rectClient.right, rectClient.bottom, &dcMem, 0, 0, SRCCOPY);
}
void CMyWnd::OnLButtonDown(UINT nFlags, CPoint point)
{
if(m_rtBitmap.PtInRect(point))
{
m_bIsPick = TRUE;
m_ptPre = point;
SetCapture();
}
CFrameWnd::OnLButtonDown(nFlags, point);
}
void CMyWnd::OnLButtonUp(UINT nFlags, CPoint point)
{
m_bIsPick = FALSE;
ReleaseCapture();
CFrameWnd::OnLButtonUp(nFlags, point);
}
void CMyWnd::OnMouseMove(UINT nflags,CPoint point)
{
if(m_bIsPick)
{
m_rtBitmap.top += point.y-m_ptPre.y;
m_rtBitmap.bottom += point.y-m_ptPre.y;
m_rtBitmap.right += point.x-m_ptPre.x;
m_rtBitmap.left += point.x-m_ptPre.x;
m_ptPre = point;
Invalidate();
}
}
//我在MyTestDlg.h里定义了
CBitmap m_bitmap;
BITMAP bmInfo;
//这是我写的在对话框里显示图片的函数
void CMyTestDlg::OnPaint()
{
if (IsIconic())//判断当前窗口是否是最小化窗口
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CDC* pDC = GetDC();
CDC mDC;//定义内存DC
mDC.CreateCompatibleDC(pDC);//通过PDC创建内存DC,初始化
m_bitmap.LoadBitmap(IDB_BITMAP1);//加载图像
mDC.SelectObject(&m_bitmap);//把图像加载到内存DC
m_bitmap.GetBitmap(&bmInfo);//把图片信息存到bmInfo
pDC->BitBlt(0,0,bmInfo.bmWidth,bmInfo.bmHeight,&mDC,0,0,SRCCOPY);//绘制图片
ReleaseDC(pDC);//释放DC
CDialog::OnPaint();
}
}