File Preview Dialog Class

Many times you may want to allow your users to see a preview of the file that they are about to open. CPreviewFileDialog does just that.

The only work that you will have to do is to write a class derived from CAbstractPreview . Two functions will have to be implemented in this class.

SetPreviewFile(const CString& csFileName) -
This function is called each time the user changes his selection. This method allows you to open the file and get things ready for painting it in the Open File Dialog ( and yes , you can use it for a save Dialog also).

Keep in mind that the file name passed may not be a valid one. For example, if the user chooses a directory, you will get the name of the directory. It is upto you to decide how you want this case handled.

DrawPreview(CDC *pDC,int x,int y,int width,int height) -
This routine will be called to actually paint the image. The DC which is sent is not clipped. You will have to make sure that you keep within the bounds. The preview is shown on the right hand side of the window. By default, the width and height of the preview window are 100 bytes. You may want to draw on a smaller scale . Or, more often , you may have to scale your view to the smallest fitting rectangle. You can call the routine Calc for this. Pass it the width and height of your original view and the window width and height passed to you. You will get a CRect which centres your view.

One more small thing ! The sample project contains a dialog called "FILEOPEN_TEMPLATE". You will have to copy this into your project resource file.

Also , please do not forget to include CPreviewFileDialog.cpp in your project. I`m aware that the file name has an extra C prefixed, but I had to rename it as it was conflicting with some of my older projects which are in the include path.

In every other way, use CPreviewFileDialog exactly as you would CFileDialog. Just pass its constructor a pointer to a class derived from CAbstractPreview.

CPreviewFileDialg also provides a checkbox the user can use to disable the preview feature.

The files to look at in the sample are CTestPreviewDoc - take a look at OnFileOpen...

I have used a support class called CDIB to handle Device Independent Bitmaps. You are free to use it , but let me warn you , no comments in the code and no formal help from me. Be warned also, that it is meant primarily for 8 bit bitmaps. If you have a doubt , mail me rrajivram@hotmail.com and I`ll try to help.

Have a look at the sample and happy previewing.

Downloads

Download demo project - 30 Kb

Download source - 2 Kb

History



Comments

  • Vista?

    Posted by bobquain on 09/01/2009 01:27am

    I wish this can run on Vista, thanks~~~

    Reply
  • File preview in Visual Basic 6.0?

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

    Originally posted by: Henk

    It seems that this is what I'm looking for, but I need this for a VB project. Is there a way of implement this code into my VB project?
    Best regards,
    Henk

    Reply
  • How to get view of GIF and JPEG and Tiff (Almost all Image file Formats)

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

    Originally posted by: Nagesh

    Rajeev,
    This problem may be simple as per your view , but i am struggled to know how to show it on Dialog. I even tried a saparate code to show it but i don't get success.
    I am eager to know it. So please help me. If possible send me code.

    Thanx,
    Nagesh

    Reply
  • Windows 98 - 2000

    Posted by Legacy on 09/27/2000 12:00am

    Originally posted by: xavier

    Windows 98 - 2000 allow resizing common dialog...
    Do you know a way I can catch this event?

    Reply
  • Just what I was looking for..

    Posted by Legacy on 01/13/2000 12:00am

    Originally posted by: Neil Burton

    This worked flawlessly from compile to run. Thanks.

    Reply
  • Very nice, but can you explain it more?

    Posted by Legacy on 06/27/1999 12:00am

    Originally posted by: John Gann

    Rajiv,

    This is very nice and it works as you say but I'm trying to understand why. Sorry if my questions are too simple but I have struggled with customizing the common dialogs before and have never seen this technique. Can you tell me how you discovered this?

    I'm looking at your dialog template and see only two controls. The static group box seems to have the same id as the common file dialog template. Is this a requirement?

    It seems that the extra space to hold the preview rect is proportional to (but not the same as) the size of your dialog template.

    Any further comments appreciated.

    Thanks for posting this.

    John

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds