Simple Direct Draw Using MFC

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Environment: Visual C++ 6

Firstly, forgive me for my code. I have really tried my best to make is as easy to read as possible but DirectX is never the easiest thing to code. I originally wanted to write DirectX applications but found that every book I read relating to DirectX was coded using SDK material and not MFC. I wanted to create a very simple wrapper that allowed the easy display of bitmaps on the screen using DirectDraw.

The code itself is fairly limited to displaying bitmaps on the screen (FULLSCREEN MODE) using DirectDraw but it shows the concepts of using wrappers to make your programs far easier to use. I could have put it all in a DLL but realised that this would not be simple for you all to use. Anyway, here is how you use the libraries.

1. Include the following header into your Application Header or StdAfx.h. E.g

#include "DirectControl.h"

2. Create an instance of the CDirectControl Object. For example:


CDirectControl m_Control;

Please note that this should be placed either as a global variable or inside the Application class header (i.e. not temporary)

3. In the App Init Instance call the following code:


 m_pMainWnd = m_Control.CreateFullScreen(800,600,16);
if (!m_pMainWnd)
return FALSE;

Replace the 800 and 600 with the required x & y resolutions that you want to use. Replace the 16 with the bits-per-pixel required. My program does not maintain or handle the Palette because I was only really using hi-color modes for the bitmaps.

4. You can now use the Controller to create bitmaps (from the resource) using the code :


m_Control.CreateOffScreenSurface("My Bitmap",IDB_IMAGE);

The first parameter is the object identifier that I use to manipulate or select the object at a further time. The second parameter is the resource id of the bitmap.

You also need to tell the controller that the bitmap is available to be rendered. The default is false so you need to set the visibility by using the code:


m_Control.GetSurface("My Bitmap")->SetRender(TRUE);

5. The bitmaps are defaulted to the top left of the screen (x=0,y=0) but you can alter its position by using:


m_Control.GetSurface("My Bitmap")->SetPosition(10,10);

Relating to the x & y screen co-ordinates. The program automatically clips to the screen.

6. Once you have created your bitmap/s you can render the screen by calling:


 m_Control.Render(TRUE,FALSE,FALSE);
The first parameter decides if you want to clear the screen first before rendering. The second decides whether you want to render any bitmaps that have been flagged renderable. the third parameter decides whether you want to update the Screen. I did it this way because you dont want to update the screen all the time e.g. if you want to use one bitmap , change its position and render it again then update the screen.

Ok. What I suggest is that you look at the example project I have done. If anyone wishes to expand it further etc. I would welcome and more additional source code or comments. Please dont laugh at my code. I just wanted Direct X to be easy for everyone. Enjoy.

BTW... I havn't implemented the Escape key so you will have to press ALT-F4 to quit.

Downloads

Download demo project - 28 Kb


Comments

  • Please help me on Directshow + MFC

    Posted by nvnoi76 on 12/01/2004 09:56pm

    Hi, I write a program to play Video files using Directshow and MFC ( Visual C++ 6.0 , DirectX 9.0 ) I compile in DEBUG mode, It runs well and correctly. But when I compile in RELEASE mode. It reports error : "Unhandle exception in Playwnd.exe (MFC42.dll)0xC0000005: Access Violation" Is there any problem on MFC42.dll ? Please help me to correct it. Thank you very much. Noi - nvnoi76@yahoo.com

    Reply
  • DirectX : embed text in video stream ~Need help

    Posted by Legacy on 10/30/2003 12:00am

    Originally posted by: HS

    Hi, I'm working on video capturing program 
    
    and would like to put text in video stream.
    Does anyone know how can this be possible?
    Thank you.

    ~HS~

    Reply
  • Need Help Loading X Files in Direct X

    Posted by Legacy on 10/04/2003 12:00am

    Originally posted by: Ace Corban

    Im gay :}

    Reply
  • Good for beginners

    Posted by Legacy on 05/08/2003 12:00am

    Originally posted by: sunil

    I found this example helpful to begin with DirectX
    thanks

    Reply
  • Same old Stopping problem

    Posted by Legacy on 08/15/2002 12:00am

    Originally posted by: amit mankikar

    hi,

    i have used your classes in my SDI application to display Bitmaps in full screen mode. However i want the additional functionality of going back to the windowed mode as soon as the user presses "Escape".

    How do I do it ?

    amit mankikar

    Reply
  • No header includes with texture and utils

    Posted by Legacy on 03/19/2002 12:00am

    Originally posted by: Shura

    No header includes with texture and utils

    Reply
  • sehr sch�n

    Posted by Legacy on 12/09/2001 12:00am

    Originally posted by: Olaf kliss


    Hallo Eva,
    sehr sch�n gemacht, weiter so. Ich bin auf der Suche nach den Methoden zur direct Programmierung.

    Reply
  • GUI not responding to mouse events after playing mpeg file using DirectShow

    Posted by Legacy on 10/25/2001 12:00am

    Originally posted by: RK

    The problem I have is as follows:

    An MPEG file is played using DirectShow in GUI made using MFC on VC++. I pause the video clip midway, in order to draw a rectangle around a chosen area in the paused frame. But the GUI now no longer is able to respond to mouse events. Can somebody give me a clue as to why this could be happening ?

    Regards,
    RK

    Reply
  • Who to play MPG files using DDraw Pay windows SDK with MFC appli. ??

    Posted by Legacy on 09/24/2001 12:00am

    Originally posted by: Marty

    I don't know who to play an MPEG file in MFC application with running a simple player windows with direct draw. (I've the SDK direct X 8.0a.
    
    Thanks

    Reply
  • Extremly easy to use

    Posted by Legacy on 08/24/2001 12:00am

    Originally posted by: Imran Baig

    So easy to use, just download it and compile.
    

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date