How To Enumerate OLE and VB Controls from an OLE Control

This article was previously published under Q141414

SUMMARY

For an OLE control to communicate with another control placed on the same Visual Basic form, the OLE control needs access to at least one interface pointer of the other control. This article illustrates a technique you can use to enumerate both OLE and Visual Basic controls present on a particular form and retrieve an interface pointer to these controls. Note that the same technique could be applied to enumerate controls placed on other control containers, provided that container exposes the functionality required to implement the technique.

MORE INFORMATION

Given a pointer to IOleClientSite, it is possible to enumerate through all of the other controls on a form by making use of the following interfaces:
IOleClientSite
IOleContainer
IEnumUnknown
IUnknown
IOleObject
IOleClientSite
IOleControlSite
Note that most of these interfaces are container-side interfaces, so the technique mentioned here is container dependent. For this method to work, the container must provide support for IOleContainer, which is currently defined as a mandatory interface in the OLE control container guidelines. Both MFC version 4.0 and greater and Visual Basic version 4.0 and greater OLE control containers provide support for this interface. Before using this method with another control container, ensure that it provides support for IOleContainer.

The method itself is illustrated by the sample code listed in this article. You can incorporate the code into an OLE control generated using ControlWizard. Use the code to enumerate all the controls, internal Visual Basic controls as well OLE controls, by calling IOleContainer::EnumObjects, and passing the following flags as its first parameter:
   OLECONTF_EMBEDDINGS: is used to retrieve OLE Controls.
   OLECONTF_OTHERS    : is used to retrieve other objects such as Visual
                        Basic internal controls.

   hr = lpContainer->EnumObjects(OLECONTF_EMBEDDINGS | OLECONTF_OTHERS,
                              &lpEnumUnk);
				
The differentiating aspect between OLE controls and other objects such as internal Visual Basic controls is that only OLE controls support the IOleObject interface. Hence, if a QueryInterface for IID_IOleObject fails for an object, then it is a different type of object. Also, if the control container provides support for Extended controls as does Visual Basic 4.0, the Extended control for a particular OLE control can also be retrieved using the method illustrated by the sample code.

Note that most of the functionality provided by the following sample code depends solely on the extent of the functionality exposed by the control container itself.

Also note that the LPOLECLIENTSITE pointer can be obtained through a call to COleControl::GetClientSite().

Sample Code

