我用atl创建一个fullcontrol控件支持mfc的。我想在里面调用一个vc做的ocx。我用add to project把控件导入,定义一个成员变量,直接通过这个成员变量访问控件的方法.我知道这是不行的。执行的时候就报错。我知道用Create方法是可以的。但是参数应该怎么设置。
如果用不支持mfc的那ocx怎么倒入?

解决方案 »

  1.   

    #import ...
    CComQIPtr<IWebBrowser2> m_spBrowser;
    CAxWindow content_wnd;
    ......
    if(  _tcslen(m_tcHtmlFileName) > 0 )
    {
    RECT rc;
    GetClientRect( &rc );
    if(m_spBrowser==NULL)
    {
    LPOLESTR pstrbrowserid;
    StringFromCLSID(IID_IWebBrowser2,&pstrbrowserid);
    _bstr_t bstrbrowser(pstrbrowserid);
    CoTaskMemFree(pstrbrowserid);
    if(content_wnd.IsWindow())
    content_wnd.DestroyWindow();
    content_wnd.Create( m_hWnd, rc, LPCTSTR(bstrbrowser), WS_CHILD&brvbar;WS_VISIBLE&brvbar;WS_HSCROLL&brvbar;WS_VSCROLL );//create a browser control
    HRESULT hrbrowser;
    hrbrowser = content_wnd.QueryControl( IID_IWebBrowser2, reinterpret_cast<void**>(&m_spBrowser) );
    }#import "c:\winnt\system32\mswinsck.ocx" no_namespace
    ...
    IMSWinsockControlPtr pWinsock;
    _bstr_t pstr("mswinsock.winsock");
    CLSID clsidwinsock;
    CLSIDFromProgID(pstr,&clsidwinsock);
    pWinsock.CreateInstance(clsidwinsock);
    if(pWinsock)
    {
    pWinsock->RemotePort=13;
    pWinsock->RemoteHost=_bstr_t("time-a.nist.gov");
    _variant_t varoptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
    HRESULT hr= pWinsock->Connect();
    if(FAILED(hr))
    MessageBox(NULL,"failed","failed",MB_OK);
    ......pWinsock->Close();
    }
    else
    MessageBox(NULL,"can not create winsock","error",MB_OK); ////////////////////////////////////////////////////////////////////////////////
    // Use swflash.ocx to play flash 
    // if it works, it is written by masterz, otherwise I don't know who writes it(*_*)
    ////////////////////////////////////////////////////////////////////////////////
    #include "stdafx.h"
    #define MAX_LOADSTRING 100
    #include <atlbase.h>
     CComModule _Module;
    #include <atlcom.h>
    #include <atlwin.h>
    #pragma comment(lib,"atl")
    #import "c:\winnt\system32\macromed\flash\swflash.ocx"
    using namespace ShockwaveFlashObjects;// Global Variables:
    HINSTANCE hInst; // current instance
    TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
    CAxWindow m_container;// Foward declarations of functions included in this code module:
    ATOM MyRegisterClass(HINSTANCE hInstance);
    BOOL InitInstance(HINSTANCE, int);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    MSG msg;
    // Initialize global strings
    wsprintf(szTitle,"use flash control in sdk exe");
    wsprintf(szWindowClass,"flashinsdk");
    MyRegisterClass(hInstance);
    CoInitialize(NULL);
    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
    return FALSE;
    }
    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
    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(NULL, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = NULL;//(LPCSTR)IDC_FLSH;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 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
    //
    //
    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");
    IShockwaveFlash* shwaveflash;
    HWND hbtnstart;
    RECT rc;
    switch (message) 
    {
    case WM_CREATE:
    GetClientRect(hWnd, &rc );
    rc.top = (rc.bottom+rc.top)/2;
    m_container.Create( hWnd, rc, LPCTSTR("ShockwaveFlash.ShockwaveFlash.1"), WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL );//create a browser control
    hbtnstart=CreateWindow("BUTTON","play",WS_CHILD|WS_VISIBLE,0,0,120,30,hWnd,(HMENU)0x100,hInst,0);
    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:
    m_container.QueryControl( __uuidof(IShockwaveFlash), reinterpret_cast<void**>(&shwaveflash) );
    shwaveflash->put_Movie(_bstr_t("c:\\downloads\\radin\\flash\\2.1.swf")); // you have to change the path here
    shwaveflash->Play();
    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;
    }
      

  2.   

    看不懂能不能重点说说create的几个参数的用法。现在我用的是支持mfc的atl.
    content_wnd.Create( m_hWnd, rc, LPCTSTR(bstrbrowser), WS_CHILD&brvbar;WS_VISIBLE&brvbar;WS_HSCROLL&brvbar;WS_VSCROLL );//
    brvbar
        BOOL Create(LPCTSTR lpszWindowName, DWORD dwStyle,
    const RECT& rect, CWnd* pParentWnd, UINT nID,
    CFile* pPersist = NULL, BOOL bStorage = FALSE,
    BSTR bstrLicKey = NULL)
    里面的参数应该怎么生成怎么填。我的被调用控件生成的类叫CFrs_com.调用控件生成的类叫CMachineObj。谢谢 我是刚入门什么都不懂。
      

  3.   

    你仔细看看上面那个使用flash控件的代码。
      

  4.   

    masterz,如果是MFC写的有什么不同呢?
      

  5.   

    IMSWinsockControlPtr pWinsock;
    IShockwaveFlash* shwaveflash;
    两个例子都有这个定义。但是我只有一个ocx的release版本 我怎么知道 它的类名是什么。
    不过无论如何 我先给50分
      

  6.   

    查看 CWindow 类的 Create 方法:
    LPCTSTR lpstrWndClass,          -- 窗口类名
    HWND hWndParent,                 -- 该窗口的父窗口
    _U_RECT rect = NULL,             -- 窗口的位置和尺寸信息
    LPCTSTR szWindowName = NULL,     -- 你要创建的窗口的窗口名
    DWORD dwStyle = 0,               -- 窗口的风格(这个地方你要是不懂我也没办法告诉你,你要进修进修)
    DWORD dwExStyle = 0,             -- 窗口的扩展风格
    _U_MENUorID MenuOrID = 0U,       -- 窗口的菜单或本窗口 ID (一般你就放 ID)
    LPVOID lpCreateParam = NULL      -- 存放 CREATESTRUCT 结构我觉得这些东西如果你都不懂的话,真的别人要给你解释很麻烦的,你最好先学学 SDK 编程
      

  7.   

    对于一个 .ocx,一般来说有相关帮助文档,经常都带有 .chm 格式的帮助文档,名称同 .ocx 的名称,如果这些都没有,那么你用开发工具:ole/com 对象察看器也能够看到所有方法,不过这些前提就是你要对 com 有概念性的正确的认识。
      

  8.   

    我用import后编译就可以看到他生成的*tlh可以查出他的类名。或者注册表都能找到.
    我刚看了createwindow的用法,下面是我的代码。
    hWnd = CreateWindow("BUTTON", "hello", WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
    RECT rc;
    ::GetClientRect(hWnd, &rc );
    if(m_container.IsWindow())
    m_container.DestroyWindow();
    m_container.Create( hWnd, rc, LPCTSTR("FRSCOM.FrscomCtrl.1"), WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL );//
    m_container.QueryControl( __uuidof(_DFrs_com), reinterpret_cast<void**>(&m_FRS) m_FRS->IsOpen();
    问题是m_frs在queryControl之后就变成了0x0000000.当执行m_FRS->IsOpen();它就报错了。
    CreateWindow的窗口类名我填了BUTTON,,但我并不需要button,我应该填什么?那个错误是不是因为窗口类名引起的。
    谢谢