一个图片或者图片按钮?在线等。。。 我想做个小程序,在特定的一个范围内有张图,(但是看不见有按钮的边框)单击时在原位置换张图,并执行自定义的一些操作,再次单击显示原图,不知道MFC怎么能够实现?希望牛人仔细给小弟讲解一下。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 哦,你以前做过mfc编程没就是插入图片控件,响应消息,换图片资源CDC::BitBlt()CDC:StrechBlt() 先确定图显示在什么地方? Button上还是视图中?button按钮上:自绘按纽,处理button消息bn_clicked更换图片视图中:定义图片显示Rect,使用BitBlt,StrechBlt绘制图片.在视图中处理mouse消息.当接到WM_LBUTTONDOWN消息时,使用PtInRect判断鼠标是否击在图片上,如果返回TRUE,执行你要的操作。 加载进程序就可以了。把button关联一个FaceButton类。///////////////.cpp文件#include "stdafx.h"#include "FaceButton.h"#include "resource.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CFaceButtonCFaceButton::CFaceButton(){ m_bTracking=FALSE; m_bHover=FALSE; m_bitmapButtonDown.LoadBitmap(IDB_BITMAP_BUTTONDOWN); m_bitmapButtonHot.LoadBitmap(IDB_BITMAP_BUTTONHOT); m_bitmapButtonNormal.LoadBitmap(IDB_BITMAP_BUTTONNORMAL);// m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONFOCUS); m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONNORMAL);}CFaceButton::~CFaceButton(){}BEGIN_MESSAGE_MAP(CFaceButton, CButton) //{{AFX_MSG_MAP(CFaceButton) ON_WM_MOUSEMOVE() ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave) ON_MESSAGE(WM_MOUSEHOVER,OnMouseHover) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CFaceButton message handlersvoid CFaceButton::PreSubclassWindow() { // TODO: Add your specialized code here and/or call the base class CButton::PreSubclassWindow(); ModifyStyle(0,BS_OWNERDRAW);}void CFaceButton::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(!m_bTracking) { TRACKMOUSEEVENT tme; tme.cbSize=sizeof(tme); tme.dwFlags=TME_HOVER | TME_LEAVE; tme.hwndTrack=m_hWnd; tme.dwHoverTime=1; m_bTracking=_TrackMouseEvent(&tme); } CButton::OnMouseMove(nFlags, point);}LRESULT CFaceButton::OnMouseLeave(WPARAM wParam,LPARAM lParam){ m_bHover=FALSE; m_bTracking=FALSE; InvalidateRect(NULL,FALSE); return 0;}LRESULT CFaceButton::OnMouseHover(WPARAM wParam,LPARAM lParam){ m_bHover=TRUE; InvalidateRect(NULL); return 0;}BOOL CFaceButton::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) { // TODO: Add your specialized code here and/or call the base class BOOL bResult=CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext); m_bitmapButtonDown.LoadBitmap(IDB_BITMAP_BUTTONDOWN); m_bitmapButtonHot.LoadBitmap(IDB_BITMAP_BUTTONHOT); m_bitmapButtonNormal.LoadBitmap(IDB_BITMAP_BUTTONNORMAL);// m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONFOCUS); m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONNORMAL); return bResult; }void CFaceButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) { // TODO: Add your code to draw the specified item CRect rcItem=lpDrawItemStruct->rcItem; CDC* pDC=(CDC* )CDC::FromHandle(lpDrawItemStruct->hDC);//return point int nDCState=pDC->SaveDC(); TCHAR strIltemText[MAX_PATH+1]; GetWindowText(strIltemText,MAX_PATH); UINT uItemState=lpDrawItemStruct->itemState; if(m_bHover) { if(uItemState & ODS_SELECTED) { m_fsButtonState=FACE_DOWN; } else { m_fsButtonState=FACE_HOT; } } else { if(uItemState & ODS_FOCUS) { m_fsButtonState=FACE_FOCUS; } else { m_fsButtonState=FACE_NORMAL; } } DrawItemBitmap(pDC,&rcItem); CPoint pointBitmap(0,0); CSize sizeBitmap(71,22); switch(m_fsButtonState) { case FACE_DOWN: pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonDown,DST_COMPLEX,(HBRUSH)NULL);//显示图像并应用可视化效果来表示一个状态 break; case FACE_HOT: pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonHot,DST_COMPLEX,(HBRUSH)NULL); break; case FACE_FOCUS: pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonFocus,DST_COMPLEX,(HBRUSH)NULL); break; case FACE_NORMAL: pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonNormal,DST_COMPLEX,(HBRUSH)NULL); break; } if(strIltemText!=NULL) { CFont* pFont=GetFont(); CFont* pOldFont=pDC->SelectObject(pFont); CSize textExtend=pDC->GetTextExtent(strIltemText,lstrlen(strIltemText)); CPoint textPoint(rcItem.CenterPoint().x-textExtend.cx/2,rcItem.CenterPoint().y-textExtend.cy/2); if(m_fsButtonState & FACE_DOWN) textPoint.Offset(1,1); int nOldBkMode=pDC->SetBkMode(TRANSPARENT); if(uItemState & ODS_DISABLED) { pDC->DrawState(textPoint,textExtend,strIltemText,DSS_DISABLED,TRUE,0,(HBRUSH)NULL); } else { pDC->DrawState(textPoint,textExtend,strIltemText,DSS_NORMAL,TRUE,0,(HBRUSH)NULL); } pDC->SetBkMode(nOldBkMode); pDC->SelectObject(pOldFont); } pDC->RestoreDC(nDCState);}void CFaceButton::DrawItemBitmap(CDC* pDC, CRect* lpItemRect){ //CRect rc; //GetClientRect(&rc); CDC dcCompatible; CBitmap* pOldBitmap=NULL; dcCompatible.CreateCompatibleDC(pDC); switch(m_fsButtonState) { case FACE_DOWN: pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonDown); //pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonDown,DST_COMPLEX,(HBRUSH)NULL); break; case FACE_HOT: pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonHot); //pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonHot,DST_COMPLEX,(HBRUSH)NULL); break; case FACE_FOCUS: pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonFocus); //pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonFocus,DST_COMPLEX,(HBRUSH)NULL); break; case FACE_NORMAL: pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonNormal); //pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonNormal,DST_COMPLEX,(HBRUSH)NULL); break; } pDC->BitBlt(lpItemRect->left, lpItemRect->top, lpItemRect->Width(), lpItemRect->Height(), &dcCompatible, 0, 0, SRCCOPY); dcCompatible.SelectObject(pOldBitmap);}//////////.h文件#if !defined(AFX_FACEBUTTON_H__56B0E787_C68E_413F_AA51_E9E3F0C8FD27__INCLUDED_)#define AFX_FACEBUTTON_H__56B0E787_C68E_413F_AA51_E9E3F0C8FD27__INCLUDED_#if _MSC_VER > 1000#pragma once#endif // _MSC_VER > 1000// FaceButton.h : header file///////////////////////////////////////////////////////////////////////////////// CFaceButton windowclass CFaceButton : public CButton{// Constructionpublic: CFaceButton();// Attributespublic: enum FACESTATE{FACE_NORMAL=0x00000001,FACE_HOT=0x0000002,FACE_DOWN=0x00000004,FACE_FOCUS=0x00000008};private: BOOL m_bTracking; BOOL m_bHover; CBitmap m_bitmapButtonHot; CBitmap m_bitmapButtonNormal; CBitmap m_bitmapButtonFocus; CBitmap m_bitmapButtonDown; FACESTATE m_fsButtonState;// Operationspublic: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CFaceButton) public: virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL); virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct); protected: virtual void PreSubclassWindow(); //}}AFX_VIRTUALprivate: void DrawItemBitmap(CDC* pDC, CRect* lpItemRect);// Implementationpublic: virtual ~CFaceButton(); // Generated message map functionsprotected: //{{AFX_MSG(CFaceButton) afx_msg void OnMouseMove(UINT nFlags, CPoint point); afx_msg LRESULT OnMouseLeave(WPARAM wParam,LPARAM lParam); afx_msg LRESULT OnMouseHover(WPARAM wParam,LPARAM lParam); afx_msg LRESULT OnImitateStateMsgHandler(WPARAM wParam,LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP()};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_FACEBUTTON_H__56B0E787_C68E_413F_AA51_E9E3F0C8FD27__INCLUDED_) 在客户窗口的一个区域画一张图,设置一个状态量i,然后响应这一区域的鼠标消息,当i=0是持行XX操作,当i=1持行复位图片操作就行了呀 为什么我写的SetTimer函数回调到Cancel函数里 CEditView做记事本如何在另存为添加文件类型呢? 关于打印预览,急,谢谢 关于检测键盘的全局钩子函数 启动禁用网卡.. 怎样共享COM服务的内存?(作成进程外服务) 在对话框中怎么获得view的变量,在线等. visual C++编译问题? 有关链表的问题 vc6.0中如何设置行列个数,怎样控制文本显示 C++下,有没好的结合CDialog类来实现的观察者模式实现?听说JAVA下有很成功的例子!VC++下那里有? ATL开发的COM组建的问题,IE报出异常
就是插入图片控件,响应消息,换图片资源CDC::BitBlt()
CDC:StrechBlt()
button按钮上:自绘按纽,处理button消息bn_clicked更换图片
视图中:定义图片显示Rect,使用BitBlt,StrechBlt绘制图片.在视图中处理mouse消息.当接到WM_LBUTTONDOWN消息时,使用PtInRect判断鼠标是否击在图片上,如果返回TRUE,执行你要的操作。
///////////////.cpp文件
#include "stdafx.h"
#include "FaceButton.h"
#include "resource.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CFaceButtonCFaceButton::CFaceButton()
{
m_bTracking=FALSE;
m_bHover=FALSE;
m_bitmapButtonDown.LoadBitmap(IDB_BITMAP_BUTTONDOWN);
m_bitmapButtonHot.LoadBitmap(IDB_BITMAP_BUTTONHOT);
m_bitmapButtonNormal.LoadBitmap(IDB_BITMAP_BUTTONNORMAL);
// m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONFOCUS);
m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONNORMAL);
}CFaceButton::~CFaceButton()
{
}
BEGIN_MESSAGE_MAP(CFaceButton, CButton)
//{{AFX_MSG_MAP(CFaceButton)
ON_WM_MOUSEMOVE()
ON_MESSAGE(WM_MOUSELEAVE,OnMouseLeave)
ON_MESSAGE(WM_MOUSEHOVER,OnMouseHover)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CFaceButton message handlersvoid CFaceButton::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CButton::PreSubclassWindow();
ModifyStyle(0,BS_OWNERDRAW);
}void CFaceButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(!m_bTracking)
{
TRACKMOUSEEVENT tme;
tme.cbSize=sizeof(tme);
tme.dwFlags=TME_HOVER | TME_LEAVE;
tme.hwndTrack=m_hWnd;
tme.dwHoverTime=1;
m_bTracking=_TrackMouseEvent(&tme);
}
CButton::OnMouseMove(nFlags, point);
}LRESULT CFaceButton::OnMouseLeave(WPARAM wParam,LPARAM lParam)
{
m_bHover=FALSE;
m_bTracking=FALSE;
InvalidateRect(NULL,FALSE);
return 0;
}LRESULT CFaceButton::OnMouseHover(WPARAM wParam,LPARAM lParam)
{
m_bHover=TRUE;
InvalidateRect(NULL);
return 0;
}BOOL CFaceButton::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
BOOL bResult=CWnd::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext);
m_bitmapButtonDown.LoadBitmap(IDB_BITMAP_BUTTONDOWN);
m_bitmapButtonHot.LoadBitmap(IDB_BITMAP_BUTTONHOT);
m_bitmapButtonNormal.LoadBitmap(IDB_BITMAP_BUTTONNORMAL);
// m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONFOCUS);
m_bitmapButtonFocus.LoadBitmap(IDB_BITMAP_BUTTONNORMAL); return bResult;
}void CFaceButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
CRect rcItem=lpDrawItemStruct->rcItem;
CDC* pDC=(CDC* )CDC::FromHandle(lpDrawItemStruct->hDC);//return point
int nDCState=pDC->SaveDC();
TCHAR strIltemText[MAX_PATH+1];
GetWindowText(strIltemText,MAX_PATH);
UINT uItemState=lpDrawItemStruct->itemState; if(m_bHover)
{
if(uItemState & ODS_SELECTED)
{
m_fsButtonState=FACE_DOWN;
}
else
{
m_fsButtonState=FACE_HOT;
}
}
else
{
if(uItemState & ODS_FOCUS)
{
m_fsButtonState=FACE_FOCUS;
}
else
{
m_fsButtonState=FACE_NORMAL;
}
}
DrawItemBitmap(pDC,&rcItem);
CPoint pointBitmap(0,0);
CSize sizeBitmap(71,22);
switch(m_fsButtonState)
{
case FACE_DOWN:
pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonDown,DST_COMPLEX,(HBRUSH)NULL);//显示图像并应用可视化效果来表示一个状态
break;
case FACE_HOT:
pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonHot,DST_COMPLEX,(HBRUSH)NULL);
break;
case FACE_FOCUS:
pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonFocus,DST_COMPLEX,(HBRUSH)NULL);
break;
case FACE_NORMAL:
pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonNormal,DST_COMPLEX,(HBRUSH)NULL);
break;
}
if(strIltemText!=NULL)
{
CFont* pFont=GetFont();
CFont* pOldFont=pDC->SelectObject(pFont);
CSize textExtend=pDC->GetTextExtent(strIltemText,lstrlen(strIltemText));
CPoint textPoint(rcItem.CenterPoint().x-textExtend.cx/2,rcItem.CenterPoint().y-textExtend.cy/2);
if(m_fsButtonState & FACE_DOWN)
textPoint.Offset(1,1);
int nOldBkMode=pDC->SetBkMode(TRANSPARENT);
if(uItemState & ODS_DISABLED)
{
pDC->DrawState(textPoint,textExtend,strIltemText,DSS_DISABLED,TRUE,0,(HBRUSH)NULL);
}
else
{
pDC->DrawState(textPoint,textExtend,strIltemText,DSS_NORMAL,TRUE,0,(HBRUSH)NULL);
}
pDC->SetBkMode(nOldBkMode);
pDC->SelectObject(pOldFont);
} pDC->RestoreDC(nDCState);}void CFaceButton::DrawItemBitmap(CDC* pDC, CRect* lpItemRect)
{
//CRect rc;
//GetClientRect(&rc);
CDC dcCompatible;
CBitmap* pOldBitmap=NULL;
dcCompatible.CreateCompatibleDC(pDC);
switch(m_fsButtonState) {
case FACE_DOWN:
pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonDown);
//pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonDown,DST_COMPLEX,(HBRUSH)NULL);
break;
case FACE_HOT:
pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonHot);
//pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonHot,DST_COMPLEX,(HBRUSH)NULL);
break;
case FACE_FOCUS:
pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonFocus);
//pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonFocus,DST_COMPLEX,(HBRUSH)NULL);
break;
case FACE_NORMAL:
pOldBitmap = dcCompatible.SelectObject(&m_bitmapButtonNormal);
//pDC->DrawState(pointBitmap,sizeBitmap,(HBITMAP)&m_bitmapButtonNormal,DST_COMPLEX,(HBRUSH)NULL);
break;
}
pDC->BitBlt(lpItemRect->left, lpItemRect->top, lpItemRect->Width(),
lpItemRect->Height(), &dcCompatible, 0, 0, SRCCOPY); dcCompatible.SelectObject(pOldBitmap);
}
//////////.h文件
#if !defined(AFX_FACEBUTTON_H__56B0E787_C68E_413F_AA51_E9E3F0C8FD27__INCLUDED_)
#define AFX_FACEBUTTON_H__56B0E787_C68E_413F_AA51_E9E3F0C8FD27__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// FaceButton.h : header file
///////////////////////////////////////////////////////////////////////////////
// CFaceButton windowclass CFaceButton : public CButton
{
// Construction
public:
CFaceButton();// Attributes
public:
enum FACESTATE{FACE_NORMAL=0x00000001,FACE_HOT=0x0000002,FACE_DOWN=0x00000004,FACE_FOCUS=0x00000008};private:
BOOL m_bTracking;
BOOL m_bHover;
CBitmap m_bitmapButtonHot;
CBitmap m_bitmapButtonNormal;
CBitmap m_bitmapButtonFocus;
CBitmap m_bitmapButtonDown;
FACESTATE m_fsButtonState;
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFaceButton)
public:
virtual BOOL Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext = NULL);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
protected:
virtual void PreSubclassWindow();
//}}AFX_VIRTUALprivate:
void DrawItemBitmap(CDC* pDC, CRect* lpItemRect);// Implementation
public:
virtual ~CFaceButton(); // Generated message map functions
protected:
//{{AFX_MSG(CFaceButton)
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg LRESULT OnMouseLeave(WPARAM wParam,LPARAM lParam);
afx_msg LRESULT OnMouseHover(WPARAM wParam,LPARAM lParam);
afx_msg LRESULT OnImitateStateMsgHandler(WPARAM wParam,LPARAM lParam);
//}}AFX_MSG DECLARE_MESSAGE_MAP()
};///////////////////////////////////////////////////////////////////////////////{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.#endif // !defined(AFX_FACEBUTTON_H__56B0E787_C68E_413F_AA51_E9E3F0C8FD27__INCLUDED_)