- MyButton::MyButton()
- {
- m_nCurrentCount = 0;
- // 初始化
- for(int n = 0; n < 10; n ++)
- {
- hDownBitmap[n] = NULL;
- hUpBitmap[n] = NULL;
- }
- }
- void MyButton::AddButton(CString sDownImage, CString sUpImage)
- {
- hDownBitmap[m_nCurrentCount] = SHLoadImageFile(sDownImage);
- // 这里应该加个断言防止加载失败
- ASSERT(NULL != hDownBitmap[m_nCurrentCount]);
- hUpBitmap[m_nCurrentCount] = SHLoadImageFile(sUpImage);
- // 这里应该加个断言防止加载失败
- ASSERT(NULL != hUpBitmap[m_nCurrentCount]);
- m_nCurrentCount ++;
- }
- void MyButton::SetButtonRect(CRect rc)
- {
- rc[m_nCurrentCount] = rc;
- }
-
- void MyButton::InitButtonImage(HWND hWnd)
- {
- HDC hdc = GetDC(hWnd);
- // 利用双缓冲绘图
- for(int n = 0; n < m_nCurrentCount; n ++)
- {
- HDC hMemDc = CreateCompatibleDC(hdc);
- HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDc, hDownBitmap[m_nCurrentCount]);
- BITMAP bm;
- GetObject(hDownBitmap[m_nCurrentCount], sizeof(bm), &bm);
- StretchBlt(hdc, rc[m_nCurrentCount].left, rc[m_nCurrentCount].top, rc[m_nCurrentCount].right - rc[m_nCurrentCount].left, rc[m_nCurrentCount].bottom - rc[m_nCurrentCount].top, &dcSrc, 0, 0, nWidthOld, nHeightOld, SRCCOPY);
- BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hMemDc, 0, 0, SRCCOPY);
- SelectObject(hMemDc, hOldBmp);
- DeleteDC(hMemDc);
- }
- }
-
- void MyButton::InitButtonImage(HWND hWnd)
- {
- HDC hdc = GetDC(hWnd);
- // 利用双缓冲绘图
- for(int n = 0; n < m_nCurrentCount; n ++)
- {
- HDC hMemDc = CreateCompatibleDC(hdc);
- HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDc, hDownBitmap[m_nCurrentCount]);
- BITMAP bm;
- GetObject(hDownBitmap[m_nCurrentCount], sizeof(bm), &bm);
- StretchBlt(hdc, rc[m_nCurrentCount].left, rc[m_nCurrentCount].top, rc[m_nCurrentCount].right - rc[m_nCurrentCount].left, rc[m_nCurrentCount].bottom - rc[m_nCurrentCount].top, &dcSrc, 0, 0, nWidthOld, nHeightOld, SRCCOPY);
- SelectObject(hMemDc, hOldBmp);
- DeleteDC(hMemDc);
- }
- }
- void MyButton::SetUpButtonImage(HWND hWnd, int nSel)
- {
- HDC hdc = GetDC(hWnd);
- CRect rcSel;
- rcSel = rc[nSel];
-
- m_nPreSel = nSel;
- // 利用双缓冲绘图
- HDC hMemDc = CreateCompatibleDC(hdc);
- HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDc,hUpBitmap[nSel]);
- BITMAP bm;
- GetObject(hDownBitmap[m_nCurrentCount], sizeof(bm), &bm);
- StretchBlt(hdc, rcSel.left, rcSel.top, rcSel.right - rcSel.left, rcSel.bottom - rcSel.top, &dcSrc, 0, 0, nWidthOld, nHeightOld, SRCCOPY);
- SelectObject(hMemDc, hOldBmp);
- DeleteDC(hMemDc);
- }
-
- void MyButton::SetUpButtonImage(HWND hWnd, int nPreSel)
- {
- HDC hdc = GetDC(hWnd);
- CRect rcSel;
- rcSel = rc[nSel];
-
- // 利用双缓冲绘图
- HDC hMemDc = CreateCompatibleDC(hdc);
- HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDc,hUpBitmap[nSel]);
- BITMAP bm;
- GetObject(hDownBitmap[m_nCurrentCount], sizeof(bm), &bm);
- StretchBlt(hdc, rcSel.left, rcSel.top, rcSel.right - rcSel.left, rcSel.bottom - rcSel.top, &dcSrc, 0, 0, nWidthOld, nHeightOld, SRCCOPY);
- SelectObject(hMemDc, hOldBmp);
- DeleteDC(hMemDc);
- }
-
- int MyButton::OnClick(IN HWND hwnd, IN CPoint pt) // 捕捉点击消息
- {
- int nClicked = -1; // 选中的图标
- for ( int n = 0; n < m_nCountTool; n ++)
- {
- if ( m_Rect[n].PtInRect(pt))
- {
- nClicked = n;
- break;
- }
- }
- if ( -1 == nClicked )
- {
- return -1;
- }
- else
- {
- m_nCurrentSel = nClicked;
- SetDownImage(hwnd, m_nCurrentSel); // 设置当前选中图标
- SetUpImage(hwnd, m_nCurrentSel); // 恢复上一级选中图标的状态
- InvalidateRect(hwnd, rc[m_nCurrentSel], TRUE); // 刷新选中范围
- InvalidateRect(hwnd, rc[m_nPreSel], TRUE); // 刷新上一级选中范围
- }
- }
|