locked
how to use shape.parent RRS feed

  • Question

  • hi.

    I'm using vsto2005 excel development. my project is built on vs2005 + excel 20003
    I want draw something temp on excel workbook, but don't know how to get the handle.
    I try create a transparent activex  using addoleobject method, drawing something throught afxgetapp()->getmainwnd()->getdc(), but it always draw out of workbook, since the hwnd handle is excel application.
    so i think i can use shape.parent (shape comes from sheet.shapes.addoleobject()), i search the web, no detail found about "parent"
    can anybody give me some hints
    • Moved by Cindy Meister MVP Friday, August 7, 2009 4:29 PM not about VSTO technology (From:Visual Studio Tools for Office)
    Tuesday, August 4, 2009 7:27 AM

Answers

  • Hi,

    This really isn't the right place for your question as this forum is meant for questions regarding Visual Studio Tools For Office.  VSTO is a specific technology that involves .Net programming, so this isn't best place to go when you have MFC/ATL questions.  I can tell you that Parent isn't going to help you since it is going to surface the Excel.WorkSheet object for the worksheet that the control lives on.  What you really want to do is call IOleInPlaceSiteWindowless::GetDC.  This assumes two things.  First, that Excel supports windowless controls (which I believe it does) and that your control is actually a windowless control. 

    In order to draw on Excel's surface successfully, you must be a windowless control (and Excel must support that).  Being a windowless control gives you a hook into the container's message queue (via IOleInPlaceObjectWindowless::OnWindowMessage).  Minimally, you would need to respond to WM_PAINT messages, etc by redrawing whatever it is that you want to draw.  Otherwise, whenever Excel repaints the worksheet whatever your drew will get erased.  I suppose you could also achieve this by subclassing the document window.

    If you are truly a windowless control, you should have access to the IOleInPlaceSiteWindowless.  You can call GetDC off this interface to get the device context you need.  IOleInPlaceSiteWindowless (if it isn't exposed directly in your control's object model) can be obtained by first calling QueryInterface on IOleClientSite for IOleInPlaceSite/Ex and then calling QueryInterface on that for IOleInPlaceSiteWindowless (if Excel supports windowless controls, then this QI will succeed).  Again, though, unless you are hooked into the message loop, drawing isn't going to do you any good because it will be erased on the first repaint.

    If Excel does not support IOleInPlaceSiteWindowless, you could get the document HWND (and from that the DC) by calling IOleClientSite::GetWindow.  If you have to go this route, you'll definitely have to subclass the document window so you can redraw yourself in response to WM_PAINT messages.

    Sorry that I can't give you the specifics for the technology that you are using, but its been years since I've programmed with MFC.  You'll need to look elsewhere for help if you need further information.  Try posting any further questions to the MFC/OLE newsgroup here: http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.vc.mfcole&cat=en_US_38185632-ee25-4a6f-bbb0-1a5ab6251b47&lang=en&cr=US

    Sincerely,

    Geoff Darst
    Microsoft VSTO Team
    Thursday, August 6, 2009 6:05 PM

All replies


  • Based on my understanding, what you want to do is to draw something on a sheet of a workbook. We can use Add methods of Sheet.Shapes such as AddShape(), AddLine(), and AddOLEObject() ...; I don't quite catch it. Why you want to use shape.parent?

    If I have misunderstood, please let us know.

    Best regards,
    Bessie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, August 6, 2009 6:54 AM
  • Hi,

    This really isn't the right place for your question as this forum is meant for questions regarding Visual Studio Tools For Office.  VSTO is a specific technology that involves .Net programming, so this isn't best place to go when you have MFC/ATL questions.  I can tell you that Parent isn't going to help you since it is going to surface the Excel.WorkSheet object for the worksheet that the control lives on.  What you really want to do is call IOleInPlaceSiteWindowless::GetDC.  This assumes two things.  First, that Excel supports windowless controls (which I believe it does) and that your control is actually a windowless control. 

    In order to draw on Excel's surface successfully, you must be a windowless control (and Excel must support that).  Being a windowless control gives you a hook into the container's message queue (via IOleInPlaceObjectWindowless::OnWindowMessage).  Minimally, you would need to respond to WM_PAINT messages, etc by redrawing whatever it is that you want to draw.  Otherwise, whenever Excel repaints the worksheet whatever your drew will get erased.  I suppose you could also achieve this by subclassing the document window.

    If you are truly a windowless control, you should have access to the IOleInPlaceSiteWindowless.  You can call GetDC off this interface to get the device context you need.  IOleInPlaceSiteWindowless (if it isn't exposed directly in your control's object model) can be obtained by first calling QueryInterface on IOleClientSite for IOleInPlaceSite/Ex and then calling QueryInterface on that for IOleInPlaceSiteWindowless (if Excel supports windowless controls, then this QI will succeed).  Again, though, unless you are hooked into the message loop, drawing isn't going to do you any good because it will be erased on the first repaint.

    If Excel does not support IOleInPlaceSiteWindowless, you could get the document HWND (and from that the DC) by calling IOleClientSite::GetWindow.  If you have to go this route, you'll definitely have to subclass the document window so you can redraw yourself in response to WM_PAINT messages.

    Sorry that I can't give you the specifics for the technology that you are using, but its been years since I've programmed with MFC.  You'll need to look elsewhere for help if you need further information.  Try posting any further questions to the MFC/OLE newsgroup here: http://www.microsoft.com/communities/newsgroups/en-us/default.aspx?dg=microsoft.public.vc.mfcole&cat=en_US_38185632-ee25-4a6f-bbb0-1a5ab6251b47&lang=en&cr=US

    Sincerely,

    Geoff Darst
    Microsoft VSTO Team
    Thursday, August 6, 2009 6:05 PM