在C ++中到Delphi [英] In the c + + to Delphi
本文介绍了在C ++中到Delphi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
uses Windows, SysUtils, Variants, Forms, ComObj, OleServer, WordXP;
...
...
...
function GetPowerPointText(fName:String): String;
var
PP, PRE, SLI: OleVariant;
i, j: Integer;
begin
Result:='';
try
PP:=CreateOleObject('PowerPoint.Application');
except
MessageBox(0,'PowerPoint not Install.','Error',MB_OK);
Exit;
end;
// 읽어올 파일이 있는지 체크
if not FileExists(fName) then begin
MessageBox(0,'파일이 없습니다.','Error',MB_OK);
Exit;
end;
// 생성한 파워포인트 어플리케이션에서 파일을 읽음 = 프리젠테이션을 Open
PRE:=PP.Presentations.Open(fName,False,True,False);
for i:=1to PRE.Slides.Count do begin
SLI:=PRE.Slides.Item(i);
for j:=1to SLI.Shapes.Count do begin
if (SLI.Shapes.Item(j).HasTextFrame) then
Result:=Result+#13#10+SLI.Shapes.Item(j).TextFrame.TextRange.Text;
Application.ProcessMessages;
end; // for j
end; // for i
SLI:=Unassigned;
PRE:=Unassigned;
PP.Quit;
PP:=Unassigned;
end;
推荐答案
您想将Delphi函数转换为C ++函数吗? Delphi的编译器隐藏了许多繁琐的细节,并使自动化变得更容易.您将需要编写数百行代码,才能使用带有COM和普通API的C ++实现相同的功能.
您可能想先从Microsoft看一下示例. CppAutomatePowerPoint [ http://msdn.microsoft.com/zh-cn/library/aa155776(v = office.10).aspx [
Do you want to translate the Delphi function into a C++ function? Delphi''s compiler hides lots of tedious details and makes Automation easier. You will need to write hundreds of lines to achieve the same functionality using C++ with COM and plain APIs.
You may want to look into the example from Microsoft first.CppAutomatePowerPoint[^]. Also, you might be interested in http://msdn.microsoft.com/en-us/library/aa155776(v=office.10).aspx[^]. This one is old but it''s still worth reading.
I just rewrote the first part of your codes. But it should help you grab the idea a bit.
CoInitialize(NULL);
//------------PP:=CreateOleObject('PowerPoint.Application');-------
CLSID ClassID;
CLSIDFromProgID(TEXT("PowerPoint.Application"), &ClassID);
IUnknown *pUnk;
HRESULT hr = CoCreateInstance(ClassID, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
if (hr != S_OK)
return 0;
IDispatch *pDisp;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&pDisp);
if (hr != S_OK)
{
pUnk->Release();
return 0;
}
//------------PRE:=PP.Presentations.Open(fName,False,True,False);
//Get Presentations first
OLECHAR* szPresentations = OLESTR("Presentations");
DISPID dispid_Presentations;
hr = pDisp->GetIDsOfNames(IID_NULL, &szPresentations, 1, LOCALE_USER_DEFAULT, &dispid_Presentations);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
return 0;
}
DISPPARAMS dpNoArgs = {NULL, NULL, 0, 0};
VARIANT vResult;
IDispatch *pDispPresentations;
hr = pDisp->Invoke(dispid_Presentations, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET, &dpNoArgs, &vResult, NULL, NULL);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
return 0;
}
pDispPresentations = vResult.pdispVal;
//Open the file
VARIANT vArgsOpen[4];
for (int i = 0; i < 4; ++i)
{
VariantInit(vArgsOpen+i);
}
vArgsOpen[0].vt = VT_BSTR;
vArgsOpen[0].bstrVal = SysAllocString(L"I need to go out for lunch.ppt");;
vArgsOpen[1].vt = VT_BOOL;
vArgsOpen[1].boolVal = FALSE;
vArgsOpen[2].vt = VT_BOOL;
vArgsOpen[2].boolVal = TRUE;
vArgsOpen[3].vt = VT_BOOL;
vArgsOpen[3].boolVal = FALSE;
DISPPARAMS dpOpen;
dpOpen.cArgs = 4;
dpOpen.cNamedArgs = 0;
dpOpen.rgvarg = vArgsOpen;
IDispatch *pDispPresentation;
OLECHAR* szOpen = OLESTR("Open");
DISPID dispid_Open;
hr = pDispPresentations->GetIDsOfNames(IID_NULL, &szOpen, 1, LOCALE_USER_DEFAULT, &dispid_Open);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();
return 0;
}
hr = pDispPresentations->Invoke(dispid_Open, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD, &dpOpen, &vResult, NULL, NULL);
SysFreeString(vArgsOpen[0].bstrVal);
if (hr != S_OK)
{
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();
return 0;
}
pDispPresentation = vResult.pdispVal;
pDispPresentation->Release();
pDisp->Release();
pUnk->Release();
pDispPresentations->Release();
CoUninitialize();
return 1;
您可以尝试使用Microsoft Dev Studio中的类向导将控件包装在C ++类中.它可以很好地处理细节,并为您提供了实现的捷径.
在许多情况下都可以正常工作.
You could try using the class wizard in Microsoft Dev Studio to wrap the control in a C++ class. It handles the details fairly well, and will give you a shortcut to implementation.
It works fine in many cases.
这篇关于在C ++中到Delphi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文