Forum Discussion

nishay_patel's avatar
nishay_patel
Occasional Contributor
11 years ago

Win32API Alternative for Script Extensions

Follow-up questions for : http://smartbear.com/forums/f81/t91301/modal-user-forms-to-be-on-top-of-other-windows/



I need to be able to use Win32API.SetWindowPos() function within a script extension.  I know Win32API is not available to script extensions, so is there some alternative I can use?  Even coding a function from scratch would be acceptable.  I am writing the script extensions in JS.



Thanks

Nish
  • AlexKaras's avatar
    AlexKaras
    Champion Level 3
    Hi Nish,



    Considering, that neither Win32API nor DLL objects are accessible from script extensions, I think that you may consider to create a simple .net assembly with the function that will call SetWindowPos() and call this .net function using the dotNET object that is accessible from script extensions.

    Sounds not that aesthetically beautiful but might appear to be an acceptable solution...



    P.S. Another idea came to my mind is to use the RunDLL32 utility (e.g. http://support.microsoft.com/kb/164787) called via shell.execute or TestedApps (if they are accessible from script extensions) and command it to call the SetWindowPos() function.
  • nishay_patel's avatar
    nishay_patel
    Occasional Contributor
    Oh, I should clarify, I am using SetWindowPos() to set the form as the topmost window on the screen.



    Win32API.SetWindowPos(form.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Though I have not tried this I would think the .Position (same as WIN32API call) method would work within your extension.
  • nishay_patel's avatar
    nishay_patel
    Occasional Contributor
    Thanks for the reply, Ryan.



    Let me clarify a little further.  I only need the SetWindowPos function to make a window the topmost window on the screen, not move the window around the screen.



    Specifically, I need the constant  "HWND_TOPMOST".



    Otherwise, the window I am trying to show gets hidden behind other windows, like the browser.  See my first post, and the link to my previous problem that led to this one.  The "form" i refer to in my code is actually a modal form in TestComplete.



    The answer that Alex left in the post in the link above was spot on, but since Win32API is unavailable for script extensions, I need another method that will bring the form window to the top most level.
  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Understood. Likewise you can use the .TopMost property of the form the .Position was meant as an example of calling methods/properties used in TC from the extension.



    So try replacing the .Position call in my extension with .TopMost = true;
  • nishay_patel's avatar
    nishay_patel
    Occasional Contributor
    TopMost is not a property of the Window class in TC



    Here is a list of all Window object properties



    The problem is, when you show a modal form, test execution is paused.  So the properties of the window must be edited before the call to ShowModal is made.



    So ideally, it would be

    var form = Sys.Process("Test*te").Window("TUserForm", UserForms.UserForm1.Caption, 1);

    [edit window properties here]

    UserForms.UserForm1.ShowModal();



    And it seems like Win32API is the only thing capable of doing this.



    Also, the UserForms object itself would also be incapable of this, since it wouldn't have access to all of your operating system's windows.
  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    Well you are correct in saying that the window class does not support that method, and you cannot call the Win32API support from within an extension nor does JScript allow access to Win32 API. So without some wonky work around I don't believe this is possible.
  • Ryan_Moran's avatar
    Ryan_Moran
    Valued Contributor
    That was an interesting idea to use the rundll32 to call SetWindowPos....seemed like it should work but for whatever reason it goes off into space and never returns when I attempted it. Maybe someone else here can improve on this and explain why this doesn't work:






    wshell = new ActiveXObject('WScript.Shell');


    wshell.run("notepad");


    var myWindow = Sys.Process("notepad").Window("*Notepad*");


    wshell.run('rundll32.exe user32.dll,SetWindowPos ' + myWindow.Handle + ' -1 0 0 0 0 1');