如何将对象作为参数传递给 powerbuilder 中的函数

发布于 2024-09-30 17:23:14 字数 425 浏览 0 评论 0原文

我正在使用 powerbuilder 9 .0.1 Build 7171 我想写一个通用的 函数来处理对象属性,如字体名称。我想改变字体 对象按钮,列表框的想法是我将传递 窗口名称或窗口句柄和函数将获取控制列表,如果 control.type 是列表框或按钮或图片按钮,它会更改其字体。

我不想为每个窗口都编写窗口函数,有吗 我可以操纵 Windows 的属性

FOR n = 1 到 UpperBound(Control[]) IF Control[n].TypeOf() = CommandButton!或 Control[n].TypeOf() = PictureButton!THEN set_button_name(this.Control[n]) 结束如果 下一步

在这种情况下,我必须在每个窗口的打开事件中硬编码此代码 使用 function() 来调用它

I am using powerbuilder 9 .0.1 Build 7171 I want to write a generalised
function to handle object properties like font name. I want to change font
of the object buttons, list box idea is I will just pass on
window name or window handle and function will get the control list and if
control.type is listbox or button or picture button it will change its font.

I don't want to write windows function for every window is there any
way where I can just manipulate properties of windows

FOR n = 1 to UpperBound(Control[])
IF Control[n].TypeOf() = CommandButton! OR Control[n].TypeOf() = PictureButton!THEN
set_button_name(this.Control[n])
End if
NEXT

In this case I have to hardcode this code in open event of every window
to call this by using function()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ记忆︶ㄣ 2024-10-07 17:23:14

因此,您确实遇到了两个问题:

  1. 如何获取控件列表(您不会问,但根据代码片段,您会这样做)
  2. 如何从 WindowObject 开始并更改与后代关联的属性

控件列表

您已经获得了对窗口的 Control 数组的隐式引用,这是一个很好的第一步,但可能会忽略选项卡和用户对象等对象中的嵌入式 Control 数组。像下面这样的函数(警告:未经测试,仅用于说明)将递归所有控件,并处理选项卡中的选项卡等情况。

function of_getcontrols (windowobject awo_input[], ref windowobject awo_output[]) returns integer

long ll_Input, ll_InputCount, ll_OutputCount, ll_Sub, ll_SubCount
window lw_Control
userobject luo_UserObject
tab ltb_Tab
windowobject lwo_Empty[], lwo_Sub[]

awo_Output = lwo_Empty

ll_InputCount = UpperBound (awo_Input)
FOR ll_Input = 1 TO ll_InputCount
    ll_OutputCount ++
    awo_Output[ll_OutputCount] = awo_Input[ll_Input]

    // look for nested control arrays
    CHOOSE CASE awo_Input[ll_Input].TypeOf()
        CASE UserObject!
            luo_UserObject = awo_Input[ll_Input]
            ll_SubCount = of_GetControls (luo_UserObject.Control, lwo_Sub)
            FOR ll_Sub = 1 TO ll_SubCount
                ll_OutputCount ++
                awo_Output[ll_OutputCount] = lwo_Sub[ll_Sub]
            NEXT

        CASE Tab!
            ltb_Tab = awo_Input[ll_Input]
            ll_SubCount = of_GetControls (ltb_Tab.Control, lwo_Sub)
            FOR ll_Sub = 1 TO ll_SubCount
                ll_OutputCount ++
                awo_Output[ll_OutputCount] = lwo_Sub[ll_Sub]
            NEXT
    END CHOOSE
NEXT

RETURN ll_OutputCount

类型转换

下一个问题是如何访问或操作对象的属性,其中编译器无法识别该属性对该对象类型的所有权。解决方案是转换为编译器能够识别属性关联的类型。您在上面的示例中看到了转换:

userobject luo_UserObject
...
luo_UserObject = awo_Input[ll_Input]
...luo_UserObject.Control...

