Applying Visual Effects to the Desktop'�Shoot
Environment: VC6 SP4
Introduction
This article discusses how an application can apply visual effects to the Desktop, by copying Desktop contents, applying effects on it using GDI functions, and then re-displaying it. Additionally, this application also demonstrates how to play WAV files using Win32 API.
Shoot comes in handy when you are frustrated with your computer and would like to literally shoot the brains out of it. This application, when run, converts your mouse pointer to a gun's target finder and wherever you click, it makes a gun shot sound and burns a hole through the Desktop. To close the application, just press the Escape key.
Background
Like all other windows, the Desktop window also has a device context associated with it. The Win32 API GetDCEx(...) can be used to get a handle to this Desktop device context. By using this handle, you can copy the Desktop window contents to some device context of your application. Then, by using any combination of GDI operations, various visual effects can be applied to it.
Using the Code
We start copying the Desktop window in the constructor of the dialog class because we need to do this before the window for this application comes up. We first get a handle to the Desktop device context and then convert it to a pointer to a CDC object. We also get the screen resolution.
CDC *pDesktopDC = CDC::FromHandle (::GetDCEx(NULL, NULL, 0)); int screenMaxX = GetSystemMetrics(SM_CXSCREEN); int screenMaxY = GetSystemMetrics(SM_CYSCREEN);
Then, we create a device context in memory that is compatible with the Desktop device context.
m_pProcDC = new CDC; m_pProcDC->CreateCompatibleDC (pDesktopDC);
When a memory device context is created, a 1x1 pixel-sized bitmap is selected to it and hence GDI operations on it do not produce the required effect. We circumvent this by creating a bitmap of the same dimension as the Desktop and selecting it to the memory device context.
m_pBMP = new CBitmap;
m_pBMP->CreateCompatibleBitmap (pDesktopDC, screenMaxX,
screenMaxY);
m_pProcDC->SelectObject (m_pBMP);
The memory device context is now ready for use and we can copy the contents of the Desktop device context to it.
m_pProcDC->BitBlt(0, 0, screenMaxX, screenMaxY, pDesktopDC,
0, 0, SRCCOPY);
Once this is done, you can apply any visual effect to the memory device context and then redisplay it.
However, in this application we will not apply the visual effect right now. We need to create a bullet mark on the screen when the user clicks. For that, we use a bitmap resource named IDB_SHOTMARK, which is like a gunshot mark. To use this bitmap, we load it and select it to another device context created in memory.
m_pShotDC = new CDC; m_pShotDC->CreateCompatibleDC(pDesktopDC); m_pShotBmp = new CBitmap; m_pShotBmp->LoadBitmap ( IDB_SHOTMARK ); m_pShotDC->SelectObject ( m_pShotBmp );
When we are done with copying the Desktop contents and loading bitmaps, we are ready to display them.
In the OnInitDialog, we make the dialog cover the whole Desktop, by giving it the size of the Desktop and making it stay always on top by using a call to SetWindowPos.
::SetWindowPos( this->GetSafeHwnd(), HWND_TOPMOST, 0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SWP_SHOWWINDOW);
We also keep the gun's view finder cursor, which is another resource, ready, by loading it.
HINSTANCE hInstResource = AfxFindResourceHandle
(MAKEINTRESOURCE(IDC_TF),
RT_GROUP_CURSOR);
m_hCursor = ::LoadCursor(hInstResource, MAKEINTRESOURCE(IDC_TF));
In the OnPaint handler, we display the Desktop window by copying it from the device context in memory where we had stored it previously to the dialog's device context.
CDC *pDC = GetDC();
pDC->BitBlt (0, 0, screenMaxX, screenMaxY, m_pProcDC,
0, 0, SRCCOPY );
ReleaseDC(pDC);
The only thing that is left is to show the gunshot mark and make the sound when the user clicks. For this, we handle the message WM_LBUTTONDOWN where we copy the contents of the gunshot bitmap loaded in the m_pShotDC device context to the point where the user clicked. We also play the WAV file by using the PlaySound function. To use this function, you need to link the VC98\Lib\WINMM.LIB with your application and include the mmsystem.h header file in your application.
void CShootDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
...
// Show gunshot mark
CDC *pDC = GetDC();
pDC->BitBlt (point.x - 10, point.y - 10, 48, 48, m_pShotDC,
0, 0, SRCAND);
ReleaseDC(pDC);
...
// Play gunshot sound
::PlaySound((LPCTSTR)soundFile, NULL,
SND_FILENAME | SND_ASYNC | SND_NODEFAULT |
SND_PURGE);
...
}
Points of Interest
The project assumes that the winmm.lib is available as ..\..\VC98\Lib\WINMM.LIB. So, you need to extract the shoot sources in the MyProjects folder or change the relative path before building the application.
The application assumes that the gunshot.wav file is present in the same folder as the executable. If it does not find the WAV file, the gunshot sound will not be made.
Downloads
Download demo executable - 71 KbDownload source - 77 Kb

