GradientDialog.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:9k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. // GradientDialog.cpp : implementation file
  16. //
  17. #include "stdafx.h"
  18. #include "bo2kgui.h"
  19. #include "GradientDialog.h"
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25. /////////////////////////////////////////////////////////////////////////////
  26. // GradientDialog dialog
  27. CGradientDialog::CGradientDialog()
  28. {
  29. CommonConstruct();
  30. }
  31. CGradientDialog::CGradientDialog(UINT uResource, CWnd* pParent /*=NULL*/)
  32. : CDialog(uResource, pParent)
  33. {
  34. CommonConstruct();
  35. }
  36. CGradientDialog::CGradientDialog(LPCTSTR pszResource, CWnd* pParent /*=NULL*/)
  37. : CDialog(pszResource, pParent)
  38. {
  39. CommonConstruct();
  40. }
  41. CGradientDialog::~CGradientDialog()
  42. {
  43. if(m_pGradBits) {
  44. free(m_pGradBits);
  45. m_pGradBits=NULL;
  46. }
  47. }
  48. void CGradientDialog::CommonConstruct()
  49. {
  50. VERIFY(m_HollowBrush.CreateStockObject(HOLLOW_BRUSH));
  51. m_bGradient=FALSE;
  52. m_pGradBits=NULL;
  53. //{{AFX_DATA_INIT(CGradientDialog)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. }
  57. void CGradientDialog::DoDataExchange(CDataExchange* pDX)
  58. {
  59. CDialog::DoDataExchange(pDX);
  60. //{{AFX_DATA_MAP(CGradientDialog)
  61. // NOTE: the ClassWizard will add DDX and DDV calls here
  62. //}}AFX_DATA_MAP
  63. }
  64. HBRUSH CGradientDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
  65. {
  66. switch(nCtlColor) {
  67. case CTLCOLOR_STATIC:
  68. // The Slider Control has CTLCOLOR_STATIC, but doesn't let
  69. // the background shine through,
  70. TCHAR lpszClassName[255];
  71. GetClassName(pWnd->m_hWnd, lpszClassName, 255);
  72. if(lstrcmp(lpszClassName, TRACKBAR_CLASS) == 0)
  73. return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  74. case CTLCOLOR_BTN:
  75. // let static controls shine through
  76. pDC->SetBkMode(TRANSPARENT);
  77. return HBRUSH(m_HollowBrush);
  78. default:
  79. break;
  80. }
  81. // if we reach this line, we haven't set a brush so far
  82. return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
  83. }
  84. BEGIN_MESSAGE_MAP(CGradientDialog, CDialog)
  85. //{{AFX_MSG_MAP(CGradientDialog)
  86. ON_WM_CTLCOLOR()
  87.   ON_WM_ERASEBKGND()
  88. ON_WM_QUERYNEWPALETTE()
  89.     ON_WM_PALETTECHANGED()
  90. ON_WM_RBUTTONDOWN()
  91. //}}AFX_MSG_MAP
  92. END_MESSAGE_MAP()
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CGradientDialog message handlers
  95. void CGradientDialog::SetGradient()
  96. {
  97. m_bGradient=FALSE;
  98. }
  99. void CGradientDialog::SetGradient(COLORREF c1, COLORREF c2, int dir)
  100. {
  101. m_bGradient=TRUE;
  102. m_Color1=c1;
  103. m_Color2=c2;
  104. m_nNumColors=236;
  105. m_nDir=dir;
  106. CreateGradPalette();
  107. CreateGradientBitmap();
  108. }
  109. BOOL CGradientDialog::OnEraseBkgnd(CDC* pDC) 
  110. {
  111. if(m_bGradient) {
  112. CRect rc;
  113. int w,h;
  114. GetClientRect(rc);
  115. w=rc.Width();
  116. h=rc.Height();
  117. if(w==0 || h==0) {
  118. return CDialog::OnEraseBkgnd(pDC);
  119. }
  120. if(m_pGradBits==NULL) return FALSE;
  121. StretchDIBits(pDC->GetSafeHdc(),0,0,w,h,0,0,320,240,m_pGradBits,&m_biGradBitInfo,DIB_RGB_COLORS,SRCCOPY);
  122. } else {
  123. return CDialog::OnEraseBkgnd(pDC);
  124. }
  125. return TRUE;
  126. }
  127. //BEGIN_MESSAGE_MAP(CGradpalWnd, CFrameWnd)
  128.     //ON_WM_PAINT()
  129. //END_MESSAGE_MAP()
  130. BOOL CGradientDialog::OnQueryNewPalette()
  131. {
  132.     CClientDC dc(this);
  133.     
  134.     CPalette *pPalOld = dc.SelectPalette(&m_Pal, FALSE);
  135.     
  136.     BOOL bRet = dc.RealizePalette();
  137.     
  138.     dc.SelectPalette(pPalOld, FALSE);
  139.     
  140.     if (bRet)
  141.         // some colors changed
  142.         this->Invalidate();
  143.     
  144.     return bRet;
  145. }
  146. void CGradientDialog::OnPaletteChanged(CWnd *pFocusWnd)
  147. {
  148.     if (pFocusWnd != this)
  149.         this->OnQueryNewPalette();
  150. }
  151. BOOL CGradientDialog::CreateGradPalette()
  152. {
  153.     if (m_Pal.GetSafeHandle() != NULL) {
  154. m_Pal.DeleteObject();
  155. }
  156.     
  157.     BOOL bSucc = FALSE;
  158.         
  159.     LPLOGPALETTE lpPal = (LPLOGPALETTE)new BYTE[sizeof(LOGPALETTE) +
  160.                                                 sizeof(PALETTEENTRY) *
  161.                                                 m_nNumColors];
  162.     
  163.     if (lpPal != NULL)
  164.     {
  165. int nIndex;
  166.         lpPal->palVersion = 0x300;
  167.         lpPal->palNumEntries = m_nNumColors;
  168.         
  169.         PALETTEENTRY *ppe = lpPal->palPalEntry;
  170.         int r1=GetRValue(m_Color1);
  171. int r2=GetRValue(m_Color2);
  172. int g1=GetGValue(m_Color1);
  173. int g2=GetGValue(m_Color2);
  174. int b1=GetBValue(m_Color1);
  175. int b2=GetBValue(m_Color2);
  176.         for (nIndex = 0; nIndex < m_nNumColors; nIndex++)
  177.         {
  178.             ppe->peRed = (BYTE) r1 + MulDiv((r2-r1),nIndex,m_nNumColors-1);
  179.             ppe->peGreen = (BYTE) g1 + MulDiv((g2-g1),nIndex,m_nNumColors-1);
  180.             ppe->peBlue = (BYTE) b1 + MulDiv((b2-b1),nIndex,m_nNumColors-1);
  181.             ppe->peFlags = (BYTE)0;
  182. m_PalVal[nIndex]=(ppe->peRed << 16) | (ppe->peGreen << 8) | (ppe->peBlue);
  183.             ppe++;
  184.         }
  185.         
  186.         bSucc = m_Pal.CreatePalette(lpPal);
  187.         delete [](PBYTE)lpPal;
  188.     }
  189.     
  190.     return bSucc;
  191. }
  192. void CGradientDialog::CreateGradientBitmap(void)
  193. {
  194. int x,y,w,h;
  195. CRect rectangle;
  196. w=320;
  197. h=240;
  198. if(m_pGradBits!=NULL) {
  199. free(m_pGradBits);
  200. m_pGradBits=NULL;
  201. }
  202. m_pGradBits=(DWORD*) malloc(w*h*sizeof(DWORD));
  203. memset(&m_biGradBitInfo,0,sizeof(BITMAPINFO));
  204. m_biGradBitInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
  205. m_biGradBitInfo.bmiHeader.biWidth=320;
  206. m_biGradBitInfo.bmiHeader.biHeight=240;
  207. m_biGradBitInfo.bmiHeader.biPlanes=1;
  208. m_biGradBitInfo.bmiHeader.biBitCount=32;
  209. m_biGradBitInfo.bmiHeader.biCompression=BI_RGB;
  210. if(m_nDir==0) {
  211. for(y=0;y<h;y++) {
  212. for(x=0;x<w;x++) {
  213. *(m_pGradBits+(y*w)+x)=m_PalVal[MulDiv(m_nNumColors,y,h)];
  214. }
  215. }
  216. }
  217. else if(m_nDir==1) {
  218. for(y=0;y<h;y++) {
  219. int l,r;
  220. l=MulDiv((m_nNumColors/2),y,h);
  221. r=l+(m_nNumColors/2)-1;
  222. for(x=0;x<w;x++) {
  223. *(m_pGradBits+(y*w)+x)=m_PalVal[l+MulDiv((r-l),x,w)];
  224. }
  225. }
  226. }
  227. else if(m_nDir==2) {
  228. for(x=0;x<w;x++) {
  229. for(y=0;y<h;y++) {
  230. *(m_pGradBits+(y*w)+x)=m_PalVal[MulDiv(m_nNumColors,x,w)];
  231. }
  232. }
  233. }
  234. else if(m_nDir==3) {
  235. for(y=0;y<h;y++) {
  236. int l,r;
  237. r=MulDiv((m_nNumColors/2),y,h);
  238. l=r+(m_nNumColors/2)-1;
  239. for(x=0;x<w;x++) {
  240. *(m_pGradBits+(y*w)+x)=m_PalVal[l+MulDiv((r-l),x,w)];
  241. }
  242. }
  243. }
  244. }
  245. void CGradientDialog::OnRButtonDown(UINT nFlags, CPoint point) 
  246. {
  247. if(m_bGradient) {
  248. HMENU hmenu=LoadMenu(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_GRADIENTMENU));
  249. HMENU hsubmenu=GetSubMenu(hmenu,0);
  250. ClientToScreen(&point);
  251. int nCmd=TrackPopupMenu(hsubmenu,TPM_LEFTALIGN|TPM_TOPALIGN|TPM_NONOTIFY|TPM_RETURNCMD|TPM_RIGHTBUTTON,
  252. point.x,point.y,0,GetSafeHwnd(),NULL);
  253. COLORREF dwFace=GetSysColor(COLOR_3DFACE);
  254. COLORREF dwLight=GetSysColor(COLOR_3DHILIGHT);
  255. COLORREF dwShadow=GetSysColor(COLOR_3DSHADOW);
  256. COLORREF dwFadeFace=dwFace;
  257. COLORREF dwFadeLight=RGB(255-((255-GetRValue(dwLight))/2),
  258. 255-((255-GetGValue(dwLight))/2),
  259. 255-((255-GetBValue(dwLight))/2));
  260. COLORREF dwFadeShadow=RGB(GetRValue(dwShadow)/2,
  261. GetGValue(dwShadow)/2,
  262. GetBValue(dwShadow)/2);
  263. switch(nCmd) {
  264. case IDM_COLOR_0:
  265. SetGradient(dwFace,dwLight,1);
  266. break;
  267. case IDM_COLOR_1:
  268. SetGradient(dwShadow,dwFace,1);
  269. break;
  270. case IDM_COLOR_2:
  271. SetGradient(dwFadeFace,dwFadeLight,1);
  272. break;
  273. case IDM_COLOR_3:
  274. SetGradient(dwFadeShadow,dwFadeFace,1);
  275. break;
  276. case IDM_COLOR_RED:
  277. SetGradient(RGB(216,115,115),RGB(226,66,66),1);
  278. break;
  279. case IDM_COLOR_ORANGE:
  280. SetGradient(RGB(240,148,80),RGB(234,107,72),1);
  281. break;
  282. case IDM_COLOR_YELLOW:
  283. SetGradient(RGB(239,235,136),RGB(172,170,56),1);
  284. break;
  285. case IDM_COLOR_GREEN:
  286. SetGradient(RGB(131,213,81),RGB(127,172,126),1);
  287. break;
  288. case IDM_COLOR_BLUE:
  289. SetGradient(RGB(25,174,222),RGB(120,177,203),1);
  290. break;
  291. case IDM_COLOR_VIOLET:
  292. SetGradient(RGB(150,129,183),RGB(102,110,176),1);
  293. break;
  294. case IDM_COLOR_BROWN:
  295. SetGradient(RGB(203,185,156),RGB(158,139,117),1);
  296. break;
  297. }
  298. InvalidateRect(NULL,TRUE);
  299. UpdateWindow();
  300. DestroyMenu(hmenu);
  301. }
  302. CDialog::OnRButtonDown(nFlags, point);
  303. }