VC實(shí)現(xiàn)圖片拖拽及動(dòng)畫(huà)的實(shí)例
基礎(chǔ)知識(shí)
1.PictureBox控件的使用
2.加載位圖文件
1.通過(guò)文件路徑獲得位圖句柄
//獲得位圖句柄
void CMovePictureDlg::GetHandleFromPath(CString path)
{
hBitmap= (HBITMAP)::LoadImage(AfxGetInstanceHandle(),path,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);//創(chuàng)建位圖句柄
}
2.通過(guò)位圖句柄創(chuàng)建位圖對(duì)象并獲得位圖信息
//獲取位圖對(duì)象
void CMovePictureDlg::GetBitMap(HBITMAP hBitmap)
{
m_BitMap.Attach(hBitmap);//通過(guò)位圖句柄創(chuàng)建位圖對(duì)象
//獲取圖像信息
BITMAPINFOHEADER bminfo;
m_BitMap.GetObject(sizeof(bminfo),&bminfo);
//獲取位圖寬高
m_nBmpWidth=bminfo.biWidth;
m_nBmpHeight=bminfo.biHeight;
}
實(shí)現(xiàn)步驟:
1.創(chuàng)建一個(gè)對(duì)話框工程命名為MovePicture
2.打開(kāi)對(duì)話框資源拖入一個(gè)PictureBox控件,設(shè)置ID為:IDC_PICTUREBOX,設(shè)置類(lèi)型為:位圖
3.拖入兩個(gè)靜態(tài)文本控件和兩個(gè)編輯框控件,靜態(tài)文本控件標(biāo)題分別為:輸入動(dòng)量系數(shù):、輸入阻力系數(shù):,編輯框ID分別為:IDC_EDITDV、IDC_EDITF,為IDC_EDITDV關(guān)聯(lián)變量為:m_editDV,為IDC_EDITF關(guān)聯(lián)變量為:m_editF
4.實(shí)現(xiàn)代碼
頭文件
// MovePictureDlg.h : header file
//
#include "PictureBox.h"
#if !defined(AFX_MOVEPICTUREDLG_H__6FFC1DDF_478C_43D6_B854_4D51E98D5E50__INCLUDED_)
#define AFX_MOVEPICTUREDLG_H__6FFC1DDF_478C_43D6_B854_4D51E98D5E50__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CMovePictureDlg dialog
class CMovePictureDlg : public CDialog
{
// Construction
public:
void GetBitMap(HBITMAP hBitmap);
void GetHandleFromPath(CString path);
void SetPicRect(int x,int y);
void SetSysPath();
CMovePictureDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CMovePictureDlg)
enum { IDD = IDD_MOVEPICTURE_DIALOG };
CEdit m_editF;
CEdit m_editDV;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMovePictureDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CMovePictureDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnDestroy();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CString sysPath;//保存系統(tǒng)路徑
CString bmpPath;//位圖路徑
HBITMAP hBitmap;//位圖句柄
CBitmap m_BitMap;//位圖對(duì)象
int m_nBmpWidth;//位圖寬度
int m_nBmpHeight;//位圖高度
CPictureBox* pictureBox;//圖片控件
CRect picRect;//控件所占區(qū)域
BOOL isSelect;//判斷控件是否被選中
BOOL isDown;//判斷鼠標(biāo)是否按下
CPoint oldPoint;//鼠標(biāo)原始位置
CPoint startPoint;//移動(dòng)起始位置
SYSTEMTIME startTime;//開(kāi)始時(shí)間
SYSTEMTIME endTime;//結(jié)束時(shí)間
double vx;//x方向速度
double vy;//y方向速度
double f;//阻力
double dv;//動(dòng)力增量
CRect clientRect;//客戶(hù)區(qū)域
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_MOVEPICTUREDLG_H__6FFC1DDF_478C_43D6_B854_4D51E98D5E50__INCLUDED_)
實(shí)現(xiàn)文件
// MovePictureDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MovePicture.h"
#include "MovePictureDlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
//計(jì)算時(shí)間差
inline __int64 TimeDiff(SYSTEMTIME left,SYSTEMTIME right)
{
CTime tmLeft(left.wYear,left.wMonth,left.wDay,0,0,0);
CTime tmRight(left.wYear,left.wMonth,left.wDay,0,0,0);
CTimeSpan sp;
sp = tmLeft - tmRight;//計(jì)算日期比較麻煩,就交給MFC去做吧
long lLMinllis = (left.wHour*3600 + left.wMinute*60 + left.wSecond)*1000 + left.wMilliseconds;
long lRMinllis = (right.wHour*3600 + right.wMinute*60 + right.wSecond)*1000 + right.wMilliseconds;
return (__int64)sp.GetDays()*86400000 + (lLMinllis - lRMinllis);
}
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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMovePictureDlg dialog
CMovePictureDlg::CMovePictureDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMovePictureDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMovePictureDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
isDown = FALSE;//初始化鼠標(biāo)按下?tīng)顟B(tài)
isSelect = FALSE;//初始化選中狀態(tài)
f = 0.05;//初始化阻力
dv = 5;//初始化動(dòng)力增量
}
void CMovePictureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMovePictureDlg)
DDX_Control(pDX, IDC_EDITF, m_editF);
DDX_Control(pDX, IDC_EDITDV, m_editDV);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMovePictureDlg, CDialog)
//{{AFX_MSG_MAP(CMovePictureDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMovePictureDlg message handlers
//對(duì)話框初始化
BOOL CMovePictureDlg::OnInitDialog()
{
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
pictureBox = (CPictureBox*)GetDlgItem(IDC_PICTUREBOX);//獲得圖片框指針
SetSysPath();//設(shè)置系統(tǒng)路徑
bmpPath = sysPath+"http://image.bmp";//設(shè)置位圖路徑
GetHandleFromPath(bmpPath);//創(chuàng)建位圖句柄
GetBitMap(hBitmap);//獲得位圖
//設(shè)置位圖控件區(qū)域
SetPicRect(0,0);
pictureBox->MoveWindow(picRect);//設(shè)置控件位置
//設(shè)置文本框值
CString strDV,strF;
strDV.Format("%f",dv);
strF.Format("%f",f);
m_editDV.SetWindowText(strDV);
m_editF.SetWindowText(strF);
return TRUE; // return TRUE unless you set the focus to a control
}
void CMovePictureDlg::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.
//重繪函數(shù)
void CMovePictureDlg::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
{
this->GetClientRect(&clientRect);//獲得客戶(hù)區(qū)域大小
pictureBox->SetBitmap(hBitmap);//顯示位圖
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMovePictureDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//鼠標(biāo)左鍵按下
void CMovePictureDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
isDown=TRUE;//鼠標(biāo)按下
if(PtInRect(picRect,point))//鼠標(biāo)在圖片區(qū)域按下
{
isSelect=TRUE;//圖片選中
oldPoint = point;//記錄下鼠標(biāo)位置
startPoint = point;//記錄下鼠標(biāo)起始位置
GetSystemTime(&startTime);//得到當(dāng)前時(shí)間
}
CDialog::OnLButtonDown(nFlags, point);
}
//鼠標(biāo)左鍵彈起
void CMovePictureDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
isDown=FALSE;//鼠標(biāo)彈起
if(isSelect)
{
CString strDV,strF;
m_editDV.GetWindowText(strDV);
m_editF.GetWindowText(strF);
dv = atof(strDV);
f = atof(strF);
isSelect=FALSE;//釋放圖片
GetSystemTime(&endTime);//得到當(dāng)前時(shí)間
double dx = point.x-startPoint.x;//計(jì)算x軸位移
double dy = point.y-startPoint.y;//計(jì)算y軸位移
int s = TimeDiff(endTime,startTime);//計(jì)算時(shí)間差
//double r = sqrt(dx*dx+dy*dy);//計(jì)算鼠標(biāo)位移長(zhǎng)度
//double v=r/s;//求平均速度
vx=dv*dx/s;//x軸平均速度
vy=dv*dy/s;//y軸平均速度
SetTimer(1,1,NULL);//設(shè)置定時(shí)器
}
CDialog::OnLButtonUp(nFlags, point);
}
//鼠標(biāo)移動(dòng)
void CMovePictureDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(isDown&&isSelect)//在鼠標(biāo)按下?tīng)顟B(tài)下移動(dòng)
{
int dx = point.x-oldPoint.x;//計(jì)算x值的相對(duì)變化
int dy = point.y-oldPoint.y;//計(jì)算y值的相對(duì)變化
//重新設(shè)置PictureBox位置
picRect.left=picRect.left+dx;
picRect.top=picRect.top+dy;
SetPicRect(picRect.left,picRect.top);//重置圖片位置
pictureBox->MoveWindow(picRect.left,picRect.top,m_nBmpWidth,m_nBmpHeight);//改變圖片框大小
oldPoint=point;//重置原始坐標(biāo)
}
CDialog::OnMouseMove(nFlags, point);
}
//設(shè)置系統(tǒng)路徑
void CMovePictureDlg::SetSysPath()
{
//獲取當(dāng)前路徑,保存到字符數(shù)組strBuff中
char strBuff[256];
GetCurrentDirectory(256,strBuff);
sysPath.Format("%s",strBuff);//將路徑保存到全局變量中
}
//設(shè)置控件區(qū)域
void CMovePictureDlg::SetPicRect(int x, int y)
{
picRect.top=y;
picRect.bottom=picRect.top+m_nBmpHeight;
picRect.left=x;
picRect.right=picRect.left+m_nBmpWidth;
}
//獲得位圖句柄
void CMovePictureDlg::GetHandleFromPath(CString path)
{
hBitmap= (HBITMAP)::LoadImage(AfxGetInstanceHandle(),path,IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION|LR_LOADFROMFILE);//創(chuàng)建位圖句柄
}
//獲取位圖對(duì)象
void CMovePictureDlg::GetBitMap(HBITMAP hBitmap)
{
m_BitMap.Attach(hBitmap);//通過(guò)位圖句柄創(chuàng)建位圖對(duì)象
//獲取圖像信息
BITMAPINFOHEADER bminfo;
m_BitMap.GetObject(sizeof(bminfo),&bminfo);
//獲取位圖寬高
m_nBmpWidth=bminfo.biWidth;
m_nBmpHeight=bminfo.biHeight;
}
//定時(shí)器事件
void CMovePictureDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(abs(vx)>abs(vy))//當(dāng)水平速度大于垂直速度時(shí),只改變水平方向增量
{
if(picRect.left==clientRect.left)
{
vx=-vx;
}
if(picRect.left==clientRect.right-m_nBmpWidth)
{
vx=-vx;
}
picRect.left+=vx;
}
else if(abs(vx)<abs(vy))//當(dāng)水平速度小于垂直速度時(shí),只改變垂直方向增量
{
if(picRect.top==clientRect.top)
{
vy=-vy;
}
if(picRect.top==clientRect.bottom-m_nBmpHeight)
{
vy=-vy;
}
picRect.top+=vy;
}
//邊界判斷
if(picRect.left<clientRect.left)
{
picRect.left=clientRect.left;
}
if(picRect.left>clientRect.right-m_nBmpWidth)
{
picRect.left=clientRect.right-m_nBmpWidth;
}
if(picRect.top<clientRect.top)
{
picRect.top=clientRect.top;
}
if(picRect.top>clientRect.bottom-m_nBmpHeight)
{
picRect.top=clientRect.bottom-m_nBmpHeight;
}
pictureBox->MoveWindow(picRect.left,picRect.top,m_nBmpWidth,m_nBmpHeight);//改變圖片框大小
//增量遞減,勻減速運(yùn)動(dòng)
if(vx>0)
{
vx-=f;
if(vx<0)
{
vx=0;
}
}
else if(vx<0)
{
vx+=f;
if(vx>0)
{
vx=0;
}
}
if(vy>0)
{
vy-=f;
if(vy<0)
{
vy=0;
}
}
else if(vy<0)
{
vy+=f;
if(vy>0)
{
vy=0;
}
}
SetPicRect(picRect.left,picRect.top);//重置圖片位置
CDialog::OnTimer(nIDEvent);
}
void CMovePictureDlg::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
this->KillTimer(1);//銷(xiāo)毀定時(shí)器
}
1.在對(duì)話框中添加一PictureBox控件,設(shè)置ID為IDC_PICTUREBOX,類(lèi)型為位圖
2.創(chuàng)建CPictureBox類(lèi)繼承于CStatic
3.在對(duì)話框類(lèi)中聲明
CPictureBox* pictureBox;//圖片控件
4.在對(duì)話框類(lèi)的OnInitDialog方法中獲得控件指針
pictureBox = (CPictureBox*)GetDlgItem(IDC_PICTUREBOX);//獲得圖片框指針
5.顯示位圖
pictureBox->SetBitmap(hBitmap);//顯示位圖
6.設(shè)置控件位置
pictureBox->MoveWindow(picRect);//設(shè)置控件位置
欄 目:C語(yǔ)言
本文標(biāo)題:VC實(shí)現(xiàn)圖片拖拽及動(dòng)畫(huà)的實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/4218.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計(jì)-用棧實(shí)現(xiàn)表達(dá)式求值的方法詳解
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項(xiàng)的七種實(shí)現(xiàn)方法
- 01-10C語(yǔ)言 解決不用+、-、&#215;、&#247;數(shù)字運(yùn)算符做加法
- 01-10使用C++實(shí)現(xiàn)全排列算法的方法詳解
- 01-10用C++實(shí)現(xiàn)DBSCAN聚類(lèi)算法
- 01-10深入全排列算法及其實(shí)現(xiàn)方法
- 01-10全排列算法的非遞歸實(shí)現(xiàn)與遞歸實(shí)現(xiàn)的方法(C++)
- 01-10用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(一)
- 01-10用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(二)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 04-02jquery與jsp,用jquery
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子