Fork me on GitHub

CButtonST|CUniButton等按钮类的使用

CButtonST

CButtonST类的使用参考链接:http://www.cnblogs.com/lidabo/archive/2012/12/17/2821122.html

CCeButtonST

CCeButtonST是和CButtonST类(VC6.0 )相对应的EVC(Embedded Visual C++)版本。

CUniButton

CUniButton主要用于创建不规则形状的按钮。

1、首先下载UniButton.cppUniButton.h文件

2、用VS2010创建基于对话框的MFC应用程序

3、将UniButton.cpp和UniButton.h文件添加入本工程,其后会多出一个CUniButton类

4、通过类向导为本对话框添加CUniButton类的成员对象以及定义按钮ID(添加到C***Dlg.h中),代码如下所示:

public:
    CUniButton m_Btn1;
    CUniButton m_Btn2;
    CUniButton m_Btn3;
    CUniButton m_Btn4;
    CUniButton m_Btn5;
    enum {UNI_BTN1 = 1001, UNI_BTN2 = 1002, UNI_BTN3 = 1003, UNI_BTN4 = 1004, UNI_BTN5 = 1005};
    CUniButton m_BtnLeft;
    CUniButton m_BtnRight;
    CUniButton m_BtnUp;
    CUniButton m_BtnDown;
    CUniButton m_BtnMid;
    enum {UNI_BTNL = 1006, UNI_BTNR = 1007, UNI_BTNU = 1008, UNI_BTND = 1009, UNI_BTNM = 1010};

5、在本对话框源文件(C***Dlg.cpp)的OnInitDialog()函数中添加以下代码:

