Transparent MFC Window by vamsi

This is a Win32 Application, which shows a Transparent MFC Window such that the Window looks same as the background window present on the desktop. On this Window I have done some GDI work Using the GDI class CPen.

On moving the mouse on the window it displays four colored lines from the four corners of the MFC window. To get the co-ordinates of the window I used MFC CRect class GetClientRect function, and to draw lines from the corners of the window I used MFC CClientDC class object which gets the Device context.

Following is the code for this Win32 Application.

#include <afxwin.h>
class CWinFrame:public CFrameWnd
{ 
   private:
    CRect rectObj;
     public:
     CWinFrame()
     {
       //Registers a Window of our own style since here I didn't specify any
       //Background colour, the window background will be transparent.
       CString strMyClass=AfxRegisterWndClass(NULL);
       //Creating a Window of our own style.
       Create(strMyClass,"Trans",WS_POPUP);
     }
     void OnPaint()
     {
       //gets the client Area coordinates
       GetClientRect(&rectObj);
     }
     void OnRButtonDown()
     {
      //To quit from the Application
      PostQuitMessage(1);
     }
     void OnMouseMove(UINT flag,CPoint pointObj)
     {
       CClientDC dc(this);
      //GDI Object to draw coloured lines from four sides of the window
      CPen penObj;
      penObj.CreatePen(0,2,RGB(0,0,255));
      dc.SelectObject(&penObj);
      dc.MoveTo(rectObj.left,rectObj.top);
      dc.LineTo(pointObj.x,pointObj.y);
      penObj.DeleteObject();
      //Creates a Pen with the given RGB Color combination. 
      penObj.CreatePen(0,2,RGB(0,255,0));
      dc.SelectObject(&penObj);
      dc.MoveTo(rectObj.right,rectObj.top);
      dc.LineTo(pointObj.x,pointObj.y);
      penObj.DeleteObject();
      penObj.CreatePen(0,2,RGB(255,0,0));
      dc.SelectObject(&penObj);
      dc.MoveTo(rectObj.left,rectObj.bottom);
      dc.LineTo(pointObj.x,pointObj.y);
      penObj.DeleteObject();
      penObj.CreatePen(0,2,RGB(255,255,0));
      dc.SelectObject(&penObj);
      dc.MoveTo(rectObj.right,rectObj.bottom);
      dc.LineTo(pointObj.x,pointObj.y);
   }
   DECLARE_MESSAGE_MAP()
 };
 BEGIN_MESSAGE_MAP(CWinFrame,CFrameWnd)
      ON_WM_PAINT()
      ON_WM_MOUSEMOVE()
      ON_WM_RBUTTONDOWN()
 END_MESSAGE_MAP()
 class CMainApp:public CWinApp
 {
   public:
   BOOL InitInstance()
   {
      CWinFrame *wndFrame;
      wndFrame=new CWinFrame();
      m_pMainWnd=wndFrame;
      wndFrame->ShowWindow(3);
      return TRUE;
   }
 };
 CMainApp appObj;

To run this Application,

  1. Create a New Win32 Application in Visual C++ 6.00.
  2. Give some name to the project for creating the Transparent MFC window.
  3. Choose an Empty Project in the Wizard, where you have to enter the Project Type.
  4. After creating the project, click on Menu –> Project –> Add to Project –> New –> C++ Source File
  5. Give a name for the C++ File and Create the file.
  6. Copy the Above source code and compile it.
  7. The compiler will give two linker errors as follows

nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __beginthreadex
Debug/TransWindow.exe : fatal error LNK1120: 2 unresolved externals

    8. Click on the Menu –> Project –> Settings –> General (Tab) –> Microsoft Foundation Classes, choose Use MFC in Shared DLL.
9. Now Build and Run the Application.