下面阴影处理中// GetRValue(oldcolor) * alpha / 1;改成/255,怎么与/1没什么变化?????

mirroatl199 2013-01-29 09:03:01


/*========================================================================
说明: 在指定的矩形区域内绘制阴影.
==========================================================================*/
void DrawShadow(CDC *pDC, CRect rect)
{
COLORREF oldcolor = RGB(255, 255, 255);
BYTE newValR, newValG, newValB;
BYTE AlphaArray[] = {140, 170, 212, 240};
BYTE AlphaArray2[] = {170, 205, 220, 240, 240, 250, 255};

// 底部的阴影 -----------------------------------------
int i, j;
for (j = 0; j < 4; j++)
{
for (i = 6; i <= rect.right - 5; i++)
{
oldcolor = pDC->GetPixel(i, rect.bottom - (4 - j));
newValR = GetRValue(oldcolor) * AlphaArray[j] / 255;
newValG = GetGValue(oldcolor) * AlphaArray[j] / 255;
newValB = GetBValue(oldcolor) * AlphaArray[j] / 255;
pDC->SetPixel(i, rect.bottom - (4 - j), RGB(newValR, newValG, newValB));
}
}

// 右边的阴影 -----------------------------------------
for (i = 0; i < 4; i++)
{
for (j = 6; j <= rect.bottom - 5; j++)
{
oldcolor = pDC->GetPixel(rect.right - (4 - i), j);
newValR = GetRValue(oldcolor) * AlphaArray[i] / 255;
newValG = GetGValue(oldcolor) * AlphaArray[i] / 255;
newValB = GetBValue(oldcolor) * AlphaArray[i] / 255;
pDC->SetPixel(rect.right - (4 - i), j, RGB(newValR, newValG, newValB));
}
}

// 其他部位的阴影 --------------------------------------
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
if ((i + j) > 6) break;
BYTE alpha = AlphaArray2[i + j];
oldcolor = pDC->GetPixel(rect.right - 4 + i, rect.bottom - 4 + j);
newValR = GetRValue(oldcolor) * alpha / 1; //改成/255,怎么与/1没什么变化?????
newValG = GetGValue(oldcolor) * alpha / 1;
newValB = GetBValue(oldcolor) * alpha / 1;
pDC->SetPixel(rect.right - 4 + i, rect.bottom - 4 + j, RGB(newValR, newValG, newValB));

oldcolor = pDC->GetPixel(rect.right - 4 + i, rect.top + 5 - j);
newValR = GetRValue(oldcolor) * alpha / 1;
newValG = GetGValue(oldcolor) * alpha / 1;
newValB = GetBValue(oldcolor) * alpha / 1;
pDC->SetPixel(rect.right - 4 + i, rect.top + 5 - j, RGB(newValR, newValG, newValB));

oldcolor = pDC->GetPixel(rect.left - i + 5, rect.bottom - 4 + j);
newValR = GetRValue(oldcolor) * alpha / 1;
newValG = GetGValue(oldcolor) * alpha / 1;
newValB = GetBValue(oldcolor) * alpha / 1;
pDC->SetPixel(rect.left - i + 5, rect.bottom - 4 + j, RGB(newValR, newValG, newValB));
}
}
}
...全文
223 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
呵呵,来学习一下,还没有做过这个,呵呵。
mirroatl199 2013-01-31
  • 打赏
  • 举报
回复
真没有 要不发程序给你?
引用 1 楼 sha_jinhao 的回复:
AlphaArray[j] / 255; 是一个100% 到 0%之间的小数 你确定没变化!
fengbingchun 2013-01-30
  • 打赏
  • 举报
回复
二值图????
jimette 2013-01-29
  • 打赏
  • 举报
