只需要将 ParentFont置为False
http://blog.163.com/xd8171@126/blog/static/62081043200910179332166/
http://hi.baidu.com/jangill/item/4daeb8f1b5c68517ce9f325c
http://hi.baidu.com/jangill/item/53b6d8b896049ea4eaba935a
以TImage为例,运用程序要调用Dll中创建的Timage控件,将图片显示到外部DC,如果将TImage 赋值,通常会出现 “cannot assign a tfont to a tfont”的错误,因为dll不支持TFont的直接赋值。 这样的话,我们只需要将 控件的 ParentFont 属性设为 False 就可以。
不过由于TImage的ParentFont(派生于TControl)没有public 或 published,所以对TImage稍微做点工作。
type
TMyImage = class(TImage)
private
protected
public
published
property ParentFont;
end;
在Dll创建的TImage用 TMyImage类 代替,只需要将 ParentFont置为False,在接口函数中传入运用程序
Form窗口的Self(TWinControl)值作为 MyImage.Parent 。
procedure InitImage(AParent: TWinControl);
var
oSelImg: TMyImage;
begin
oSelImg:=TMyImage.Create(nil);
oSelImg.AutoSize:=true;
oSelImg.Picture.LoadFromFile('d:\11.bmp');
oSelImg.ParentFont:= False;
oSelImg.Parent:= AParent;
oSelImg.Show;
end;
不过,这样的参数传递自然比不上直接传DC的Handle方便,我们需要在DLL中做一下改动来实现。
var
ControlAtom: TAtom;
ControlAtomString: string;
RM_GetObjectInstance: DWORD;// registered window message
procedure _InitControls;
begin
ControlAtomString:= Format('ControlOfs%.8X%.8X', [GetModuleHandle(nil), GetCurrentThreadID]);
ControlAtom:= GlobalAddAtom(PChar(ControlAtomString));
RM_GetObjectInstance:= RegisterWindowMessage(PChar(ControlAtomString));
end;
function _FindControl(Handle:HWnd):TWinControl;
var
OwningProcess: DWORD;
begin
Result:= nil;
if (Handle<>0) and
(GetWindowThreadProcessID(Handle,OwningProcess)<>0) and
(OwningProcess=GetCurrentProcessId)then
begin
if GlobalFindAtom(PChar(ControlAtomString)) = ControlAtom then
Result:= Pointer(GetProp(Handle, MakeIntAtom(ControlAtom)))
else
Result:= Pointer(SendMessage(Handle, RM_GetObjectInstance, 0, 0));
end;
end;
procedure InitImage(AParent: HWND);
var
oSelImg: TMyImage;
begin
oSelImg:=TMyImage.Create(nil);
oSelImg.AutoSize:=true;
oSelImg.Picture.LoadFromFile('d:\11.bmp');
oSelImg.ParentFont:= False;
oSelImg.Parent:= _FindControl(AParent);