//btn1
    HRGN r1;
    HDC hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    r1 = CreateEllipticRgn(0, 0, 63, 31);
    m_Btn1.Create(_T("Btn 1"), WS_CHILD | WS_VISIBLE, CPoint(100, 15), r1, this, UNI_BTN1, 2, GetSysColor(COLOR_BTNFACE), RGB(0, 255, 0), RGB(0, 255, 0), GetSysColor(COLOR_BTNFACE)); 
    DeleteObject(r1);

    //btn2
    HRGN r2;
    LOGFONT lf;
    GetFont() -> GetLogFont(&lf);
    lf.lfHeight = -128;
    lf.lfWeight = 1000;
    HFONT hFont = CreateFontIndirect(&lf);
    HFONT hOldFont = (HFONT) SelectObject(hDC, hFont);
    r2 = CreateRectRgn(0, 0, 0, 0);

    int mode = SetBkMode(hDC, TRANSPARENT);
    BeginPath(hDC);
    TextOut(hDC, 0, 0, _T(""), 1);
    EndPath(hDC);
    r2 = PathToRegion(hDC);
    SetBkMode(hDC, mode);

    m_Btn2.Create(_T(""), WS_CHILD | WS_VISIBLE, CPoint(15, 30), r2, this, UNI_BTN2, RGB(255, 255, 0));

    SelectObject(hDC, hOldFont);
    DeleteObject(hFont);
    DeleteObject(r2);

    //btn3
    HRGN r3;
    BeginPath(hDC);
    MoveToEx(hDC, 0, 32, NULL);
    LineTo(hDC, 48, 32);
    LineTo(hDC, 48, 16);
    LineTo(hDC, 96, 48);
    LineTo(hDC, 48, 80);
    LineTo(hDC, 48, 64);
    LineTo(hDC, 0, 64);
    LineTo(hDC, 0, 32);
    EndPath(hDC);
    //r3 = CreateRectRgn(0, 0, 63, 63);
    r3 = PathToRegion(hDC);
    m_Btn3.Create(_T("Btn 3"), WS_CHILD | WS_VISIBLE, CPoint(150, 50), r3, this, UNI_BTN3, 2, RGB(250, 207, 194), RGB(255, 0, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(r3);
    DeleteObject(hDC);

    //btn4
    HRGN r4;
    hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    BeginPath(hDC);
    MoveToEx(hDC, 31, 15, NULL);
    CPoint p[7];
    p[ 0] = CPoint(5, 0);
    p[ 1] = CPoint(0, 55);
    p[ 2] = CPoint(0, 28);
    p[ 3] = CPoint(31, 64);
    p[ 4] = CPoint(59, 55);
    p[ 5] = CPoint(59, 0);
    p[ 6] = CPoint(31, 15);
    PolyBezier(hDC, &p[0], 7);
    EndPath(hDC);
    r4 = CreateRectRgn(0, 0, 63, 63);
    r4 = PathToRegion(hDC);
    m_Btn4.Create(_T("Btn 4"), WS_CHILD | WS_VISIBLE, CPoint(200, 10), r4, this, UNI_BTN4, 2, RGB(151, 244, 219), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(r4);
    DeleteObject(hDC);

    //btn5
    HRGN r5,r51,r52;
    r5 = CreateRectRgn(16, 0, 80, 31);
    r51 = CreateEllipticRgn(0, 0, 32, 32);
    r52 = CreateEllipticRgn(64, 0, 96, 32);
    CombineRgn(r5, r5, r51, RGN_OR);
    CombineRgn(r5, r5, r52, RGN_OR);
    m_Btn5.Create(_T("Btn 5"), WS_CHILD | WS_VISIBLE, CPoint(180, 80), r5, this, UNI_BTN5, 2, GetSysColor(COLOR_BTNFACE), RGB(211, 247, 254), RGB(211, 247, 254), GetSysColor(COLOR_BTNFACE)); 
    DeleteObject(r5);
    DeleteObject(r51);
    DeleteObject(r52);
////////////////////////////////////////////////////////////////////////////
    //btn左
    HRGN rgnLeft;
    hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    BeginPath(hDC);

    MoveToEx(hDC,200-80/2*sqrt(2.0),200-80/2*sqrt(2.0),NULL);
    LineTo(hDC,200-40/2*sqrt(2.0),200-40/2*sqrt(2.0));

    AngleArc(hDC,200,200,40,135,90);

    LineTo(hDC,200-80/2*sqrt(2.0),200+80/2*sqrt(2.0));

    AngleArc(hDC,200,200,80,225,-90);

    EndPath(hDC);

    rgnLeft = PathToRegion(hDC);
    m_BtnLeft.Create(_T("L"), WS_CHILD | WS_VISIBLE, CPoint(200, 150), rgnLeft, this, UNI_BTNL, 2, RGB(250, 207, 194), RGB(0, 255, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(rgnLeft);
    DeleteObject(hDC);

    //btn右
    HRGN rgnRight;
    hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    BeginPath(hDC);

    MoveToEx(hDC,200+80/2*sqrt(2.0),200-80/2*sqrt(2.0),NULL);
    LineTo(hDC,200+40/2*sqrt(2.0),200-40/2*sqrt(2.0));

    AngleArc(hDC,200,200,40,45,-90);

    LineTo(hDC,200+80/2*sqrt(2.0),200+80/2*sqrt(2.0));

    AngleArc(hDC,200,200,80,-45,90);

    EndPath(hDC);

    rgnRight = PathToRegion(hDC);
    m_BtnRight.Create(_T("R"), WS_CHILD | WS_VISIBLE, CPoint(318, 146), rgnRight, this, UNI_BTNR, 2, RGB(250, 207, 194), RGB(0, 255, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(rgnRight);
    DeleteObject(hDC);

    //btn上
    HRGN rgnUp;
    hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    BeginPath(hDC);

    MoveToEx(hDC,200+80/2*sqrt(2.0),200-80/2*sqrt(2.0),NULL);
    LineTo(hDC,200+40/2*sqrt(2.0),200-40/2*sqrt(2.0));

    AngleArc(hDC,200,200,40,45,90);

    LineTo(hDC,200-80/2*sqrt(2.0),200-80/2*sqrt(2.0));

    AngleArc(hDC,200,200,80,135,-90);

    EndPath(hDC);

    rgnUp = PathToRegion(hDC);
    m_BtnUp.Create(_T("U"), WS_CHILD | WS_VISIBLE, CPoint(230, 120), rgnUp, this, UNI_BTNU, 2, RGB(250, 207, 194), RGB(0, 255, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(rgnUp);
    DeleteObject(hDC);

    //btn下
    HRGN rgnDown;
    hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    BeginPath(hDC);

    MoveToEx(hDC,200+80/2*sqrt(2.0),200+80/2*sqrt(2.0),NULL);
    LineTo(hDC,200+40/2*sqrt(2.0),200+40/2*sqrt(2.0));

    AngleArc(hDC,200,200,40,-45,-90);

    LineTo(hDC,200-80/2*sqrt(2.0),200+80/2*sqrt(2.0));

    AngleArc(hDC,200,200,80,225,90);

    EndPath(hDC);

    rgnDown = PathToRegion(hDC);
    m_BtnDown.Create(_T("D"), WS_CHILD | WS_VISIBLE, CPoint(227, 238), rgnDown, this, UNI_BTND, 2, RGB(250, 207, 194), RGB(0, 255, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(rgnDown);
    DeleteObject(hDC);

    //btn中
    HRGN rgnMid;
    hDC = CreateCompatibleDC(GetDC() -> GetSafeHdc());
    BeginPath(hDC);
    AngleArc(hDC,200,200,40,0,360);
    //Ellipse(hDC,170,170,230,230);
    EndPath(hDC);
    rgnMid = PathToRegion(hDC);
    m_BtnMid.Create(_T("M"), WS_CHILD | WS_VISIBLE, CPoint(245, 165), rgnMid, this, UNI_BTNM, 2, RGB(250, 207, 194), RGB(0, 255, 0), RGB(255, 0, 0), GetSysColor(COLOR_BTNFACE)); 

    DeleteObject(rgnMid);
    DeleteObject(hDC);

6、运行效果如下所示:

注:

若为动态生成的以上按钮添加消息响应请参考本人的另一篇博客:

MFC动态按钮的创建及其消息响应(自定义消息)

7、上面的Button1是我静态添加的按钮,测试之用;绿色矩形是我为在对话框测试画矩形之用,代码在OnPaint()函数中:

void CbuttonDlg::OnPaint()
{
    if (IsIconic())
    {
        CPaintDC dc(this); // 用于绘制的设备上下文

        SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

        // 使图标在工作区矩形中居中
        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;

        // 绘制图标
        dc.DrawIcon(x, y, m_hIcon);
    }
    else
    {
        //CDialogEx::OnPaint();

        //画矩形
        CPaintDC dc(this); // 用于绘制的设备上下文

        CPen pen(PS_SOLID,1,RGB(255,0,0));
        //CBrush* pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
        //dc.SelectObject(pBrush);
        CBrush brush(RGB(0,255,0));
        dc.SelectObject(brush);
        //CClientDC dc(this);
        dc.SelectObject(&pen);
        dc.Rectangle(500,200,550,300);
    }
}

CRoundButton

CRoundButton主要用于创建圆形按钮,如果需要请百度,本文不做具体介绍。

CSXButton

CSXButton也是不错的派生于CButton的按钮扩展类,本人未曾使用,博友可尝试使用一下

CButtonSSL

posted @ 2014-11-01 10:15  晨光iABC  阅读(1925)  评论(0编辑  收藏  举报