zoukankan      html  css  js  c++  java
  • MFC动态创建按钮,并在按钮上实现位图的切换显示

    动态创建按钮,并在按钮中添加位图,通过单击按钮显示不同的位图,可设置为显示按钮按下和弹起两种状态。只要判断a值从而输入不同的响应代码。

    1、在头文件中添加:

        CButton *pBtn;

    2、在初始化函数中添加:

        pBtn = new CButton();

        pBtn->Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_BITMAP, CRect(0,0,100,40), this, IDC_BUTTON);//按钮显示名、属性、ID号

        pBtn->MoveWindow(CRect(400,100,500,140));//位置和按钮大小

        pBtn->SetBitmap((HBITMAP)LoadImage(AfxGetInstanceHandle(), "1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));//插入位图的,位图的位置要放在程序中

        此时运行程序就可显示按钮,并且如果有位图,位图会显示在按钮上面;

    3、在Resource.h中

        添加ID号:比如#define IDC_BUTTON 1002

    4、在头文件下方

        protected:

        //{{AFX_MSG(CMy3View)

            // NOTE - the ClassWizard will add and remove member functions here.

            //    DO NOT EDIT what you see in these blocks of generated code !

        //}}AFX_MSG

        afx_msg void OnButton();//这句函数声明

        DECLARE_MESSAGE_MAP()

    5、在

        BEGIN_MESSAGE_MAP(CMy3View, CFormView)

        //{{AFX_MSG_MAP(CMy3View)

            // NOTE - the ClassWizard will add and remove mapping macros here.

            //    DO NOT EDIT what you see in these blocks of generated code!

        //}}AFX_MSG_MAP

        // Standard printing commands

        ON_BN_CLICKED(IDC_BUTTON, OnButton)//在外面(ID号,响应函数名)

        END_MESSAGE_MAP()

    6、手动添加响应函数

        void CMyView::OnButton()

        {

            MessageBox("响应函数添加测试成功");

        }

    7、在头文件中添加一个整形的成员变量

        int a;

    8、构造函数中初始化

        a = 0;

    9、在按钮响应函数中添加

        void CMyView::OnButton()

        {

            if(a%2 == 0)

            {    

                pBtn->SetBitmap((HBITMAP)LoadImage(AfxGetInstanceHandle(), "2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));

            }

            else if(a%2 == 1)

            {

                pBtn->SetBitmap((HBITMAP)LoadImage(AfxGetInstanceHandle(), "1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION));

            }

            a++;

        }

    如果单纯的实现按钮的位图切换的话,建议使用CBitmapButton类

    http://blog.csdn.net/bigtree_mfc/article/details/40625523

  • 相关阅读:
    FreeMarker缓存处理
    freemarker入门实例与源码研究准备工作
    Freemarker常用技巧(二)
    Freemarker常用技巧(一)
    Hessian与Spring整合
    Hessian学习
    数组常见的面试题
    关于排序的实现
    Redis与Memcache的区别
    JDBC编程步骤
  • 原文地址:https://www.cnblogs.com/findumars/p/5801428.html
Copyright © 2011-2022 走看看