I was working on a project some months ago, where the client had a showreel DVD with a number of videos on it. They needed a menu screen/window to pop up automatically whenever the DVD was inserted into a Windows PC. The Menu window was to be fairly straightforward, having a set of buttons, each of which was to trigger the playback of a different video. However, the client wanted the menu buttons to start each video, in whichever video-player was set as the default for that system.
I decided to code the app in C++, using the Cinder Framework (as the Menu needed some graphics and audio).
I came across various methods for opening files externally, via their default program. However, the simplest method was to use the ‘Shell Execute’ command.
The code snippet below, shows this method in action: it simply opens a video file in whichever program is assigned to open it by default.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
// -------------------------------------------------- // Opening a File (e.g. Video) in its default program // -------------------------------------------------- /* The code snippet was taken from an App created using Cinder, a C++ Framework for Creative Coding: http://libcinder.org/ However, 'ShellExecute' should work in standard Windows Desktop Apps. https://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx */ #include "cinder/app/AppNative.h" #include "cinder/gl/gl.h" #include <stdio.h>; //IMPORTANT! ShellExecute requires this header #include <shellapi.h>; using namespace ci; using namespace ci::app; using namespace std; class CinderApp : public AppNative { public: //-- standard Cinder Functions -- void setup(); void update(); void draw(); void mouseDown( MouseEvent event ); private: void playVideo(string videoFileName); }; void CinderApp::setup() { /* Setup Cinder App */ } void CinderApp::update() { /* Update state of App */ } void CinderApp::draw() { /* Draw graphics */ } //Cinder triggers this function when a mouse button is pressed void CinderApp::mouseDown( MouseEvent event ) { string filepath = "videos\\testVideo.wmv"; try { fs::path videoPath = getAssetPath( filepath ); wstring videoFilePath = videoPath.wstring(); LPCWSTR videoFilePathChars = (LPCWSTR)videoFilePath.c_str(); // THE IMPORTANT BIT! //-------------------------------------------------------------------- //The 'ShellExecute' function will open the video file externally, //in whichever application has been set as the default for WMV files. ShellExecute(0, 0, videoFilePathChars, 0, 0 , SW_SHOW ); //-------------------------------------------------------------------- } catch(...) { cout << "ERROR! could not play video!!!" << endl; } } |