Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / MFC
Article

OCX Snapshot

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
9 Feb 2000Public Domain 72.3K   2.3K   25   3
How to take a snapshot of an OCX without using HWNDs.
  • Download demo project - 33 Kb
  • Download source files - 27 Kb
  • Sample Image - ViewObjSnap.jpg

    Recently, the company I CO-OP for (Bradley Ward Systems) needed a way to show complex data reports over the web. We already have lots of custom OCXs that present these reports in graph or chart form. These OCXs are used in our custom applications - they were never intended to be embedded in web pages.

    Because we wanted to make the web reports as light-weight as possible, refitting the OCXs to also be embedded in web pages was simply not an option - we didn't want to impose stringent system requirements on the computers that would be used to view these reports. We decided that the best way to show these reports over the web was to take a "snapshot" of one of our OCXs and write it out to a JPEG for display on a web page.

    So, our biggest problem was how to get a picture of one of our OCXs... We tried various ways of getting the picture by using the HWND of a form our OCX was embedded on. There were two major problems with this: first, a visible form would have to appear on the screen of the webserver whenever we wanted to get a picture of an OCX; second, a user would always have to be logged onto the webserver in order to create the form that the OCX was embedded on. We simply didn't find a way around these problems.

    So, instead of using the HWND to trick the OCX into painting into a DC of our creation, we went a little deeper...

    Every OCX must implement the standard IViewObject interface - of which one of the methods is Draw(). Sounds perfect, right? Almost - we also have to determine the dimensions of the OCX, functionality which IViewObject does not expose. We used the IOleObject interface's GetExtents() for this. I wrote a small ActiveX control that makes these calls, and returns an IPicture object for embedding in a Picture control on a VB form.

    This ActiveX control I present here - called ViewObjSnap - is a scaled-down version of the one I wrote at work. It doesn't write the picture out to a JPG or anything like that, so you would need to modify it a little to be useful in a real world situation. It is only intended to demonstrate an interesting solution to our problem.

    The source code for the ViewObjSnap control is contained in the ViewObjSnap_src.zip file. The source code and a demo VB project, along with the compiled EXE for those that don't own VB, are in ViewObjSnap_demo.zip.

    License

    This article, along with any associated source code and files, is licensed under A Public Domain dedication


    Written By
    Web Developer
    United States United States
    Programming in C++ (and various other languages) since 1993. Started with OWL, then went on to MFC, ATL, Java, etc...

    Graduated from Georgia Tech in December 2001.

    Comments and Discussions

     
    QuestionDoes anybody try with C++? Pin
    Jason_Huang@Acer3-Feb-04 23:06
    Jason_Huang@Acer3-Feb-04 23:06 
    GeneralMissing Lib Pin
    Jose Cruz16-Oct-02 10:10
    Jose Cruz16-Oct-02 10:10 
    GeneralRe: Missing Lib Pin
    Abhijeet Dev26-Oct-03 3:41
    Abhijeet Dev26-Oct-03 3:41 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.