我有一个奇怪的问题,在不同版本的Delphi中使用接口.以下最小化代码在Delphi XE及更高版本中编译和运行,但在Delphi 7中没有.具体来说,在Delphi 7中编译时,function TForm1.Load: IMoleculeSubject;似乎没有返回正确的结果,即对新创建的实例的正确引用.你能帮忙评论一下原因和可能的解决方法吗?非常感谢!
unit uInterface;
interface
type
IMoleculeSubject = interface
['{BEB4425A-186C-45DF-9DCE-C7175DB0CA90}']
end;
TMoleculeSubject = class(TInterfacedObject, IMoleculeSubject)
end;
implementation
end.
Run Code Online (Sandbox Code Playgroud)
unit uBusiness;
interface
uses
uInterface;
type
TMoleculeDecorator = class(TMoleculeSubject)
private
FID: Integer;
public
property ID: Integer read FID;
constructor Create;
end;
implementation
{ TMoleculeDecorator }
constructor TMoleculeDecorator.Create;
begin
inherited Create;
FID := Random(100);
end;
end.
Run Code Online (Sandbox Code Playgroud)
unit Unit1;
interface
uses
uInterface, uBusiness,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls,
Forms, Dialogs;
type
TForm1 = …Run Code Online (Sandbox Code Playgroud) 如下图所示,在JPanel(500X500)上绘制了一个AttributedString。
如FontMetrics.getStringBounds()跟踪输出所示,该 AttributedString 的宽度为 164.0。
java.awt.geom.Rectangle2D$Float[x=0.0,y=-12.064453,w=164.0,h=15.09375]
Run Code Online (Sandbox Code Playgroud)
不过图片建议宽度应该是300-400(因为面板的宽度是500)。
你能帮忙评论原因和解决方法吗?

