小编for*_*ajt的帖子

正则表达式可选捕获组?

经过几个小时的搜索,我决定问这个问题.为什么这个正则表达式:^(dog).+?(cat)?不起作用,因为我认为它应该工作(捕获第一只狗和猫,如果有的话)?我在这里错过了什么?

dog, cat
dog, dog, cat
dog, dog, dog
Run Code Online (Sandbox Code Playgroud)

regex optional capturing-group

20
推荐指数
3
解决办法
4万
查看次数

TThread和COM - "CoInitialize尚未被调用",尽管在构造函数中调用了CoInitialize

我正在尝试在一个线程中使用COM接口.从我所读到的,我必须CoInitialize/CoUninitialize在每个线程中调用.

虽然这很好用:

procedure TThreadedJob.Execute;
begin
   CoInitialize(nil);

   // some COM stuff

   CoUninitialize;
end;
Run Code Online (Sandbox Code Playgroud)

当我将调用移动到构造函数和析构函数时:

TThreadedJob = class(TThread)
...
  protected
    procedure Execute; override;
  public
    constructor Create;
    destructor Destroy; override;
...

constructor TThreadedJob.Create;
begin
  inherited Create(True);
  CoInitialize(nil);
end;

destructor TThreadedJob.Destroy;
begin
  CoUninitialize;
  inherited;
end;

procedure TThreadedJob.Execute;
begin

   // some COM stuff

end;
Run Code Online (Sandbox Code Playgroud)

我得到EOleException:CoInitialize没有被称为异常,我不知道为什么.

delphi com multithreading ole tthread

8
推荐指数
1
解决办法
846
查看次数

在Delphi 7中覆盖组件的方法?

基于这个答案,我试图覆盖Delphi 7中的OnShowWindow方法TOleContainer.

unit MyOleContainer;

interface

uses
    Windows, OleCtnrs;

type
  TOleContainer = class(OleCtnrs.TOleContainer)
  private
    function OnShowWindow(fShow: BOOL): HResult; stdcall; override;
  end;

implementation

function TOleContainer.OnShowWindow(fShow: BOOL): HResult;
begin
   Result := S_OK;
end;

end.
Run Code Online (Sandbox Code Playgroud)

但这不会编译给出以下错误:[Error] MyOleContainer.pas(11): Field definition not allowed after methods or properties为什么?

编辑:

您能解释一下如何"声明IOleClientSite的实现,继承自TOleContainer并隐藏方法OnShowWindow [...]使用TOleContainer作为IOleClientSite"?

EDIT2:

这是你的意思吗?

TMyContainer = class(TOleContainer, IOleClientSite)
private 
   FIOleClientSite: IOleClientSite;
   function SaveObject: HResult; stdcall;
   ...

constructor TMyContainer.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Self.OleObjectInterface.GetClientSite(FIOleClientSite);
end;

function TMyContainer.SaveObject: HResult;
begin
  Result := …
Run Code Online (Sandbox Code Playgroud)

delphi methods components overriding

4
推荐指数
1
解决办法
1121
查看次数

区分字符串格式

有一个指向某个缓冲区的无类型指针,该缓冲区可以包含ANSI或Unicode字符串,如何判断它所持有的当前字符串是否为多字节?

c c++ string unicode ansistring

2
推荐指数
2
解决办法
287
查看次数

TListView:关于项目添加?

将项目添加到TListView时如何捕获事件?

OnInsert根据文件,我认为该活动将起到作用.它甚至将实际TListItem对象传递给处理程序:

OnInsert在将新项插入列表视图后立即发生.

编写一个OnInsert事件处理程序,以便在项刚刚添加到列表时进行响应.Item参数是添加到Items属性的TListItem对象

这是我的代码:

procedure TForm1.Button1Click(Sender: TObject);
begin
  with ListView1.Items.Add do
  begin
     Caption := 'foo';
     SubItems.Add('bar');
  end;
end;

procedure TForm1.TListView1Insert(Sender: TObject; Item: TListItem);
begin
   //Item is empty
   ShowMessage(Item.Caption);
end;
Run Code Online (Sandbox Code Playgroud)

但令人惊讶的Item.Caption是,它始终是空的.对我来说似乎胡说八道.

编辑:

Items.AddItem()按照建议切换到另一个奇怪的问题.该OnInsert事件处理程序现在将按预期,但TListView不显示TListItem.Caption.

procedure TForm1.Button1Click(Sender: TObject);
begin
  with ListView1.Items.Add do
  begin
     Caption := 'foo1';
     SubItems.Add('bar1');
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  item: TListItem;
begin
  item := TListItem.Create(ListView1.Items);
  item.Caption := 'foo2';
  item.Subitems.Add('bar2');
  ListView1.Items.AddItem(item);
end; 

procedure TForm1.ListView1Insert(Sender: TObject; …
Run Code Online (Sandbox Code Playgroud)

delphi listview delphi-7 tlistview

2
推荐指数
1
解决办法
577
查看次数

在Assign()之后,TListItem.Checked将自身切换为True

我遇到了TListItem的一些意外行为.检查以下测试代码:

  var
  Item: TListItem;

  //...

  ListView1.Checkboxes := True;
  with ListView1.Items.Add do
  begin
    Caption := 'old item';
    Checked := False;
  end;

  Item := TListItem.Create(ListView1.Items);
  Item.Caption := 'new item';
  Item.Checked := False;

  ListView1.Items[0].Assign(Item);

  Assert(ListView1.Items[0].Caption = 'new item');
  Assert(ListView1.Items[0].Checked = False); //WTF
Run Code Online (Sandbox Code Playgroud)

断言失败发生,因为Checked属性以某种方式将自身切换为True.我没有在帮助文件中找到关于此行为的任何评论.

这是一个错误还是一个功能?

我正在使用Delphi 7进行所有更新.

delphi listview delphi-7 listviewitem

1
推荐指数
1
解决办法
114
查看次数

简单的Delphi代码中的奇怪行为

我传递TProcessItem结构作为我的函数的返回值.在GetProcessFromHandle函数中,FindByID返回的结构的ExeFile属性包含期望的可执行文件名,但在GetProcessFromHandle返回的结构中,它以某种方式变为空.我添加了两个Messagebox来演示它.有人能够解释这里发生了什么吗?

function GetProcessFromHandle(hWnd: HWND): TProcessItem;
var
  ProcessInfo: TProcessInfo;
  PID: Cardinal;
begin
  Result := nil;
  GetWindowThreadProcessId(hWnd, @PID);
  ProcessInfo := TProcessInfo.Create(nil);
  try
    Result := ProcessInfo.RunningProcesses.FindByID(PID);
    if Assigned(Result) then ShowMessage(Result.ExeFile); //first message
  finally
    ProcessInfo.Free;
  end;
end;

procedure Test;
var
  Process: TProcessItem;
begin
  Process := GetProcessFromHandle(FindWindow(nil, 'My App'));
  if Assigned(Process) ShowMessage(Process.ExeFile); //second message
end; 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

delphi

0
推荐指数
1
解决办法
216
查看次数