编译器无法识别 WindowObject 和我试图引用的 Control 数组之间的关联,因此我通过将 WindowObject 分配给 UserObject 类型的变量来将 WindowObject 转换为 UserObject。之后,我可以通过该变量引用 Control 数组。

您将执行与我类似的操作,可能在循环内执行 CHOOSE CASE TypeOf(),然后在每个 CASE 内转换为与类型匹配的特定变量并在那里操作属性。

我猜您希望有一个单行赋值选项,但 PowerScript 中没有。 (如果您在每个函数上调用该函数,则可以使用 DYNAMIC 关键字在 WindowObject 上调用该函数,但您有责任确保该函数在调用之前就存在。)我相信 有一种方法可以直接使用 PBNI 来做到这一点,但我无法帮助你,而且我强烈怀疑它的努力远远超过了它的价值。

祝你好运,

特里。

So, you've really got two issues:

  1. How to get the list of controls (you don't ask, but based on the code snippet, you do)
  2. How to start with a WindowObject and change an attribute that is associated with a descendant

List of Controls

You've got an implicit reference to the window's Control array, which is a good first step, but may neglect embedded Control arrays in objects like tabs and userobjects. A function like the following (warning: untested, for illustration only) will recurse through all your controls, and handle cases like tabs within tabs.

function of_getcontrols (windowobject awo_input[], ref windowobject awo_output[]) returns integer

long ll_Input, ll_InputCount, ll_OutputCount, ll_Sub, ll_SubCount
window lw_Control
userobject luo_UserObject
tab ltb_Tab
windowobject lwo_Empty[], lwo_Sub[]

awo_Output = lwo_Empty

ll_InputCount = UpperBound (awo_Input)
FOR ll_Input = 1 TO ll_InputCount
    ll_OutputCount ++
    awo_Output[ll_OutputCount] = awo_Input[ll_Input]

    // look for nested control arrays
    CHOOSE CASE awo_Input[ll_Input].TypeOf()
        CASE UserObject!
            luo_UserObject = awo_Input[ll_Input]
            ll_SubCount = of_GetControls (luo_UserObject.Control, lwo_Sub)
            FOR ll_Sub = 1 TO ll_SubCount
                ll_OutputCount ++
                awo_Output[ll_OutputCount] = lwo_Sub[ll_Sub]
            NEXT

        CASE Tab!
            ltb_Tab = awo_Input[ll_Input]
            ll_SubCount = of_GetControls (ltb_Tab.Control, lwo_Sub)
            FOR ll_Sub = 1 TO ll_SubCount
                ll_OutputCount ++
                awo_Output[ll_OutputCount] = lwo_Sub[ll_Sub]
            NEXT
    END CHOOSE
NEXT

RETURN ll_OutputCount

Casting

The next issue is how to access or manipulate an attribute of an object where the compiler doesn't recognize ownership of that attribute to that object's type. The solution is casting to a type where the compiler will recognize the attribute association. You saw casting in the sample above:

userobject luo_UserObject
...
luo_UserObject = awo_Input[ll_Input]
...luo_UserObject.Control...

The compiler wouldn't have recognized the association between the WindowObject and the Control array I was trying to reference, so I cast the WindowObject to a UserObject by assigning the WindowObject to a variable of type UserObject. After that, I can reference the Control array via that variable.

You'll be doing something similar to mine, probably inside a loop doing a CHOOSE CASE TypeOf(), then inside each CASE casting to a specific variable matching the type and manipulating the attribute there.

I'm guessing you were hoping there was a one-line assignment option, but there's not in PowerScript. (If it was a function you were calling on each, you could use the DYNAMIC keyword calling the function on the WindowObject, but it would be your responsibility to ensure that function was there before you called it.) I believe there's a way to directly do this with PBNI, but I can't help you with that, and I strongly suspect it's far more effort than it's worth.

Good luck,

Terry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文