这可能是一个简单的问题,但我想知道如何确保调用类的构造函数.
如果我有以下代码:
type TMyObject = class(TObject)
public
constructor Create;override;
end;
implementation
constructor TMyObject.Create;override;
begin
inherited;
//do other instantiation
end;
Run Code Online (Sandbox Code Playgroud)
Delphi不允许这样 - '无法覆盖静态方法'.
我想这样做的是确保对象是使用创建了创建自定义构造函数和禁止调用祖先创建构造函数.
我目前解决这个问题的方法是定义一个唯一签名的Create构造函数,如下所示:
constructor Create(aName : String);overload;
Run Code Online (Sandbox Code Playgroud)
但程序员可能会调用祖先的Create()方法.
我有一个字符串,我需要在绘制时计算Rect大小(文本高度).我的实现使用DrawTextW()
带有DT_WORDBREAK or DT_CALCRECT
标志的函数.
我的字符串的一个例子:
thisisaverylonglonglonglineoftextthatneedstofitinsideagivenrectwidth
Run Code Online (Sandbox Code Playgroud)
我可以在MSDN文档中看到,该DrawTextW()
方法指出:
如果最大的单词比矩形宽,则扩展宽度.如果文本小于矩形的宽度,则宽度减小.如果只有一行文本,DrawText会修改矩形的右侧,以便它限定行中的最后一个字符.
但是在MSDN文档中,DrawTextExW()
方法没有说明这一点.
所以我尝试使用该DrawTextExW()
方法计算高度,但结果与DrawTextW()
函数相同,其中它扩展了rect的宽度以适合最大的文本行.
那么,当绘制一个大字符串(没有空格)在哪里DT_WORDBREAK
和DT_CALCRECT
指定时,如何正确计算文本rect的高度(给定(固定)宽度?)
编辑:
作为旁注,有谁知道Microsoft Excel如何进行单元格文本绘制?是否有对此文本绘图的API调用?这是我原来的问题源于此,但在Excel中实现的方式是在任何字符(而不仅仅是空格)上绘制文本和wordbreak/wordwrap.
嗨,我有一个内部有几个框架的表格.
对于某些帧,我希望滚动内容(或至少处理鼠标轮事件).
我尝试过以下方法:
只需为每个帧分配一个OnMouseWheel事件处理程序
覆盖父窗体的MouseWheel事件:
procedure TFmReview.MouseWheelHandler(var Message: TMessage);
var Control: TControl;
begin
Control := ControlAtPos(ScreenToClient(SmallPointToPoint(TWMMouseWheel(Message).Pos)), False, True);
if Assigned(Control) and (Control <> ActiveControl) then
begin
ShowMessage(Control.Name);
Message.Result := Control.Perform(CM_MOUSEWHEEL, Message.WParam, Message.LParam);
if Message.Result = 0 then
Control.DefaultHandler(Message);
end else inherited MouseWheelHandler(Message);
end;
Run Code Online (Sandbox Code Playgroud)
不幸的是,两者似乎都没有用.
因此,简单地说,如何将鼠标滚轮事件指向鼠标光标所在的最顶层控件(无论光标位于哪个帧/父/窗体等)?
不确定这在SO上是否可能有效,但我希望有人可以建议使用正确的算法.
我有以下RAW数据.
在图像中,您可以看到"步骤".基本上我希望得到这些步骤,但随后获得所有数据的移动平均值.在下图中,您可以看到移动平均线:
但是你会注意到,在"步"处,移动平均线减小了我想保持高垂直梯度的梯度.
是否有任何平滑技术会考虑大的垂直"偏移",但平滑其他数据?
我想是一个非常简单的,但我需要能够将表单最大化到特定的屏幕.似乎没有找到任何特定于Delphi的信息.
我可以记住后续应用程序加载时的表单位置.但是,当我恢复位置,然后调用时WindowState := wsMaximized
,表单移动到另一个屏幕!(我确实在屏幕上也可以看到其他形式 - 它似乎最大化到'活动屏幕')
所以我需要一个像这样的函数:
procedure Maximize(const aScreenIndex : Integer);
begin
if aScreenIndex < Screen.MonitorCount then
//Maximize to that screen
end;
Run Code Online (Sandbox Code Playgroud) 只是想知道是否可以将(任意)变量的内容复制到剪贴板?
也就是说,在调试过程中,我可以将鼠标悬停在变量上,它会显示值.我希望将此值复制到剪贴板.
假设我有以下代码:
function DoSomething:Boolean;
var obj : TMyObject;
i : Integer;
begin
Result := False; //We haven't executed GetValue() correctly yet
obj := TMyObject.Create();
try
//perform some code that may produce an exception
i := obj.GetValue();
//Set the return to True as we executed GetValue() successfully
Result := True;
finally
//do some cleanup
obj.Free;
end;
end;
Run Code Online (Sandbox Code Playgroud)
Delphi编译器抱怨分配给Result的值从未在第一行中使用.
我可能错过了一些明显的东西,但我不明白为什么编译器会优化它(如果优化已经开启).
我一直被教导要明确设置我的变量,以免混淆他们的价值观.最重要的是,如果GetValue()
函数生成异常,该Result := True;
行将永远不会执行.因此,无论Delphi初始化变量是什么,我们都会受到怜悯.
这是安全/可接受的代码吗?我应该简单地删除方法的第一行,这会使它更难阅读吗?如果失败,我将不得不关闭特定的编译器警告,但我不愿意这样做,因为此警告消息可以提供有用的信息.
我试图使用TFilestream写入网络共享(本地).如果网络连接没有中断,一切正常.
但是,如果我拉网线然后重新连接,则由于访问限制而导致打开文件流的后续尝试失败.我甚至无法删除资源管理器中的文件!似乎TFilestream锁定文件,解决这个问题的唯一方法是重启.
在我的应用程序中,我在写入文件的整个过程中保持文件打开(这是每秒写一次的日志文件).
我失败的代码如下:
procedure TFileLogger.SetLogFilename(const Value: String);
var line : String;
Created : Boolean;
begin
if not DirectoryExists(ExtractFilePath(Value)) then //create the dir if it doesnt exist
begin
try
ForceDirectories(ExtractFilePath(Value));
except
ErrorMessage(Value); //dont have access to the dir so flag an error
Exit;
end;
end;
if Value <> FLogFilename then //Either create or open existing
begin
Created := False;
if Assigned(FStream) then
FreeandNil(FStream);
if not FileExists(Value) then //create the file and write header
begin
//now create a new file
try …
Run Code Online (Sandbox Code Playgroud) 我有以下接口/类:
public interface IUnitOfWork : IDisposable
{
event EventHandler<EventArgs> Saved;
DbSet<T> Set<T>() where T : class;
DbEntityEntry<T> Entry<T>(T entity) where T : class;
void Commit();
}
Run Code Online (Sandbox Code Playgroud)
以及存储库的实现:
public class CachedSqlRepository<T, TKey, TContext> : ICacheRepository<T, TKey, TContext>
where T : class
where TContext : DbContext, IDisposable, new()
{
//A list of the Navigation Properties to include
private readonly Expression<Func<T, object>>[] _NavigationProperties;
public CachedSqlRepository(params Expression<Func<T, object>>[] navigationProperties)
{
_NavigationProperties = navigationProperties;
using (TContext dbContext = new TContext()) //Fetch the List of Entities …
Run Code Online (Sandbox Code Playgroud) 好的,所以我了解如何使用CancellationTokenSource
. 在我看来,“Task
种类”类型会自动处理此异常 - 它将Task
's 设置Status
为 Cancelled。
现在您实际上仍然需要处理OperationCancelledException
. 否则异常冒泡到Application.UnhandledException
. Task 本身可以识别它并在内部进行一些处理,但您仍然需要将调用代码包装在 try 块中以避免未处理的异常。有时,这似乎是不必要的代码。如果用户按下取消,则取消Task
(显然任务本身也需要处理它)。我觉得不需要任何其他代码要求。只需检查Status
任务完成状态的属性。
从语言设计的角度来看,这有什么具体原因吗?有没有其他方法可以将Status
属性设置为取消?
.net c# task-parallel-library cancellation cancellationtokensource
delphi ×7
c# ×2
.net ×1
algorithm ×1
cancellation ×1
class ×1
constructor ×1
debugging ×1
delphi-7 ×1
drawtext ×1
file-locking ×1
filestream ×1
filter ×1
focus ×1
forms ×1
logging ×1
mousewheel ×1
smoothing ×1
tform ×1
tframe ×1
winapi ×1