added some development tools
[windows-sources.git] / developer / VSSDK / VisualStudioIntegration / Tools / Wizards / Templates / VC / MyToolWindow.h
blob2b83777651282d3554725dddfae4f3c4c8f5e783
1 //
2 // MyToolWindow.h
3 //
4 // This file contains the implementation of a tool window that hosts a .NET user control
5 //
7 #pragma once
9 #include <AtlWin.h>
10 #include <VSLWindows.h>
12 #include "..\%ProjectName%UI\Resource.h"
14 class %ProjectClass%WindowPane :
15 public CComObjectRootEx<CComSingleThreadModel>,
16 public VsWindowPaneFromResource<%ProjectClass%WindowPane, IDD_%ProjectClass%_DLG>,
17 public VsWindowFrameEventSink<%ProjectClass%WindowPane>,
18 public VSL::ISupportErrorInfoImpl<
19 InterfaceSupportsErrorInfoList<IVsWindowPane,
20 InterfaceSupportsErrorInfoList<IVsWindowFrameNotify,
21 InterfaceSupportsErrorInfoList<IVsWindowFrameNotify3> > > >
23 VSL_DECLARE_NOT_COPYABLE(%ProjectClass%WindowPane)
25 protected:
27 // Protected constructor called by CComObject<%ProjectClass%WindowPane>::CreateInstance.
28 %ProjectClass%WindowPane() :
29 VsWindowPaneFromResource()
32 ~%ProjectClass%WindowPane() {}
34 public:
36 BEGIN_COM_MAP(%ProjectClass%WindowPane)
37 COM_INTERFACE_ENTRY(IVsWindowPane)
38 COM_INTERFACE_ENTRY(IVsWindowFrameNotify)
39 COM_INTERFACE_ENTRY(IVsWindowFrameNotify3)
40 COM_INTERFACE_ENTRY(ISupportErrorInfo)
41 END_COM_MAP()
43 BEGIN_MSG_MAP(%ProjectClass%WindowPane)
44 COMMAND_HANDLER(IDC_CLICKME_BTN, BN_CLICKED, OnButtonClick)
45 END_MSG_MAP()
47 // Function called by VsVsWindowPaneFromResource at the end of SetSite; at this point the
48 // window pane is constructed and sited and can be used, so this is where we can initialize
49 // the event sink by siting it.
50 void PostSited(IVsPackageEnums::SetSiteResult /*result*/)
52 VsWindowFrameEventSink<%ProjectClass%WindowPane>::SetSite(GetVsSiteCache());
55 // Callback function called by ToolWindowBase when the size of the window changes.
56 void OnFrameSize(int x, int y, int w, int h)
58 // Center button.
59 CWindow button(this->GetDlgItem(IDC_CLICKME_BTN));
60 RECT buttonRectangle;
61 button.GetWindowRect(&buttonRectangle);
63 OffsetRect(&buttonRectangle, -buttonRectangle.left, -buttonRectangle.top);
65 int iLeft = (w - buttonRectangle.right) / 2;
66 if (iLeft <= 0)
68 iLeft = 5;
71 int iTop = (h - buttonRectangle.bottom) / 2;
72 if (iTop <= 0)
74 iTop = 5;
77 button.SetWindowPos(NULL, iLeft, iTop, 0, 0, SWP_NOSIZE);
80 LRESULT OnButtonClick(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& bHandled)
82 // Load the message from the resources.
83 CComBSTR strMessage;
84 VSL_CHECKBOOL_GLE(strMessage.LoadStringW(_AtlBaseModule.GetResourceInstance(), IDS_BUTTONCLICK_MESSAGE));
86 // Get the title of the message box (it is the same as the tool window's title).
87 CComBSTR strTitle;
88 VSL_CHECKBOOL_GLE(strTitle.LoadStringW(_AtlBaseModule.GetResourceInstance(), IDS_WINDOW_TITLE));
90 // Get the UI Shell service.
91 CComPtr<IVsUIShell> spIVsUIShell = GetVsSiteCache().GetCachedService<IVsUIShell, SID_SVsUIShell>();
92 LONG lResult;
93 VSL_CHECKHRESULT(spIVsUIShell->ShowMessageBox(0, GUID_NULL, strTitle, strMessage, NULL, 0, OLEMSGBUTTON_OK, OLEMSGDEFBUTTON_FIRST, OLEMSGICON_INFO, FALSE, &lResult));
95 bHandled = TRUE;
96 return 0;
101 class %ProjectClass%ToolWindow :
102 public VSL::ToolWindowBase<%ProjectClass%ToolWindow>
104 public:
105 // Constructor of the tool window object.
106 // The goal of this constructor is to initialize the base class with the site cache
107 // of the owner package.
108 %ProjectClass%ToolWindow(const PackageVsSiteCache& rPackageVsSiteCache):
109 ToolWindowBase(rPackageVsSiteCache)
113 // Caption of the tool window.
114 const wchar_t* const GetCaption() const
116 static CStringW strCaption;
117 // Avoid to load the string from the resources more that once.
118 if (0 == strCaption.GetLength())
120 VSL_CHECKBOOL_GLE(
121 strCaption.LoadStringW(_AtlBaseModule.GetResourceInstance(), IDS_WINDOW_TITLE));
123 return strCaption;
126 // Creation flags for this tool window.
127 VSCREATETOOLWIN GetCreationFlags() const
129 return CTW_fInitNew|CTW_fForceCreate;
132 // Return the GUID of the persintence slot for this tool window.
133 const GUID& GetToolWindowGuid() const
135 return CLSID_guidPersistanceSlot;
138 IUnknown* GetViewObject()
140 // Should only be called once per-instance
141 VSL_CHECKBOOLEAN_EX(m_spView == NULL, E_UNEXPECTED, IDS_E_GETVIEWOBJECT_CALLED_AGAIN);
143 // Create the object that implements the window pane for this tool window.
144 CComObject<%ProjectClass%WindowPane>* pViewObject;
145 VSL_CHECKHRESULT(CComObject<%ProjectClass%WindowPane>::CreateInstance(&pViewObject));
147 // Get the pointer to IUnknown for the window pane.
148 HRESULT hr = pViewObject->QueryInterface(IID_IUnknown, (void**)&m_spView);
149 if (FAILED(hr))
151 // If QueryInterface failed, then there is something wrong with the object.
152 // Delete it and throw an exception for the error.
153 delete pViewObject;
154 VSL_CHECKHRESULT(hr);
157 return m_spView;
160 // This method is called by the base class after the tool window is created.
161 // We use it to set the icon for this window.
162 void PostCreate()
164 CComVariant srpvt;
165 srpvt.vt = VT_I4;
166 srpvt.intVal = IDB_FRAME_IMAGES;
167 // We don't want to make the window creation fail only becuase we can not set
168 // the icon, so we will not throw if SetProperty fails.
169 if (SUCCEEDED(GetIVsWindowFrame()->SetProperty(VSFPROPID_BitmapResource, srpvt)))
171 srpvt.intVal = 1;
172 GetIVsWindowFrame()->SetProperty(VSFPROPID_BitmapIndex, srpvt);
176 private:
177 CComPtr<IUnknown> m_spView;