VC驿站

 找回密码
 加入驿站

QQ登录

只需一步,快速开始

搜索
查看: 614|回复: 3

[求助] 这个我不会弄,求高手帮忙

[复制链接]
85_avatar_middle
在线会员 发表于 2015-12-9 14:06:14 | 显示全部楼层 |阅读模式
这是Visual C++图形用户界面开发指南,这本书中光盘的源程序:
// AdvButton.cpp : implementation file
//
#include "stdafx.h"
#include "AdvButton.h"
#include "AdvBtn.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAdvButton

CAdvButton::CAdvButton()
{
        m_nColor = GetSysColor(COLOR_BTNFACE);
        m_sColor = m_nColor;
        m_hColor = m_nColor;
        m_dColor = m_nColor;
        m_nBorder = 1;
        m_lfEscapement = 0;
        m_pNormal = NULL;
        m_pSelected = NULL;
        m_pHover = NULL;
        m_pDisabled = NULL;
        m_hRgn = 0;
        m_bHover = false;
        m_bCapture = false;
        m_bMouseDown = false;
        m_bNeedBitmaps = true;
}

CAdvButton::~CAdvButton()
{
        delete m_pNormal;
        delete m_pSelected;
        delete m_pHover;
        delete m_pDisabled;
        DeleteObject(m_hRgn);
}

BEGIN_MESSAGE_MAP(CAdvButton, CButton)
        //{{AFX_MSG_MAP(CAdvButton)
        ON_WM_ERASEBKGND()
        ON_WM_MOUSEMOVE()
        ON_WM_CREATE()
        ON_WM_LBUTTONDOWN()
        ON_WM_LBUTTONUP()
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAdvButton message handlers

BOOL CAdvButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID)
{
        DeleteObject(m_hRgn);
        m_hRgn = CreateRectRgn(0, 0, 31, 31);
        CRect box(0, 0, 0, 0);
        if (m_hRgn != 0)
                CombineRgn(m_hRgn, hRgn, 0, RGN_COPY);

        GetRgnBox(m_hRgn, &box);
        OffsetRgn(m_hRgn, -box.left, -box.top);
        GetRgnBox(m_hRgn, &box);

        m_CenterPoint = CPoint(box.left + box.Width() /2 , box.top + box.Height() /2);
        box.OffsetRect(point);

        return CButton::Create(lpszCaption, dwStyle, box, pParentWnd, nID);
}

BOOL CAdvButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, COLORREF color)
{
        m_sColor = color;
        m_hColor = color;

        return Create(lpszCaption, dwStyle, point, hRgn, pParentWnd, nID);
}

BOOL CAdvButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, UINT nBorder, LONG lfEscapement, COLORREF nColor, COLORREF sColor, COLORREF hColor, COLORREF dColor)
{
        m_lfEscapement = lfEscapement;
        return Create(lpszCaption, dwStyle, point, hRgn, pParentWnd, nID, nBorder, nColor, sColor, hColor, dColor);
}

BOOL CAdvButton::Create(LPCTSTR lpszCaption, DWORD dwStyle, const CPoint point, const HRGN hRgn, CWnd* pParentWnd, UINT nID, UINT nBorder, COLORREF nColor, COLORREF sColor, COLORREF hColor, COLORREF dColor)
{
        m_nBorder = nBorder;
        m_nColor = nColor;
        m_sColor = sColor;
        m_hColor = hColor;
        m_dColor = dColor;

        return Create(lpszCaption, dwStyle, point, hRgn, pParentWnd, nID);
}

void CAdvButton::PreSubclassWindow()
{
        ModifyStyle(0, BS_OWNERDRAW | BS_PUSHBUTTON);       
        CButton::PreSubclassWindow();
}

int CAdvButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
        if (CButton::OnCreate(lpCreateStruct) == -1)
                return -1;

        m_bNeedBitmaps = true;
        SetWindowRgn(m_hRgn, true);

        return 0;
}