import javax.swing.*;
import java.awt.*;
import java.awt.font.TextAttribute;
import java.text.AttributedString;
class MyJPanel extends JPanel {
MyJPanel() {
setPreferredSize(new Dimension(500,500));
}
@Override
public void paintComponent(Graphics gold) {
super.paintComponent(gold);
Graphics2D g = (Graphics2D)gold;
//
AttributedString text = new AttributedString("Bunny rabits and flying ponies");
text.addAttribute(TextAttribute.FONT, new Font("Arial", Font.BOLD, 24), 0, "Bunny rabits".length());
text.addAttribute(TextAttribute.FOREGROUND, Color.RED, 0, "Bunny rabits".length());
text.addAttribute(TextAttribute.FONT, new Font("Arial", Font.BOLD & Font.ITALIC, 32), 17, 17 + "flying ponies".length());
text.addAttribute(TextAttribute.FOREGROUND, Color.BLUE, 17, 17 + …Run Code Online (Sandbox Code Playgroud) 综述:
请参阅Andreas的知识渊博的评论!
==========================================
如下面的代码所示,TForm7是MDIForm形式,TForm8是MDIChild形式.TForm8包含一个alClient对齐的面板,它还包含一个TPaintBox.如果TForm8的面板的ParentBackground设置为False,我无法从TForm7触发TForm8的paintbox的绘制事件.我想知道为什么会发生这种情况,如何在不明确引用它的情况下触发TForm8的paintbox绘制事件.任何建议表示赞赏!
注意:如果我调用Self.Repaintwithint TForm8,例如在其Click事件中,可以触发TForm8的paintbox的paint事件.只有当我form8.repaint在TForm8外面打电话时才能触发它.我想知道为什么会这样?
可能相关的SO页面:
如何在模态表单处于活动状态时重新绘制父表单?
包含MDIForm表单的单元.
unit Unit7;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm7 = class(TForm)
procedure FormShow(Sender: TObject);
procedure FormClick(Sender: TObject);
end;
var
Form7: TForm7;
implementation
{$R *.dfm}
uses
Unit8;
procedure TForm7.FormShow(Sender: TObject);
begin
TForm8.Create(Self);
end;
procedure TForm7.FormClick(Sender: TObject);
begin
TForm8(ActiveMDIChild).Repaint;
end;
end.
Run Code Online (Sandbox Code Playgroud)
上述单位的Dfm.
object Form7: TForm7
Left = 0
Top = 0
Caption = 'Form7'
ClientHeight = 379
ClientWidth = 750
Color = clBtnFace …Run Code Online (Sandbox Code Playgroud) 如下面的代码示例所示,person_list是一个用户派生类型,包含一个类型绑定过程compare_persons.我希望compare_persons能够接受某个组compareFunc作为其参数之一,因此声明接口部分compareFunc.注意第一个参数compareFunc是类型person_list.(正如您所说,person_list就像TStringList在Delphi的VCL中一样.)
编译无法进行.错误消息是:
[root@localhost new]# ifort -c lib1.f90
lib1.f90(26): error #6457: This derived type name has not been declared. [PERSON_LIST]
TYPE(person_list) :: persons ! error #6457: This derived type name has not been declared. [PERSON_LIST]
-------------------------^
lib1.f90(23): error #6404: This name does not have a type, and must have an explicit type. [PERSONS]
FUNCTION compareFunc(persons, index1, index2) ! error #6404: This name does …Run Code Online (Sandbox Code Playgroud) 我正在尝试TDropFileTargetMelander DragDrop套件的组件.目标是在拖放文件后执行某些任务.此外,如果在处理过程中出现问题,我希望收到例外情况.
似乎OnDrop吞下了事件处理程序中引发的异常.但是,即使我将raise语句放入组件的源代码中,我仍然无法收到异常.你能帮忙发表评论吗?
object Form4: TForm4
Left = 0
Top = 0
Caption = 'Form4'
ClientHeight = 270
ClientWidth = 392
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object edt1: TEdit
Left = 56
Top = 72
Width = 257
Height = 21
TabOrder = 0
Text = 'edt1'
end
object dropfiletarget2: …Run Code Online (Sandbox Code Playgroud) 使用TStringStream时,bytes使用其Bytes属性与bytes提取的使用不同TStream.Read.如下所示:
bytes使用所提取的TStream.Read表示正确的数据.bytes使用它的Bytes属性包含更多的数据.bytes不同bytes)你能帮忙评论可能的原因吗?非常感谢您的帮助!
PS:Delphi XE,Windows 7.(似乎在Delphi 7中的TStringStream没有LoadFromFile或SaveToFile.)
PS:可以从SkyDrive下载示例文件:REF_EncodedSample和REF_DecodedSample.(Zlib压缩的图像文件.).
procedure CompareBytes_2;
var
ss_1: TStringStream;
ss_2: TStringStream;
sbytes_Read: TBytes;
sbytes_Property: TBytes;
len_sbytes_Read: Integer;
len_sbytes_Property: Integer;
filename: string;
begin
filename := 'REF_EncodedSample'; // textual data
// filename := 'REF_DecodedSample'; // non-textual data
ss_1 := TStringStream.Create;
ss_1.LoadFromFile(filename);
ss_2 := TStringStream.Create;
ss_2.LoadFromFile(filename);
ss_1.SaveToFile(filename+ '_CopyByStringStream_1');
ss_2.SaveToFile(filename+ '_CopyByStringStream_2');
len_sbytes_Read := ss_1.Size;
SetLength(sbytes_Read, len_sbytes_Read); …Run Code Online (Sandbox Code Playgroud) 以下语句在Delphi XE(Windows 7 x64)下正常运行,但在Delphi XE3(Windows 7 x64)下提供了"Exception class EFOpenError with message ...".它看起来像一个bug.如果是这样,有解决方法吗?
TFileStream.Create(
'C:\Test.txt'
, fmOpenRead,
// FILE_SHARE_READ);
// FILE_SHARE_WRITE);
FILE_SHARE_READ or FILE_SHARE_WRITE);
Run Code Online (Sandbox Code Playgroud) 下面的控制台应用程序给出了"运行时错误"......
为什么会这样?非常感谢 !
PS:相关SO帖子
program Project2;
{$APPTYPE CONSOLE}
type
TParent = class;
TParentClass = class of TParent;
TParent = class
public
procedure Work; virtual; abstract;
end;
TChild1 = class(TParent)
public
procedure Work; override;
end;
TChild2 = class(TParent)
public
procedure Work; override;
end;
procedure TChild1.Work;
begin
WriteLn('Child1 Work');
end;
procedure TChild2.Work;
begin
WriteLn('Child2 Work');
end;
procedure Test(ImplClass: TParentClass);
var
ImplInstance: TParent;
begin
ImplInstance := ImplClass.Create;
ImplInstance.Work;
ImplInstance.Free;
end;
begin
Test(TParent);
Test(TChild1);
Test(TChild2);
Readln;
end.
Run Code Online (Sandbox Code Playgroud) 下面显示的两个程序尝试使用此处描述的技术测试对象是否被释放,对已释放的对象的错误引用.
如果在Delphi 7下编译,下面显示的第一个程序运行正确,但如果在Delphi XE和upper下编译,则错误地运行.也就是说,它输出
D7 DXE
True True
True True
True False
True True
False True
False False
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
function ValidateObj(Obj: TObject): Pointer;
// see { Virtual method table entries } in System.pas
begin
Result := Obj;
if Assigned(Result) then
try
if Pointer(PPointer(Obj)^) <> Pointer(Pointer(Cardinal(PPointer(Obj)^) + Cardinal(vmtSelfPtr))^) then
// object not valid anymore
Result := nil;
except
Result := nil;
end;
end;
function ValidateObj2(Obj: TObject): Pointer;
type
PPVmt = ^PVmt;
PVmt = ^TVmt; …Run Code Online (Sandbox Code Playgroud) 在这篇SO文章中,建议将IAutoComplete与TStringsAdapter一起使用来实现自动完成。以下代码尝试遵循建议,但无法启用自动完成功能,而无需编译和运行时异常,抱怨接口不匹配/不一致。您能帮忙评论一下根本原因和解决方法吗?
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, AxCtrls, StdVCL, ActiveX, ComObj;
const
IID_IAutoComplete = '{00bb2762-6a77-11d0-a535-00c04fd7d062}';
IID_IAutoComplete2 = '{EAC04BC0-3791-11d2-BB95-0060977B464C}';
CLSID_AutoComplete: TGUID = '{00BB2763-6A77-11D0-A535-00C04FD7D062}';
type
IAutoComplete = interface(IUnknown)
[IID_IAutoComplete]
function Init(hwndEdit: HWND; punkACL: IUnknown; pwszRegKeyPath: PWideChar;
pwszQuickComplete: PWideChar): HResult; stdcall;
function Enable(fEnable: Boolean): HResult; stdcall;
end;
IAutoComplete2 = interface(IAutoComplete)
[IID_IAutoComplete2]
function SetOptions(dwFlag: DWORD): HResult; stdcall;
function GetOptions(out dwFlag: DWORD): HResult; stdcall;
end;
TStringsAdapterCracker = class(TStringsAdapter);
TForm1 = class(TForm)
ComboBox1: TComboBox; …Run Code Online (Sandbox Code Playgroud) RoundCube为下面的纯文本电子邮件提供了一个很好的配色方案.
我想知道RoundCube如何做到这一点,以及如何在Delphi中实现这个配色方案?
>>>> Peter says:
>>>> Peter says:
>>> Jane says:
>>> Jane says:
>> Peter says:
>> Peter says:
> Jane says:
> Jane says:
Peter says:
Peter says:
Run Code Online (Sandbox Code Playgroud)

delphi ×9
interface ×2
autocomplete ×1
awt ×1
byte ×1
class ×1
com ×1
delphi-7 ×1
delphi-xe ×1
delphi-xe3 ×1
exception ×1
fastmm ×1
fortran ×1
inheritance ×1
java ×1
mdichild ×1
module ×1
polymorphism ×1
reference ×1
repaint ×1
roundcube ×1
stringstream ×1
swing ×1
tfilestream ×1
types ×1
winapi ×1