Comments
Ðлиника ÐÐ»Ð°Ñ - инноваÑионнÑй медиÑинÑкий ÑенÑÑ
Posted by HeepsyMed on 04/22/2013 12:42amhttp://www.kava-time.com.ua/kiev/klinika-ilaya-innovatsionniy-meditsinskiy-tsentr.html ÐовейÑие меÑодики леÑÐµÐ½Ð¸Ñ ilaya - ÑпÑоÑение и ÑдеÑевление ÑложнÑÑ Ð¾Ð¿ÐµÑаÑий УлÑÑÑаÑовÑеменнÑй ÑндоÑкоп PENTAX (ЯпониÑ) ÑкÑпеÑÑного клаÑÑа Ð¸Ð¼ÐµÐµÑ ÑникалÑнÑÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ Ñанней диагноÑÑики новообÑазований желÑдоÑно-киÑеÑного ÑÑакÑа Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ Ð¿ÑогÑÐ°Ð¼Ð¼Ñ Â«i-scan» (виÑÑÑалÑÐ½Ð°Ñ Ñ ÑомоÑндоÑкопии) без Ð²Ð²ÐµÐ´ÐµÐ½Ð¸Ñ ÐºÐ¾Ð½ÑÑаÑÑного веÑеÑÑва (1-й обÑÑановка в СÐÐ). ТÑеÑкаÑÑ Ð²Ð¾Ð·Ð¼Ð¾Ð¶Ð½Ð¾ÑÑÑ Ð¾Ð´Ð½Ð¾Ð²Ñеменного Ñ Ð´Ð¸Ð°Ð³Ð½Ð¾ÑÑикой вÑÐ¿Ð¾Ð»Ð½ÐµÐ½Ð¸Ñ ÑндоÑкопиÑеÑÐºÐ¸Ñ Ð¾Ð¿ÐµÑаÑий. ÐиагноÑÑика и леÑение поÑеÑно-каменной болезни биполÑÑнÑм ÑезекÑоÑкопом Richard Wolf (ÐеÑманиÑ) и лазеÑной ÑÑÑановкой Lumenis (СШÐ) позволÑÐµÑ ÑÑагменÑиÑоваÑÑ Ð³ÑÐ°Ð½Ð¸Ñ Ñ Ð²ÑÑкий локализаÑии (поÑка, моÑеÑоÑник, моÑевой пÑзÑÑÑ Ð¸ моÑеиÑпÑÑкаÑелÑнÑй канал) и воÑÑÑановиÑÑ ÑÑÐ¾Ð´Ð¸Ð½Ð°Ð¼Ð¸ÐºÑ Ð¿Ð¾Ñле неÑазделÑно визиÑ. Ð ilaya возможно дейÑÑвие ÑндоÑкопиÑеÑÐºÐ¸Ñ Ð¾Ð¿ÐµÑаÑий ÑообÑазно леÑÐµÐ½Ð¸Ñ Ð´Ð¾Ð±ÑокаÑеÑÑвенной гипеÑплазии пÑедÑÑаÑелÑной Ð¶ÐµÐ»ÐµÐ·Ñ (аденома), вклÑÑÐ°Ñ Ð¢Ð£Ð Ð (ТÑанÑÑÑеÑÑалÑÐ½Ð°Ñ ÑезекÑÐ¸Ñ Ð¿ÑоÑÑаÑÑ) и лазеÑнÑÑ ÑнÑклеаÑÐ¸Ñ Ð¿ÑоÑÑаÑÑ Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ Lumenis (СШÐ). ÐÑ Ð¿ÐµÑевели ÑÑжелÑе Ñ Ð¸ÑÑÑгиÑеÑкие опеÑаÑии в велиÑина «ХиÑÑÑгии одного днÑ».
Replyjust what i needed
Posted by garyofcourse on 06/24/2008 09:47amAbhinaba, thanks a ton for writing this article. Im new to windows programming and I spent the whole day asking people on the msdn, codeproject, and codeguru forums on how to draw stuff on the desktop till someone gave me a link to this article. Been a great help, gonna go try your code. Thanks again.
ReplyFabulous job Mr.Abhinaba Basu
Posted by vemireddy587 on 03/18/2007 05:57amApplying visual effects to the desktop - shoot
Posted by Legacy on 02/04/2004 12:00amOriginally posted by: Michael
Thanks for the article. In your article regarding the PlaySound API, you mentioned to include mmsystem.h in the project and "To use this function, you need to link the VC98\Lib\WINMM.LIB with your application". Please tell me how to do this in VC++ 6.0? Thanks.
Michael
Reply