回复
AlphaArray[j] / 255; 是一个100% 到 0%之间的小数 你确定没变化!
转载同事的测试例子,也许大家可以看看 // MyFade.cpp : implementation file // #include "stdafx.h" #include "MyFade.h" #pragma warning(disable: 4201) #include #pragma comment(lib,"winmm.lib") #pragma warning(default: 4201) #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define WC_FADEWND _T("FadeWnd") ///////////////////////////////////////////////////////////////////////////// // CMyFade CMyFade::CMyFade(CWnd* pParent) { // register the window WNDCLASS wndcls; HINSTANCE hInst = AfxGetInstanceHandle(); if (!(::GetClassInfo(hInst, WC_FADEWND, &wndcls))) { // otherwise we need to register a new class wndcls.style = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW; wndcls.lpfnWndProc = ::DefWindowProc; wndcls.cbClsExtra = 0; wndcls.cbWndExtra = 0; wndcls.hInstance = hInst; wndcls.hIcon = NULL; // no application icon wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW); wndcls.hbrBackground = NULL; // no background wndcls.lpszMenuName = NULL; // no manu application wndcls.lpszClassName = WC_FADEWND; if (!AfxRegisterClass(&wndcls)) AfxThrowResourceException(); } m_hBitmap = NULL; m_hNewBitmap = NULL; m_pWndLock = NULL; } CMyFade::~CMyFade() { if (m_hBitmap) { DeleteObject(m_hBitmap); m_hBitmap = NULL; } if (m_hNewBitmap) { DeleteObject(m_hNewBitmap); m_hNewBitmap = NULL; } if (m_pWndLock) { m_pWndLock->SetFocus(); } } BEGIN_MESSAGE_MAP(CMyFade, CWnd) //{{AFX_MSG_MAP(CMyFade) ON_WM_PAINT() ON_WM_TIMER() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CMyFade message handlers void CMyFade::FadeScreen(CWnd *pParent) { if(pParent) { CMyFade *pFade=new CMyFade(pParent); if(pFade) { pFade->Create(pParent, CRect(0, 0, 0, 0)); } } } void CMyFade::OnDraw(CDC* pDC) { CRect rc; GetWindowRect(rc); HDC hbmDC=CreateCompatibleDC(pDC->GetSafeHdc()); HBITMAP hOldbmp=(HBITMAP)::SelectObject(hbmDC, m_hNewBitmap); // 将屏幕位图再输入到屏幕上去 BitBlt(pDC->GetSafeHdc(), 0, 0, rc.Width(), rc.Height(), hbmDC, 0, 0, SRCCOPY); SelectObject(hbmDC, hOldbmp); DeleteObject(hbmDC); } void CMyFade::OnPaint() { CPaintDC dc(this); OnDraw(&dc); } HBITMAP CMyFade::CopyScreenToBitmap(LPRECT lpRect) { HDC hScrDC, hMemDC; // screen DC and memory DC int nX, nY, nX2, nY2; // coordinates of rectangle to grab int nWidth, nHeight; // DIB width and height int xScrn, yScrn; // screen resolution HGDIOBJ hOldBitmap, hBitmap; if (IsRectEmpty(lpRect)) return NULL; hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL); hMemDC = CreateCompatibleDC(hScrDC); xScrn = GetDeviceCaps(hScrDC, HORZRES); yScrn = GetDeviceCaps(hScrDC, VERTRES); nX = lpRect->left; nY = lpRect->top; nX2 = lpRect->right; nY2 = lpRect->bottom; if (nX < 0) nX = 0; if (nY < 0) nY = 0; if (nX2 > xScrn) nX2 = xScrn; if (nY2 > yScrn) nY2 = yScrn; nWidth = nX2 - nX; nHeight = nY2 - nY; hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight); hOldBitmap = SelectObject(hMemDC, hBitmap); // 将屏幕输入到内存DC里面去,再通过SelectObject到位图句柄里面 BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY); // 将刚刚得到到的屏幕位图置换出句柄 hBitmap=SelectObject(hMemDC, hOldBitmap); DeleteDC(hScrDC); DeleteDC(hMemDC); return (HBITMAP)hBitmap; } BOOL CMyFade::Create(CWnd *pParent, CRect rc) { if (!pParent || !IsWindow(pParent->GetSafeHwnd())) { MessageBox("error1"); return false; } CClientDC dcParent(pParent); if (dcParent.GetDeviceCaps(RASTERCAPS) & RC_PALETTE) { MessageBox("error2"); return FALSE; } GetDesktopWindow()->GetWindowRect(rc); CString szText; GetDesktopWindow()->GetWindowText(szText); if (!CWnd::CreateEx( 0, WC_FADEWND, szText, WS_POPUP | WS_CHILD | WS_VISIBLE, rc, pParent, NULL, NULL)) { MessageBox("error3"); return FALSE; } /* if(pParent!=GetDesktopWindow()) { m_pWndLock=pParent; m_pWndLock->EnableWindow(false); HRGN hRgn=CreateRectRgn(0, 0, 0, 0); int regiontype=m_pWndLock->GetWindowRgn(hRgn); if(regiontype!=ERROR) { MessageBox("error4"); SetWindowRgn(hRgn, false); } DeleteObject(hRgn); } */ m_hBitmap = CopyScreenToBitmap(rc); SetTimer(1, 2000, NULL); } ////////////////////////////////////////////////////////////////////////// // 核心函数,将屏幕变暗 HBITMAP CMyFade::FadeBitmap(HBITMAP hBmp, double dfTrans) { HBITMAP hRetBmp = NULL; if (hBmp) { HDC hBufferDC = CreateCompatibleDC(NULL); HGDIOBJ hPrevBufObject = SelectObject(hBufferDC, hBmp); HDC hDirectDC = CreateCompatibleDC(NULL); // DC for working if (hDirectDC) { BITMAP bm; GetObject(hBmp, sizeof(bm), &bm); BITMAPINFO bmInfo; ZeroMemory(&bmInfo,sizeof(bmInfo)); bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmInfo.bmiHeader.biWidth = bm.bmWidth; bmInfo.bmiHeader.biHeight = bm.bmHeight; bmInfo.bmiHeader.biPlanes = 1; bmInfo.bmiHeader.biBitCount = 32; UINT* ptPixels; HBITMAP hDirectBitmap = CreateDIBSection(hDirectDC, (BITMAPINFO*)&bmInfo, DIB_RGB_COLORS,(void**)&ptPixels, NULL, 0); if (hDirectBitmap) { // 将hDirectBitmap放入hDirectDC处理 HGDIOBJ hPrevBufDirObject = SelectObject(hDirectDC, hDirectBitmap); // 当前将原hBmp即屏幕的所有像素写入到hDirectDC // 即需要对像素灰度处理的DC BitBlt(hDirectDC,0,0,bm.bmWidth,bm.bmHeight,hBufferDC,0,0,SRCCOPY); int iAlpha = (int)(255.0 * dfTrans / 100.0); int nSize = bm.bmWidth * bm.bmHeight; for (int i=0; i> 16; int iSrcG = ptPixels[i] & 0x0000ff00 >> 8; int iSrcB = ptPixels[i] & 0x000000ff; int iGrey = (iSrcR * 54 + iSrcG * 182 + iSrcB * 19) >> 8; COLORREF Col =iGrey ; //RGB(iGrey, iGrey, iGrey) ; ptPixels[i] = RGB( (GetBValue( Col ) * iAlpha + iSrcB * (255 - iAlpha)) >> 8, (GetGValue( Col ) * iAlpha + iSrcG * (255 - iAlpha)) >> 8, (GetRValue( Col ) * iAlpha + iSrcR * (255 - iAlpha)) >> 8 ); } SelectObject(hDirectDC,hPrevBufDirObject); hRetBmp = hDirectBitmap; } DeleteDC(hDirectDC); } SelectObject(hBufferDC, hPrevBufObject); DeleteDC(hBufferDC); } return hRetBmp; } static i=0; void CMyFade::OnTimer(UINT nIDEvent) { CWnd::OnTimer(nIDEvent); CClientDC dc(this); i+=2; if(i>100) { KillTimer(1); return; } DWORD dwStartCount = timeGetTime(); if(m_hNewBitmap) { DeleteObject(m_hNewBitmap); m_hNewBitmap = NULL; } m_hNewBitmap = FadeBitmap(m_hBitmap,(double)i); OnDraw(&dc); DWORD dwEndCount = timeGetTime(); // tick counter in milliseconds }

19,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