如何用纯API函数使用flash.ocx?

解决方案 »

  1.   

    为什么不能MFC/VCL呢?(我很好奇...)
      

  2.   

    纯API?晕,那不就跟直接用C语言使用COM接口一样吗?有些麻烦
      

  3.   

    使用了 atl, 可能不符合你的要求了。
    不过用 atl 真的很简单,呵呵。====================    code    ======================// Thanks to Masterz
    #include "stdafx.h"
    #include <commdlg.h>#include "resource.h"#define MAX_LOADSTRING 100
    #import "c:\winnt\system32\macromed\flash\flash.ocx" 
    #include <atlbase.h>
     CComModule _Module;
    #include <atlwin.h>
    #pragma comment(lib,"atl")
    #define ODS(x) OutputDebugString(x)// Global Variables:
    HINSTANCE hInst; // current instance
    TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar textCAxWindow m_container;
    using namespace ShockwaveFlashObjects;
    IShockwaveFlash* shwaveflash;// Foward declarations of functions included in this code module:
    ATOM MyRegisterClass(HINSTANCE hInstance);
    BOOL InitInstance(HINSTANCE, int);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
      // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable; // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_TEST_SDK_COM, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance); CoInitialize(NULL); // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
    return FALSE;
    } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TEST_SDK_COM); // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    } CoUninitialize(); return msg.wParam;
    }//
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage is only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX);  wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = (WNDPROC)WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TEST_SDK_COM);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = (LPCSTR)IDC_TEST_SDK_COM;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);
    }//
    //   FUNCTION: InitInstance(HANDLE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;   hInst = hInstance; // Store instance handle in our global variable   AtlAxWinInit();   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);   if (!hWnd)
       {
          return FALSE;
       }   ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);   return TRUE;
    }//
    //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND - process the application menu
    //  WM_PAINT - Paint the main window
    //  WM_DESTROY - post a quit message and return
    //
    //
    HWND heditfilepath;LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    wsprintf(szHello,"use flash control in sdk");
    RECT rc; switch (message) 
    {
    case WM_CREATE:
    GetClientRect(hWnd, &rc );
    m_container.Create( hWnd, rc, LPCTSTR("ShockwaveFlash.ShockwaveFlash.1"), WS_CHILD|WS_VISIBLE);//create a browser control
    m_container.QueryControl( __uuidof(IShockwaveFlash), reinterpret_cast<void**>(&shwaveflash) );
    break;
    case WM_SIZING:
    GetClientRect(hWnd, &rc );
    rc.top = (rc.bottom+rc.top)/2;
    m_container.MoveWindow(&rc,true);
    break;
    case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
    //case 0x100:
    case IDM_ABOUT:
    {
    ODS("Start Flash\n");
    char buf[256];
    GetWindowText(heditfilepath,buf,255);
    _bstr_t bstr("D:\\firstrun.swf");
    ODS(buf);
    //bstr=_bstr_t("c:\\2.1.swf");
    shwaveflash->put_Movie(bstr); // you have to change the path here
    shwaveflash->Play();
    }
    break; // case IDM_ABOUT:
    //    DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    RECT rt;
    GetClientRect(hWnd, &rt);
    DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
    EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }// Mesage handler for about box.
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message)
    {
    case WM_INITDIALOG:
    return TRUE; case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
    {
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
    }
    break;
    }
        return FALSE;
    }
      

  4.   

    利用ATL制作程序多媒体封面组件 
    com的优越就不多说,使用atl开发com方便而快捷,并且具有size小,dependency少的特点。vc++的import 编译支持更是对com开发的巨大支持。闲话休说,这就开始吧。
      一.flash组件
            1.atl com appwizard创建新项目"atlflash",选择dll类型。
            2.加入atl object "flash"
                insert ->  new atl object
                选择object中的simple object,
                short name:“flash”
            3.接口添加method “play”
                参数为:[in]bstr bstrfile,[in]long hinstance,[in]long hwnd
            4.在cflash类中加入:
                private:
                          hwnd m_hwnd;//视频窗口句柄
            5. 实现play方法。
    stdmethodimp cflash::play(bstr bstrfile, long hinstance, long hwnd)
    {
        try{
            m_hmciwnd=(hwnd)hwnd;
            _bstr_t file(bstrfile);
            m_hmciwnd=::mciwndcreate((hwnd)hwnd,(hinstance)hinstance,
                ws_popup&brvbar;ws_visible&brvbar;
                mciwndf_noplaybar&brvbar;
                mciwndf_nomenu,                
                (char *)file);        rect rect;
            int sx,sy;
            ::getwindowrect(m_hmciwnd,&rect);
            sx=(::getsystemmetrics(sm_cxscreen)
                -rect.right+rect.left)/2;
            sy=(::getsystemmetrics(sm_cyscreen)
                -rect.bottom+rect.top)/2;    
            //窗口居中
            ::setwindowpos(m_hmciwnd,hwnd_topmost,sx,
                sy,0,0,swp_showwindow&brvbar;swp_nosize);
            
            g_nlength=mciwndgetlength(m_hmciwnd);
            mciwndplay(m_hmciwnd);
            settimer(m_hmciwnd,1,50,timerproc);
            
        }catch(...)
        {
            atltrace("error:%ul",::getlasterror()); 
        }
        return s_ok;
    }
      
              6.flash.cpp中实现timerproc,用于关闭视频窗口。
    long g_nlength;
    void callback timerproc(
                hwnd hwnd,    // handle of window for timer messages
                uint umsg,    // wm_timer message
                uint idevent,  // timer identifier
                dword dwtime  // current system time
                )
    {
        
        long nlength;
        bool bescape=::getkeystate(vk_escape)&0x0100;
        nlength=mciwndgetposition(hwnd);
                    //视频放完,或点击esc键,关闭视频窗口
        if((nlength>=g_nlength)&brvbar;&brvbar;(bescape)){
            killtimer(hwnd,idevent);
            mciwndend(hwnd);
            mciwndclose(hwnd);
            mciwnddestroy(hwnd);
        }
    };
            7.stdafx.h中加入
                #include <vwf.h>
                项目设置中加入“vwf32.lib”库,
                打开项目设置中c++,c++language中的enable error handling
                编译,一切ok。
    二,组件的使用。
          1.创建一个mdi或sdi项目"flashclient"
          2.stdafx.h中加入
              #import “atlflash.dll” no_namespace
              flashcient.cpp中加入
              struct _initcom{
        _initcom(){::coinitialize(null);}
        ~_initcom(){::couninitialize(); }
              }__initcom;
          3.cflashclientview加入
            private:
        iflashptr pflashserver;
          4.cflashclent::oninitialupdate()加入
        _bstr_t file("sample.avi");
        pflashserver->play(    file,(long)::afxgetinstancehandle(),(long)this->getsafehwnd());        
          5.编译,一切ok。
      

  5.   

    谢谢。。把flash做成alt思路很好
      

  6.   

    ioleobject这个类可以的但是不知道如何实现
      

  7.   

    在系统没有注册flash ocx下,如何使用呢?
    我自带了flash.ocx,行么?想不让程序弹出"***"没有注册的对话框?
    类似DLL一样。
    3ks!