March 28, 2024, 07:29:15 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Position on screen of an application

Started by Andy, January 22, 2019, 05:10:03 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Andy

Hi,

Thought I might have a go at updating my alternative task manager.

I was wondering, as I can get the name, title, file location of a running application, could I also get it's screen dimensions / position on screen? - not sure if I've asked this before?

Just a thought,

Thanks,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

You need the window handle HWND and use SetWindowPos. For example:

SetWindowPos(handle, HWND_TOP, 10, 10, 1000, 600, 0)

The problem is how to get the handle. If you know exactly the caption and the window class name

INT handle = FindWindowW(L"Notepad", L"notepad caption") ' class, caption

otherwise... here we go... you'll need to find the processID of the window (using CreateToolhelp32Snapshot, for example). Once you have the processID I think you can get the handle by using EnumWindows.

Andy

Fasecero,

Thanks for that - interesting!

I can get the ProcID of the window, have done it before, here is a link to my alternative task manager:

https://www.ionicwind.com/forums/index.php?topic=5931.msg43806#msg43806

But when it come to what class it is, I'm completely lost on that one.  ???

Thanks,
Andy.
:)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.

fasecero

January 23, 2019, 01:31:15 PM #3 Last Edit: January 23, 2019, 01:57:46 PM by fasecero
Sorry I have misread your message and thought you are going to set (instead of get) each app dimension / position on screen. So use GetWindowRect instead of SetWindowPos.

I'm looking at your source code and I see that when the program starts you are using CreateToolhelp32Snapshot and getting each processID (pe.th32ProcessID in your code). So you'll need to use something like the following (non tested) to get the screen coordinates of each app that have a window handle associated with it.

HWND g_HWND=0
EnumWindows(&EnumWindowsProcMy, pe.th32ProcessID)

IF g_HWND THEN
WINRECT rc
GetWindowRect(g_HWND, rc)

' rc should contain the window coordinates
ENDIF


SUB EnumWindowsProcMy(HWND hwnd, LPARAM lParam), INT
    DWORD lpdwProcessId
    GetWindowThreadProcessId(hwnd, &lpdwProcessId)

    IF lpdwProcessId = lParam THEN
        g_HWND=hwnd
        return FALSE
    ENDIF

    return TRUE
ENDSUB


Andy

January 24, 2019, 06:24:55 AM #4 Last Edit: January 24, 2019, 06:29:58 AM by Andy
Fasecero,

Yes it works!

That's great so now I can get not only the process ID of a 32 bit process, but also the handle to it's window if it has one.

Sample of my code amended:

sub EnumProgs(string answer$),int

               retval = findPIDByName(answer$)

if retval <> 0
found = 1

if GotHandle = 0
EnumWindows(&EnumWindowsProcMy, pe.th32ProcessID)
endif

IF g_HWND
showwindow win6,@swshow
GetWindowRect(g_HWND,rc)
setsize win6,rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top
SHOWIMAGE win6,photo,@IMGSCALABLE,80,50,100,100
return retval
endif

endif

return retval
endsub


And...

SUB EnumWindowsProcMy(HWND hwnd, LPARAM lParam), INT

    DWORD lpdwProcessId
    GetWindowThreadProcessId(hwnd, &lpdwProcessId)

    IF lpdwProcessId = lParam THEN
        g_HWND = hwnd
        GotHandle = 1
        stoptimer win6
        starttimer win6,200
        return FALSE
    ENDIF

    return TRUE
ENDSUB


You can find findPIDByName(answer$) - where answer$ = "Theprogramname.exe" in the code of my alternative task manager (link above).

Thank you!
Andy.
:) 
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.