【发布时间】:2013-02-18 16:35:13
【问题描述】:
我需要在当前焦点/活动表单上选择组件,但无法完全找到这样做的方法。 所以,我有一个包含所有常用程序的 delphi 单元,所以我从其他形式调用它们。 现在,问题是,使用其中一个程序我称之为组件显示动作,但是,由于组件被放置在每个表单上(找不到放置一个组件的方法,它可以被所有表单使用,但是我认为这在某种程度上无法做到。我错了吗?),我需要在当前活动的表单上为组件调用一个显示事件,否则我调用组件的指定表单会变得活跃和聚焦。
到目前为止,我已经尝试通过 Screen.ActiveForm 和 Screen.ActiveForm.Name 获取当前表单名称,但这些都不起作用,因为编译器无法编译,因为组件父级未定义(出现错误“' TForm' 不包含名为 'KeyBoard1' 的成员“...)
这是程序代码:
procedure KeyDownEvents(var Key: Word; Shift: TShiftState);
begin
CurrentForm:=Screen.ActiveForm.Name;
if Key = VK_F9 then CurrentForm.KeyBoard1.Show;
end;
使用全局变量
var CurrentForm: TForm;
因为我已经尝试了 10 种不同的组合,所以我在哪里以及缺少什么......?
谢谢,
马克。
Ps:如上所述,有没有办法放置或调用组件,在任何表单上都处于活动状态,或者应该将它放置在每个表单中,这样就不会改变焦点......?我知道在 MDI 中我可以使用主要形式的组件(据我所知......),但我在 SDI 中工作,因为需要多个监视器......
David 的完整代码:
unit CommonProcedures;
interface
uses Classes, Dialogs, StdCtrls, Math, Winapi.Windows, Winapi.Messages,
Vcl.Controls, Vcl.Forms, AdvTouchKeyboard;
procedure KeyDownEvents(var Key: Word; Shift: TShiftState);
var
CurrentForm: TComponentName;
KeyBoard1Visible: Boolean;
implementation
uses Startup, Main, Controller, Settings, Patch, Output, StageVisual, FixturesEditor,
FixturesEditorGlobal;
procedure KeyDownEvents(var Key: Word; Shift: TShiftState);
begin
CurrentForm:=Screen.ActiveForm.Name;
if ((ssAlt in Shift) and (Key = VK_F4)) then Key:=0;
if Key = VK_F1 then Main1.Show;
if Key = VK_F2 then Controller1.Show;
if Key = VK_F3 then Settings1.Show;
if Key = VK_F4 then Patch1.Show;
if Key = VK_F5 then Output1.Show;
if Key = VK_F6 then StageVisual1.Show;
if Key = VK_F7 then FixturesEditor1.Show;
if Key = VK_F8 then FixturesEditor2.Show;
if Key = VK_F9 then
begin
if KeyBoard1Visible=False
then
begin
KeyBoard1Visible:=True;
CurrentForm.KeyBoard1.Show;
end
else
begin
KeyBoard1Visible:=False;
CurrentForm.KeyBoard1.Hide;
end;
end;
end;
结束。
你可以看到,我只省略了所有其他键事件,因为它们完全不重要,但我也省略了检查键盘是显示还是隐藏的过程,因此 VK_F9 的行为类似于切换键.
这是本单元迄今为止唯一的程序,因为我几天前才开始创建程序,所以它还在一垒,所以说... 是的,正如你所见和猜测的那样,它是一种灯光秀控制器程序,是我大四的项目。
Ps:当我将问题编辑得更清楚时,看不到拒绝投票的理由......
【问题讨论】:
-
我很失望你在这里发布了假代码。请永远不要那样做。始终发布真实代码。我也不知道PS是什么意思。请尝试一次只问一个问题。
-
那是真实的,不是假的代码,但它是真实的,它只是它的一部分。我确实使用 Keyboard1 而不是 AdvPopupTouchKeyBoard1,因为它很长,对我来说保持这种状态毫无意义......将在整个过程代码下方作为答案发布,所以你会看到我遗漏了什么以及为什么...没有难过的感觉,但我不想隐瞒任何事情......
-
这不是真正的代码。例如,这不是事件处理程序的样子。它看起来像
procedure TMyFormClass.EventName。而且,CurrentForm := Screen.ActiveForm.Name是类型不匹配。发布您正在使用的确切代码很重要。即使这意味着调整它以适合某个问题。 -
只有有答案才能回答。更新原始问题很好,实际上值得鼓励。
-
给您带来的不便,我深表歉意..
标签: delphi components procedures