void CAdvButton::OnLButtonDown(UINT nFlags, CPoint point)
{
        m_bMouseDown = true;
        if (!m_bCapture) {
                SetCapture();
                m_bCapture = true;
        }
        CButton::OnLButtonDown(nFlags, point);
}

void CAdvButton::OnLButtonUp(UINT nFlags, CPoint point)
{
        CButton::OnLButtonUp(nFlags, point);

        m_bMouseDown = false;       
        if (m_bCapture) {
                ReleaseCapture();
                m_bCapture = false;
        }
        CheckHover(point);
}

void CAdvButton::OnMouseMove(UINT nFlags, CPoint point)
{
        if (!m_bMouseDown)
                CheckHover(point);

        CButton::OnMouseMove(nFlags, point);
}

void CAdvButton::CheckHover(CPoint point)
{
                if (HitTest(point)) {
                        if (!m_bCapture) {
                                SetCapture();
                                m_bCapture = true;
                        }
                        if (!m_bHover) {
                                m_bHover = true;
                                RedrawWindow();
                        }
                }
                else {
                        if (m_bCapture) {
                                ReleaseCapture();
                                m_bCapture = false;
                        }
                        m_bHover = false;
                        RedrawWindow();
                }
}

LRESULT CAdvButton::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
        if (message == WM_LBUTTONDBLCLK)
                message = WM_LBUTTONDOWN;
       
        return CButton::DefWindowProc(message, wParam, lParam);
}

BOOL CAdvButton::HitTest(CPoint point)
{
        BOOL result = false;

        HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
        GetWindowRgn(hRgn);
        CRect rgnRect;
        GetRgnBox(hRgn, &rgnRect);

        result = PtInRect(&rgnRect, point) && PtInRegion(hRgn, point.x, point.y);

        DeleteObject(hRgn);
        return result;
}

BOOL CAdvButton::OnEraseBkgnd(CDC* pDC)
{
        return 1;
}


void CAdvButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
        CDC* pDC = CDC::FromHandle(lpDrawItemStruct -> hDC);
        CRect rect;
        GetClientRect(rect);

        if (m_bNeedBitmaps)
                PrepareStateBitmaps(pDC, &rect);

        DrawButton(pDC, &rect, lpDrawItemStruct -> itemState);
}

void CAdvButton::PrepareStateBitmaps(CDC * pDC, CRect * pRect)
{
        CDC * pMemDC;
        pMemDC = new CDC;
        pMemDC -> CreateCompatibleDC(pDC);

        PrepareNormalState(pDC, pMemDC, pRect);
        PrepareSelectedState(pDC, pMemDC, pRect);
        PrepareHoverState(pDC, pMemDC, pRect);
        PrepareDisabledState(pDC, pMemDC, pRect);


        delete pMemDC;
        m_bNeedBitmaps = false;
}


void CAdvButton::PrepareNormalState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
        delete m_pNormal;
        m_pNormal = new CBitmap;
        PaintRgn(pDC, pMemDC, m_pNormal, m_nColor, pRect, true, false);
}

void CAdvButton::PrepareSelectedState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
        delete m_pSelected;
        m_pSelected = new CBitmap;
        PaintRgn(pDC, pMemDC, m_pSelected, m_sColor, pRect, true, true);
}

void CAdvButton::PrepareHoverState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
        delete m_pHover;
        m_pHover = new CBitmap;
        PaintRgn(pDC, pMemDC, m_pHover, m_hColor, pRect, true, false);
}

void CAdvButton::PrepareDisabledState(CDC * pDC, CDC * pMemDC, CRect * pRect)
{
        delete m_pDisabled;
        m_pDisabled = new CBitmap;
        PaintRgn(pDC, pMemDC, m_pDisabled, m_dColor, pRect, false, false);
}

