Combo_ImageDlg.cpp
上传用户:szcysw
上传日期:2013-03-11
资源大小:6752k
文件大小:6k
源码类别:

界面编程

开发平台:

Visual C++

  1. // Combo_ImageDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Combo_Image.h"
  5. #include "Combo_ImageDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CCombo_ImageDlg dialog
  50. CCombo_ImageDlg::CCombo_ImageDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CCombo_ImageDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CCombo_ImageDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CCombo_ImageDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CCombo_ImageDlg)
  63. // DDX_Control(pDX, IDC_COMBO1, m_combo);
  64. DDX_Control(pDX, IDC_COMBOBOXEX1, m_comboex);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CCombo_ImageDlg, CDialog)
  68. //{{AFX_MSG_MAP(CCombo_ImageDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_CBN_SELCHANGE(IDC_COMBOBOXEX1, OnSelchangeComboboxex1)
  73. //}}AFX_MSG_MAP
  74. END_MESSAGE_MAP()
  75. /////////////////////////////////////////////////////////////////////////////
  76. // CCombo_ImageDlg message handlers
  77. BOOL CCombo_ImageDlg::OnInitDialog()
  78. {
  79. CDialog::OnInitDialog();
  80. // Add "About..." menu item to system menu.
  81. // IDM_ABOUTBOX must be in the system command range.
  82. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  83. ASSERT(IDM_ABOUTBOX < 0xF000);
  84. CMenu* pSysMenu = GetSystemMenu(FALSE);
  85. if (pSysMenu != NULL)
  86. {
  87. CString strAboutMenu;
  88. strAboutMenu.LoadString(IDS_ABOUTBOX);
  89. if (!strAboutMenu.IsEmpty())
  90. {
  91. pSysMenu->AppendMenu(MF_SEPARATOR);
  92. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  93. }
  94. }
  95. // Set the icon for this dialog.  The framework does this automatically
  96. //  when the application's main window is not a dialog
  97. SetIcon(m_hIcon, TRUE); // Set big icon
  98. SetIcon(m_hIcon, FALSE); // Set small icon
  99. // TODO: Add extra initialization here
  100. //建立一图像列表控件,每幅图片的大小为32*32
  101. Combo_ImageList.Create(32,32,TRUE,4,1);
  102. //将图片依次选入图像列表控件,编号是0-3,共4幅
  103. AddBitmap(IDB_SPADE);
  104. AddBitmap(IDB_HEART);
  105. AddBitmap(IDB_CLUB);
  106. AddBitmap(IDB_DIAMOND);
  107. //让组合列表框与图像列表控件进行关联
  108. this->m_comboex.SetImageList(&Combo_ImageList);
  109. //设立四个列表框项目,并将之加入组合列表框
  110. COMBOBOXEXITEM ComboListItem[4];
  111. LPTSTR  pszText[4]={"黑桃","红心","樱花","方块"};
  112. for(int i=0;i<4;i++)
  113. {
  114. ComboListItem[i].iItem=i;
  115. ComboListItem[i].mask=CBEIF_IMAGE|CBEIF_TEXT|CBEIF_SELECTEDIMAGE;
  116. ComboListItem[i].pszText=pszText[i];
  117. ComboListItem[i].cchTextMax=4;
  118. ComboListItem[i].iImage=i;
  119. ComboListItem[i].iSelectedImage=i;
  120. ComboListItem[i].iOverlay=2;
  121. this->m_comboex.InsertItem(&ComboListItem[i]);
  122. }
  123. //设定当前选择为第一个项目
  124. this->m_comboex.SetCurSel(0);
  125. return TRUE;  // return TRUE  unless you set the focus to a control
  126. }
  127. void CCombo_ImageDlg::OnSysCommand(UINT nID, LPARAM lParam)
  128. {
  129. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  130. {
  131. CAboutDlg dlgAbout;
  132. dlgAbout.DoModal();
  133. }
  134. else
  135. {
  136. CDialog::OnSysCommand(nID, lParam);
  137. }
  138. }
  139. // If you add a minimize button to your dialog, you will need the code below
  140. //  to draw the icon.  For MFC applications using the document/view model,
  141. //  this is automatically done for you by the framework.
  142. void CCombo_ImageDlg::OnPaint() 
  143. {
  144. if (IsIconic())
  145. {
  146. CPaintDC dc(this); // device context for painting
  147. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  148. // Center icon in client rectangle
  149. int cxIcon = GetSystemMetrics(SM_CXICON);
  150. int cyIcon = GetSystemMetrics(SM_CYICON);
  151. CRect rect;
  152. GetClientRect(&rect);
  153. int x = (rect.Width() - cxIcon + 1) / 2;
  154. int y = (rect.Height() - cyIcon + 1) / 2;
  155. // Draw the icon
  156. dc.DrawIcon(x, y, m_hIcon);
  157. }
  158. else
  159. {
  160. CDialog::OnPaint();
  161. }
  162. }
  163. // The system calls this to obtain the cursor to display while the user drags
  164. //  the minimized window.
  165. HCURSOR CCombo_ImageDlg::OnQueryDragIcon()
  166. {
  167. return (HCURSOR) m_hIcon;
  168. }
  169. BOOL CCombo_ImageDlg::AddBitmap(UINT n)
  170. {
  171. BOOL breturn;
  172. CBitmap bmp;
  173. breturn=bmp.LoadBitmap(n);
  174. if (breturn!=FALSE)
  175. {
  176. int nReturn=Combo_ImageList.Add(&bmp,RGB(255,255,255));
  177. bmp.DeleteObject();
  178. }
  179. //如果在装载位图的过程中有误,返回错误标志
  180. return breturn;
  181. }
  182. void CCombo_ImageDlg::OnSelchangeComboboxex1() 
  183. {
  184. CString message;
  185. message.Format("The current choice is %d", this->m_comboex.GetCurSel());
  186. AfxMessageBox(message);
  187. // TODO: Add your control notification handler code here
  188. }