按钮中添加位图和图标

MFC中,实现往按钮中添加位图

对话框宣称一个成员变量  
  CBitmapButton  m_BmpBtn;   
m_BmpBtn.AutoLoad(***   ,   ***);   
    
 首先,我们创建一个基于对话框的应用程序CmyDialog   ;  
  Ι.MFC的CBitmapButton类,这也是最简单的功能最强的位图按钮。我们可以采取如下的步骤:  
  1.为按钮指定唯一的按钮标题(此例子为OK按钮,这里设置按钮标题为Ensure)并选中Ownerdraw属性,然后在项目中加一些位图资源,并用名字标示这些资源而不要用数字ID,其ID分别为"EnsureU"、"EnsureD"、"EnsureF"、"EnsureX"(一定要加双引号),分别对应于按钮的“松开(Up)”、“按下 (Down)”、“获得输入焦点(focused)”和“禁止(Disable)”状态。  
  2. 我们还要在对话框类中加入CBitmapButton   m_BmpBtn数据成员。  
  3. 在OnInitDialog()为这个成员函数中调用:(添加WM_INITDIALOG消息处理函数)    

  (注意不可以在构造函数中调用,否则会出错.) 
BOOL CAboutDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 
 // TODO: Add extra initialization here
    m_BmpBtn.AutoLoad(IDOK,this);
 return TRUE;  // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}
  点击编译按钮,成功后运行程序

  /*如果以上方法不行请检查你的BITMAP   资源,APPSTUDIO中,"EnsureU"和   "EnsureD"   等的资源名称都是需要用引号引起来的,   AutoLoad不成功,很可能就是由此产生的。     */  

下面是MSDN的英文资料

Use the CBitmapButton class to create pushbutton controls labeled with bitmapped images instead of text. CBitmapButton objects contain up to four bitmaps, which contain images for the different states a button can assume: up (or normal), down (or selected), focused, and disabled. Only the first bitmap is required; the others are optional.

Bitmap-button images include the border around the image as well as the image itself. The border typically plays a part in showing the state of the button. For example, the bitmap for the focused state usually is like the one for the up state but with a dashed rectangle inset from the border or a thick solid line at the border. The bitmap for the disabled state usually resembles the one for the up state but has lower contrast (like a dimmed or grayed menu selection).

These bitmaps can be of any size, but all are treated as if they were the same size as the bitmap for the up state.

Various applications demand different combinations of bitmap images:

Up Down Focused Disabled Application
× Bitmap
× × Button without WS_TABSTOP style
× × × × Dialog button with all states
× × × Dialog button with WS_TABSTOP style

When creating a bitmap-button control, set the BS_OWNERDRAW style to specify that the button is owner-drawn.  This causes Windows to send the WM_MEASUREITEM and WM_DRAWITEM messages for the button; the framework handles these messages and manages the appearance of the button for you.

To create a bitmap-button control in a window’s client area, follow these steps:

  1. Create one to four bitmap images for the button.

  2. Construct the CBitmapButton object.

  3. Call the Create function to create the Windows button control and attach it to the CBitmapButton object.

  4. Call the LoadBitmaps member function to load the bitmap resources after the bitmap button is constructed.

我们使用的方法

To include a bitmap-button control in a dialog box, follow these steps:

  1. Create one to four bitmap images for the button.

  2. Create a dialog template with an owner-draw button positioned where you want the bitmap button. The size of the button in the template does not matter.

  3. Set the button’s caption to a value such as “MYIMAGE” and define a symbol for the button such as IDC_MYIMAGE.

  4. In your application’s resource script, give each of the images created for the button an ID constructed by appending one of the letters “U,” “D,” “F,” or “X” (for up, down, focused, and disabled) to the string used for the button caption in step 3. For the button caption “MYIMAGE,” for example, the IDs would be “MYIMAGEU,” “MYIMAGED,” “MYIMAGEF,” and “MYIMAGEX.” You must specify the ID of your bitmaps within double quotes. Otherwise the resource editor will assign an integer to the resource and MFC will fail when loading the image.
     我用的CAPTION是Ensure.在Resource.h中可看出
    //{{NO_DEPENDENCIES}}
    // Microsoft Developer Studio generated include file.
    // Used by Resume.rc
    //
    #define IDD_ABOUTBOX                    100
    #define IDR_MAINFRAME                   128
    #define IDR_RESUMETYPE                  129
    #define IDB_MY_BITMAP                   130
    #define IDR_MUSIC2                      132
    #define IDR_MUSIC1                      133
    #define IDR_MUSIC3                      134
    #define IDI_ICONBTN                     136
    #define ID_OPEN_MUSIC                   32771
    #define ID_CLOSE_MUSIC                  32772
    #define ID_OPEN_MUSIC2                  32773
    #define ID_OPEN_MUSIC1                  32774
    #define ID_OPEN_MUSIC3                  32775
  5. // Next default values for new objects
    //
    #ifdef APSTUDIO_INVOKED
    #ifndef APSTUDIO_READONLY_SYMBOLS
    #define _APS_3D_CONTROLS                     1
    #define _APS_NEXT_RESOURCE_VALUE        146
    #define _APS_NEXT_COMMAND_VALUE         32776
    #define _APS_NEXT_CONTROL_VALUE         1000
    #define _APS_NEXT_SYMED_VALUE           101
    #endif
    #endif

         找不到类似与 #define EnsureU  1000 的定义.

       9. In your application’s dialog class (derived from CDialog), add a CBitmapButton member     object.

        10.In the CDialog object’s OnInitDialog routine, call the CBitmapButton object’s AutoLoad function, using as parameters the button’s control ID and the CDialog object’s this pointer.

 

If you want to handle Windows notification messages, such as BN_CLICKED, sent by a bitmap-button control to its parent (usually a class derived from CDialog), add to the CDialog-derived object a message-map entry and message-handler member function for each message. The notifications sent by a CBitmapButton object are the same as those sent by a CButton object.

The class CToolBar takes a different approach to bitmap buttons.

For more information on CBitmapButton, seeControl Topics in Visual C++ Programmer's Guide.


 使用图标制作按钮  


  1. 打开按钮的属性页,在Style中选中Icon   。  
  2. 在对话框类的头文件中定义成员变量(

注意一定要使用ClassWizard加入这个成员变量)

   
  CButton        m_IconBtn;;//对应于图标按钮  
  3. 创建相应的图标或者位图资源:  
  图标资源:IDI_ICONBTN
  4.在初始化中加入如下代码:    
 BOOL CAboutDlg::OnInitDialog()
{
 CDialog::OnInitDialog();
 
 // TODO: Add extra initialization here
  HICON Icon=AfxGetApp()->LoadIcon(IDI_ICONBTN);  
       m_IconBtn.SetIcon(Icon);   


    m_BmpBtn.AutoLoad(IDOK,this);
 return TRUE;  // return TRUE unless you set the focus to a control
               // EXCEPTION: OCX Property Pages should return FALSE
}
  …  
  重新编译运行我们的程序.OK.
 

你可能感兴趣的:(object,dialog,button,autoload,initialization,combinations)