void CAdvButton::PaintRgn(CDC * pDC, CDC * pMemDC, CBitmap * pBitmap, COLORREF color, CRect * pRect, BOOL bEnabled, BOOL bSunken)
{
        pBitmap -> CreateCompatibleBitmap(pDC, pRect -> Width(), pRect -> Height());
        CBitmap * pOldBitmap = pMemDC -> SelectObject(pBitmap);

        HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
        GetWindowRgn(hRgn);

        HBRUSH hBrush = CreateSolidBrush(color);
        pMemDC -> FillSolidRect(pRect, RGB(0, 0, 0));
        FillRgn(pMemDC -> GetSafeHdc(), hRgn, hBrush);
        DeleteObject(hBrush);

        DrawButtonCaption(pMemDC -> GetSafeHdc(), pRect, bEnabled, bSunken);
        FrameRgn3D(pMemDC -> GetSafeHdc(), hRgn, bSunken);

        DeleteObject(hRgn);
        pMemDC -> SelectObject(pOldBitmap);
}

void CAdvButton::DrawButtonCaption(HDC hDC, CRect * pRect, BOOL bEnabled, BOOL bSunken)
{
        int nOldMode = SetBkMode(hDC, TRANSPARENT);
        CString text;
        GetWindowText(text);
        LOGFONT lf;
        GetParent() -> GetFont() -> GetLogFont(&lf);
        HFONT hFont = CreateFontIndirect(&lf);
        HFONT hOldFont = (HFONT) SelectObject(hDC, hFont);

        TEXTMETRIC tm;
        GetTextMetrics(hDC, &tm);
        CPoint p = CPoint(m_CenterPoint.x, m_CenterPoint.y + tm.tmHeight/ 2);
        if (bSunken)
                p.Offset(m_nBorder, m_nBorder);
               
        if (bEnabled) {
                SetTextColor(hDC, GetSysColor(COLOR_BTNTEXT));
                SetTextAlign(hDC, TA_CENTER | TA_BOTTOM);
                TextOut(hDC, p.x, p.y, text, text.GetLength());
        }
        else {
                SetTextColor(hDC, GetSysColor(COLOR_3DHILIGHT));
                TextOut(hDC, p.x + 1, p.y + 1, text, text.GetLength());
                SetTextColor(hDC, GetSysColor(COLOR_3DSHADOW));
                TextOut(hDC, p.x, p.y, text, text.GetLength());
        }

        SelectObject(hDC, hOldFont);
        DeleteObject(hFont);
        SetBkMode(hDC, nOldMode);
}

void CAdvButton::FrameRgn3D(HDC hDC, const HRGN hRgn, BOOL bSunken)
{
        HBRUSH hBrush;
        HRGN hBaseRgn = CreateRectRgn(0, 0, 0, 0);

        COLORREF ltOuter, ltInner, rbOuter, rbInner;

        if (!bSunken) {
                ltOuter = GetSysColor(COLOR_3DLIGHT);
                ltInner = GetSysColor(COLOR_3DHILIGHT);
                rbOuter = GetSysColor(COLOR_3DDKSHADOW);
                rbInner = GetSysColor(COLOR_3DSHADOW);
        }
        else {
                rbInner = GetSysColor(COLOR_3DLIGHT);
                rbOuter = GetSysColor(COLOR_3DHILIGHT);
                ltInner = GetSysColor(COLOR_3DDKSHADOW);
                ltOuter = GetSysColor(COLOR_3DSHADOW);
        }

        switch (m_nBorder)
        {
        case 2:
                CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
                OffsetRgn(hBaseRgn, 2, 2);
                CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
                hBrush = CreateSolidBrush(ltInner);
                FillRgn(hDC, hBaseRgn, hBrush);
                DeleteObject(hBrush);

                CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
                OffsetRgn(hBaseRgn, -2, -2);
                CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
                hBrush = CreateSolidBrush(rbInner);
                FillRgn(hDC, hBaseRgn, hBrush);
                DeleteObject(hBrush);

                CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
                OffsetRgn(hBaseRgn, 1, 1);
                CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
                hBrush = CreateSolidBrush(ltOuter);
                FillRgn(hDC, hBaseRgn, hBrush);
                DeleteObject(hBrush);

                CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
                OffsetRgn(hBaseRgn, -1, -1);
                CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
                hBrush = CreateSolidBrush(rbOuter);
                FillRgn(hDC, hBaseRgn, hBrush);
                DeleteObject(hBrush);
                break;
        default:
                CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
                OffsetRgn(hBaseRgn, 1, 1);
                CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
                hBrush = CreateSolidBrush(ltInner);
                FillRgn(hDC, hBaseRgn, hBrush);
                DeleteObject(hBrush);

                CombineRgn(hBaseRgn, hRgn, 0, RGN_COPY);
                OffsetRgn(hBaseRgn, -1, -1);
                CombineRgn(hBaseRgn, hRgn, hBaseRgn, RGN_DIFF);
                hBrush = CreateSolidBrush(rbOuter);
                FillRgn(hDC, hBaseRgn, hBrush);
                DeleteObject(hBrush);
                break;
        }
       
        DeleteObject(hBaseRgn);
}