void EnumAllControlNames(LPOLECLIENTSITE lpSite)
{
   LPOLECONTAINER lpContainer;
   LPENUMUNKNOWN lpEnumUnk;

   // Note that the IOleContainer interface is currently defined as
   // mandatory. It must be implemented by control containers,
   // in the OLE Control Containers Guidelines.
   HRESULT hr = lpSite->GetContainer(&lpContainer);
   if(FAILED(hr)) {
      OutputDebugString(_T("Unable to get to the container./n"));
      return;
   }

   // OLECONTF_EMBEDDINGS is used to retrieve OLE Controls.
   // OLECONTF_OTHERS is used to retrieve other objects such as
   // Visual Basic internal controls
   hr = lpContainer->EnumObjects(
                       OLECONTF_EMBEDDINGS | OLECONTF_OTHERS,
                       &lpEnumUnk);
   if(FAILED(hr)) {
      lpContainer->Release();
      return;
   }

   LPUNKNOWN lpUnk;
   while (lpEnumUnk->Next(1, &lpUnk, NULL) == S_OK) {
      LPOLEOBJECT lpObject = NULL;
      LPOLECONTROLSITE lpTargetSite = NULL;
      LPOLECLIENTSITE lpClientSite = NULL;
      LPDISPATCH lpDisp;

      hr = lpUnk->QueryInterface(
                    IID_IOleObject, (LPVOID*)&lpObject);
      if(SUCCEEDED(hr)) {
         // This is an OLE control.
         // Navigate to the Extended Control because Visual Basic 4.0 uses
         // Extended controls.
         hr = lpObject->GetClientSite(&lpClientSite);
         if(SUCCEEDED(hr)) {
            // You have the IOleClientSite interface
            hr = lpClientSite->QueryInterface(
                       IID_IOleControlSite, (LPVOID*)&lpTargetSite);
            if(SUCCEEDED(hr)) {
               // You have the IOleControlSite interface
               // Get the IDispatch for the extended control.
               // Note that Extended controls are optional in the OLE
               // specifications for OLE Control Containers.
               hr = lpTargetSite->GetExtendedControl(&lpDisp);
            }
         }
      }
      else {
         // This is either an internal VB control or the
         // VB form itself.
         hr = lpUnk->QueryInterface(
                       IID_IDispatch, (LPVOID*)&lpDisp);
      }

      if(SUCCEEDED(hr)) {
         VARIANT va;
         VariantInit(&va);
         DISPID dispid;
         DISPPARAMS dispParams = { NULL, NULL, 0, 0 };

         // Get the names of all the controls present in a VB form.
         LPWSTR lpName[1] = { (WCHAR *)L"Name" };
         hr = lpDisp->GetIDsOfNames(IID_NULL, lpName, 1,
                                      LOCALE_SYSTEM_DEFAULT, &dispid);

         if(SUCCEEDED(hr)) {
            hr = lpDisp->Invoke(dispid/*0x80010000*/, IID_NULL,
                                  LOCALE_SYSTEM_DEFAULT,
                                  DISPATCH_PROPERTYGET |
                                  DISPATCH_METHOD,
                                  &dispParams, &va, NULL, NULL);
            if(SUCCEEDED(hr)) {
               CString szTmp((LPCWSTR)va.bstrVal);
               // szTmp now has the name.
               OutputDebugString(_T("And the name is ... ") + szTmp +
                                 _T("/n"));
            }
         }
         lpDisp->Release();
      }

      // Release interface pointers.
      if(lpObject)     lpObject->Release();
      if(lpTargetSite) lpTargetSite->Release();
      if(lpClientSite) lpClientSite->Release();

      lpUnk->Release();
   }    // End of While statement

   // Final clean up
   lpEnumUnk->Release();
   lpContainer->Release();
}
				
Additional words: VB VC visualc cdk ocx
When using PyQt5 with a QTableWidget, if you want to retrieve the name of the selected column, you can use the `currentItem()` function to access the current item in the table and then its `data()` method along with `column` attribute to obtain the data and column index. Here's an example: ```python from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem class TableSelectionExample(QWidget): def __init__(self): super().__init__() self.table = QTableWidget() self.init_table() layout = QVBoxLayout() layout.addWidget(self.table) self.setLayout(layout) def init_table(self): headers = ["Column 1", "Column 2", "Column 3"] self.table.setColumnCount(len(headers)) self.table.setHorizontalHeaderLabels(headers) for i in range(10): # Example data row = [] for j, header in enumerate(headers): item = QTableWidgetItem(f"Row {i}, Column {j+1}") row.append(item) self.table.setRow(i, row) def select_and_get_column_name(self): current_item = self.table.currentItem() if current_item: column_index = current_item.column() column_name = self.table.horizontalHeaderItem(column_index).text() return column_name if __name__ == "__main__": app = QApplication([]) ex = TableSelectionExample() ex.show() selected_column = ex.select_and_get_column_name() print(f"The selected column name is: {selected_column}") # Replace this line with your desired action after getting the column name app.exec_() ``` In this code snippet, we create a simple QTableWidget, fill it with some sample data, and define a method `select_and_get_column_name()` that retrieves the selected column name when a cell is clicked or selected. Remember to install and properly import the necessary modules for PyQt5, as shown in the provided error message[^1]. If you're still encountering issues with importing, make sure you have PyQt5 installed correctly and the environment is set up accordingly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值