VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 778|回复: 2

[原创] 贡献一份显示图片的代码

[复制链接]
92_avatar_middle
online_vip 发表于 2016-3-21 10:45:01 | 显示全部楼层 |阅读模式
先说明使用环境:VS2013+Win7.前段时间,为了在对话框中显示图片,费了一番工夫,特留心得如下,请各位拍砖。
vs2013已经自带GDI+库,使用前的基本配置在这里不提,网上N多。这里我要实现的是能从外部自由调用图片文件,显示在对话框的指定位置,调用起来还是比较文件的。自写一个类,继承自CWnd,这样,您在使用的时候还可以把这个类创建的窗口当做按钮来用。
头文件:
#pragma once
#include "afxwin.h"
class CMyBtuuon :        public CWnd
{
public:
        CMyBtuuon();
        DECLARE_DYNCREATE(CMyBtuuon);
        ~CMyBtuuon();
        virtual BOOL Create(CWnd* pParentWnd, UINT nID,UINT x, UINT y, CString img1Path,CString img2Path, CCreateContext* pCreateContent=NULL);
protected:
        CString img1Path,img2Path;
        BOOL isMouseHover;
        BOOL ImageFromIDResource(UINT nID, LPCTSTR sTR, Image * &pImg);
        DECLARE_MESSAGE_MAP();
public:
        afx_msg void OnPaint();
        afx_msg void OnMouseHover(UINT nFlags, CPoint point);
        afx_msg void OnMouseMove(UINT nFlags, CPoint point);
        afx_msg void OnMouseLeave();
        afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
源文件:
#include "stdafx.h"
#include "MyBtuuon.h"

CMyBtuuon::CMyBtuuon(){ isMouseHover = FALSE; }
CMyBtuuon::~CMyBtuuon(){}
IMPLEMENT_DYNCREATE(CMyBtuuon, CWnd)
BEGIN_MESSAGE_MAP(CMyBtuuon, CWnd)
        ON_WM_PAINT()
        ON_WM_MOUSEHOVER()
        ON_WM_MOUSEMOVE()
        ON_WM_MOUSELEAVE()
        ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
BOOL CMyBtuuon::Create(CWnd* pParentWnd,UINT nID, UINT x, UINT y, CString img1Path,CString img2Path, CCreateContext* pCreateContent)
{
        LPCTSTR lpszClassName = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, AfxGetApp()->LoadStandardCursor(IDC_ARROW),
                (HBRUSH)GetStockObject(TRANSPARENT), NULL);

        this->img1Path = img1Path;
        this->img2Path = img2Path;
        Image img(img1Path);
        BOOL OK = CWnd::Create(NULL, NULL, WS_CHILDWINDOW | WS_VISIBLE,
                CRect(x, y, x + img.GetWidth(), img.GetHeight() + y), pParentWnd, nID, pCreateContent);;
        ModifyStyleEx(0, WS_EX_TRANSPARENT);
        //delete bg;
        return TRUE;
}

void CMyBtuuon::OnPaint()
{
        CPaintDC dc(this); // device context for painting
        Graphics g(dc.m_hDC);
        if (isMouseHover)
        {
                Image bg(img2Path);
                g.DrawImage(&bg, 0, 0, bg.GetWidth(), bg.GetHeight());
        }
        else
        {
                Image bg(img1Path);
                g.DrawImage(&bg, 0, 0, bg.GetWidth(), bg.GetHeight());
        }
        g.ReleaseHDC(dc.m_hDC);
}


void CMyBtuuon::OnMouseHover(UINT nFlags, CPoint point)
{
        // TODO:  在此添加消息处理程序代码和/或调用默认值
        CRect rect;
        GetWindowRect(&rect);
        GetParent()->ScreenToClient(&rect);
        GetParent()->InvalidateRect(&rect);
        //        CWnd::OnMouseHover(nFlags, point);
}
void CMyBtuuon::OnMouseLeave()
{
        // TODO:  在此添加消息处理程序代码和/或调用默认值
        isMouseHover = FALSE;
        CRect rect;
        GetWindowRect(&rect);
        GetParent()->ScreenToClient(&rect);
        GetParent()->InvalidateRect(&rect);
        CWnd::OnMouseLeave();
}