void CAdvButton::DrawButton(CDC * pDC, CRect * pRect, UINT state)
{
        CDC * pMemDC = new CDC;
        pMemDC -> CreateCompatibleDC(pDC);
        CBitmap * pOldBitmap;

        HRGN hRgn = CreateRectRgn(0, 0, 0, 0);
        GetWindowRgn(hRgn);

        if (state & ODS_DISABLED)
                pOldBitmap = pMemDC -> SelectObject(m_pDisabled);
        else {
                if (state & ODS_SELECTED)
                        pOldBitmap = pMemDC -> SelectObject(m_pSelected);
                else {
                        if (m_bHover)
                                pOldBitmap = pMemDC -> SelectObject(m_pHover);
                        else
                                pOldBitmap = pMemDC -> SelectObject(m_pNormal);
                }       
        }

        ::SelectClipRgn(pDC -> GetSafeHdc(), hRgn);
        pDC -> BitBlt(0, 0, pRect -> Width(), pRect -> Height(), pMemDC, 0, 0, SRCCOPY);
        ::SelectClipRgn(pDC -> GetSafeHdc(), NULL);

        DeleteObject(hRgn);
        pMemDC -> SelectObject(pOldBitmap);
        delete pMemDC;
}

void CAdvButton::RgnPixelWork(CDC * pDC, CRgn * pRgn)
{
        int size = pRgn -> GetRegionData(NULL, 0);
        HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size);
        RGNDATA * pData = (RGNDATA *)GlobalLock(hData);

        int res = pRgn -> GetRegionData(pData, size);
        RECT * pRect = (RECT *) pData -> Buffer;

        for (DWORD i = 0; i < pData -> rdh.nCount; i++) {
                RECT rect = *(pRect + i);
                for (int x = rect.left; x < rect.right; x++)
                        for (int y = rect.top; y < rect.bottom; y++) {
                        }
        }

        GlobalUnlock(hData);
        GlobalFree(hData);
}
这是案例中的源程序,运行时出现错误,怎么解决???
Linking...
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
libcd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/AdvBtn.exe : fatal error LNK1120: 3 unresolved externals
执行 link.exe 时出错.

AdvBtn.exe - 1 error(s), 0 warning(s)
我是新手,希望大家多多指导




上一篇:怎么改名字 我想换呢个论坛名字
下一篇:检测文件夹变动
51_avatar_middle
online_admins 发表于 2015-12-9 14:23:54 | 显示全部楼层
你建立的工程类型不对
他需要的是一个MFC类型的工程,而你建立的是一个控制台类型的,所以肯定会报链接错误了。。。
80_avatar_middle
在线会员 发表于 2015-12-9 15:58:18 | 显示全部楼层
这个我不会弄,求高手帮忙看着头昏眼花
01_avatar_middle
在线会员 发表于 2015-12-9 17:42:14 | 显示全部楼层
Syc 发表于 2015-12-9 14:23
你建立的工程类型不对
他需要的是一个MFC类型的工程,而你简历的是一个控制台类型的,所以肯定会报链接错 ...

libcd.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
还是老大眼尖
您需要登录后才可以回帖 登录 | 加入驿站 qq_login

本版积分规则

关闭

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

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

GMT+8, 2019-5-23 22:16

Powered by Discuz! X3.4

© 2009-2019 cctry.com

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