按钮中添加位图和图标
来源:优易学  2011-1-6 10:26:35   【优易学:中国教育考试门户网】   资料下载   IT书店

  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:
  Create one to four bitmap images for the button.
  Construct the CBitmapButton object.
  Call the Create function to create the Windows button control and attach it to the CBitmapButton object.
  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:
  Create one to four bitmap images for the button.
  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.
  Set the button’s caption to a value such as “MYIMAGE” and define a symbol for the button such as IDC_MYIMAGE.
  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
  // Next default values for new objects
  //

[1] [2] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训