void CMyBtuuon::OnMouseMove(UINT nFlags, CPoint point)
{
        // TODO:  在此添加消息处理程序代码和/或调用默认值
        TRACKMOUSEEVENT   tme;
        tme.cbSize = sizeof(TRACKMOUSEEVENT);
        tme.dwFlags = TME_LEAVE | TME_HOVER; //
        tme.hwndTrack = GetSafeHwnd();
        tme.dwHoverTime = 80;
        _TrackMouseEvent(&tme);
        isMouseHover = TRUE;

        CWnd::OnMouseMove(nFlags, point);
}
BOOL CMyBtuuon::ImageFromIDResource(UINT nID, LPCTSTR sTR, Image * &pImg)
{
        HINSTANCE hInst = AfxGetResourceHandle();
        HRSRC hRsrc = ::FindResource(hInst, MAKEINTRESOURCE(nID), sTR); // type
        if (!hRsrc)
                return FALSE;
        // load resource into memory
        DWORD len = SizeofResource(hInst, hRsrc);
        BYTE* lpRsrc = (BYTE*)LoadResource(hInst, hRsrc);
        if (!lpRsrc)
                return FALSE;
        // Allocate global memory on which to create stream
        HGLOBAL m_hMem = GlobalAlloc(GMEM_FIXED, len);
        BYTE* pmem = (BYTE*)GlobalLock(m_hMem);
        memcpy(pmem, lpRsrc, len);
        IStream* pstm;
        CreateStreamOnHGlobal(m_hMem, FALSE, &pstm);
        // load from stream
        pImg = Gdiplus::Image::FromStream(pstm);
        // free/release stuff
        GlobalUnlock(m_hMem);
        pstm->Release();
        FreeResource(lpRsrc);
        GlobalFree(m_hMem);
        return TRUE;
}



void CMyBtuuon::OnLButtonUp(UINT nFlags, CPoint point)
{
        // TODO:  在此添加消息处理程序代码和/或调用默认值
//        GetParent()->SendMessage(WM_COMMAND, GetDlgCtrlID() | 0, (LONG)GetSafeHwnd());
        GetParent()->SendMessage(GetDlgCtrlID(),0,0);
        CWnd::OnLButtonUp(nFlags, point);
}
调用方法:
1.在对话框头文件中添加两个变量:m_btnTool1,m_btnTool2.
2.在OnInitDialog中,以下两句即可实现。(IDB_TOOLBTN1,IDB_TOOLBTN2为自定义的ID号)
        m_btnTool1.Create(this, IDB_TOOLBTN1, rect.left + 2, rect.top + 35, L"images\\t1bg.png",L"images\\t1hover.png");
        m_btnTool2.Create(this, IDB_TOOLBTN2, rect.left + 58, rect.top + 35, L"images\\t2bg.png", L"images\\t2hover.png");




上一篇:win32,win64编程永恒;语言编程需要注意的64位和32机器的区别
下一篇:聊天软件源码
49_avatar_middle
online_vip 发表于 2016-3-21 11:38:18 | 显示全部楼层
你要是实现图像的扩大和缩小,随鼠标能移动,就像百度地图那样就还行
44_avatar_middle
在线会员 发表于 2016-3-22 14:33:17 | 显示全部楼层
贡献一份显示图片的代码
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

关闭

站长提醒上一条 /2 下一条

QQ|小黑屋|手机版|VC驿站 ( 辽ICP备09019393号tongdun|网站地图wx_jqr

GMT+8, 2019-3-23 22:09

Powered by Discuz! X3.4

© 2009-2019 cctry.com

快速回复 返回顶部 返回列表