在Delphi中,考虑一下
var
i: integer;
begin
for i := 0 to N do
begin
{ Code }
end;
Run Code Online (Sandbox Code Playgroud)
有人可能会认为,i = N在后for循环,但确实在Delphi编译器保证这一点?在Delphi if循环之后,可以假设循环变量等于循环内的最后一个值吗?
更新
在尝试了几个简单的循环之后,我怀疑它i实际上等于一个加上i循环后循环内部的最后一个值 ...但是你能依靠这个吗?
因此,Ruby 有时候使用大括号而不是要求它们的宽松容忍度导致了很多混乱,因为我正在尝试学习Rails以及何时/何地使用它们以及为什么?
有时参数或值会像(@user, @comment)它们看起来那样传递,而其他时候传递[ :user => comment ]的只是::action => 'edit'
我在谈论[] vs () vs {}的我们
规则是什么?你有什么技巧可以帮助你记住吗?
我甚至不确定这是否适合一个问题,但这只是一个问题.我有一个用Delphi XE编写的互联网广播播放器,使用BASS库进行音频流和播放.该应用程序需要在Windows XP,Vista和7下运行.
Bass可以轻松控制全局音量,但无法使声音静音,一般来说,根据每个应用程序控制音量更好.
Bass还可以轻松控制"通道"(流)的音量,但同样没有静音,这也不是适合每个应用程序的控制.(Windows混音器中的应用程序音量控制不受影响.)
据我所知,对于Vista及以上版本,我需要ISimpleAudioVolume和/或IAudioEndpointVolume,但找不到这些的Delphi实现.所以问题的一部分是,它作为第三方库存在吗?
第二部分是什么是在XP上控制音量和切换静音(系统范围或每个应用程序)的正确方法,这些接口不可用?
请原谅我略带幽默的头衔.我在其中使用了两个不同的"安全"一词(显然).
我对线程很陌生(好吧,我已经使用了多年的线程,但只有非常简单的形式).现在我面临着编写某些算法的parallal实现的挑战,并且线程需要处理相同的数据.考虑以下新手错误:
const
N = 2;
var
value: integer = 0;
function ThreadFunc(Parameter: Pointer): integer;
var
i: Integer;
begin
for i := 1 to 10000000 do
inc(value);
result := 0;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
threads: array[0..N - 1] of THandle;
i: Integer;
dummy: cardinal;
begin
for i := 0 to N - 1 do
threads[i] := BeginThread(nil, 0, @ThreadFunc, nil, 0, dummy);
if WaitForMultipleObjects(N, @threads[0], true, INFINITE) = WAIT_FAILED then
RaiseLastOSError;
ShowMessage(IntToStr(value));
end;
Run Code Online (Sandbox Code Playgroud)
初学者可能希望上面的代码显示消息20000000.实际上,首先value是等于0 …
delphi multithreading thread-safety critical-section interlocked-increment
我使用_popen启动进程来运行命令并收集输出
这是我的c ++代码:
bool exec(string &cmd, string &result)
{
result = "";
FILE* pipe = _popen(cmd.c_str(), "rt");
if (!pipe)
return(false);
char buffer[128];
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
_pclose(pipe);
return(true);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有控制台窗口打开的情况下这样做(就像目前在_popen语句中那样)?
我正在研究在我的应用程序的某些部分用 Direct2D 替换 GDI。
为此,我阅读了 Embarcadero 官方文档并创建了这个最小的 Direct2D 应用程序:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Direct2D, D2D1;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
FCanvas: TDirect2DCanvas;
protected
procedure CreateWnd; override;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
procedure WMSize(var Message: TWMSize); message WM_SIZE;
procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
public
destructor Destroy; override;
property Canvas: TDirect2DCanvas read FCanvas;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CreateWnd;
begin …Run Code Online (Sandbox Code Playgroud) case在Delphi语句中使用类型常量的最优雅(或最不丑陋)的方法是什么?
也就是说,假设您需要声明一个类型化的常量
const
MY_CONST: cardinal = $12345678;
...
Run Code Online (Sandbox Code Playgroud)
那么Delphi编译器就不会接受
case MyExpression of
MY_CONST: { Do Something };
...
end;
Run Code Online (Sandbox Code Playgroud)
但你需要写
case MyExpression of
$12345678: { Do Something };
...
end;
Run Code Online (Sandbox Code Playgroud)
这容易出错,难以更新,也不优雅.
是否有任何技巧可以使编译器插入常量的值(最好通过检查const源代码中的常量值,但可能通过在运行时查找值)?我们假设您不会在运行时更改"常量"的值.
我正在用Delphi编写一个屏幕保护程序.我想要在每个显示器上全屏显示TpresentationFrm.为此,我写了以下(不完整)程序:
program ScrTemplate;
uses
...
{$R *.res}
type
TScreenSaverMode = (ssmConfig, ssmDisplay, ssmPreview, ssmPassword);
function GetScreenSaverMode: TScreenSaverMode;
begin
// Some non-interesting code
end;
var
i: integer;
presentationForms: array of TpresentationFrm;
begin
Application.Initialize;
Application.MainFormOnTaskbar := True;
case GetScreenSaverMode of
ssmConfig:
Application.CreateForm(TconfigFrm, configFrm);
ssmDisplay:
begin
SetLength(presentationForms, Screen.MonitorCount);
for i := 0 to high(presentationForms) do
begin
Application.CreateForm(TpresentationFrm, presentationForms[i]);
presentationForms[i].BoundsRect := Screen.Monitors[i].BoundsRect;
presentationForms[i].Visible := true;
end;
end
else
ShowMessage(GetEnumName(TypeInfo(TScreenSaverMode), integer(GetScreenSaverMode)));
end;
Application.Run;
end.
Run Code Online (Sandbox Code Playgroud)
当ssmDisplay被执行的代码,两种形式确实是创造了(是的,我有整整两个显示器).但它们都出现在第一个监视器上(索引0,但不是主监视器).
单步执行代码时,我发现它Screen.Monitors[i].BoundsRect是正确的,但由于某种原因,表单获得了不正确的边界:
Watch Name Value (TRect: Left, …Run Code Online (Sandbox Code Playgroud) 当我设置TFileOpenDialog的Filename属性然后执行它时,我只看到在Filename字段中选择的文件名的最后14个字符.如果我按下Home我可以看到整个字符串都在那里,但我希望它能正确显示.我已经在两台Windows 7和一台Windows 8机器上进行了测试,结果相同.有没有人对如何解决这个问题有任何建议或提示?
FileOpenDialog1.FileName :=
'C:\Program Files (x86)\Embarcadero\RAD Studio\9.0\available_downloads_en.htm';
if FileOpenDialog1.Execute then
ShowMessage(FileOpenDialog1.FileName);
Run Code Online (Sandbox Code Playgroud)
显示问题的初始显示:

按Home后的整个字符串:

我有一个函数,它生成一个数组作为结果,并希望迭代其结果来执行一些操作:
for MyElm in AFunction(SomeVar) do
begin
//...
end;
Run Code Online (Sandbox Code Playgroud)
因为集合部分是函数的结果,所以确保它只执行一次是很重要的.
问:每次迭代都会执行AFunction吗?
delphi ×8
audio ×1
c++ ×1
curly-braces ×1
delphi-xe2 ×1
direct2d ×1
dpi ×1
for-loop ×1
jrubyonrails ×1
parentheses ×1
popen ×1
ruby ×1
screensaver ×1
shell ×1
variables ×1
volume ×1
windows ×1