How can I get the icon of a running application provided I know the Hwnd?
If you have the handle to the window, you can use GetClassLong:
HICON icon = (HICON)GetClassLong(window, GCL_HICON);
I you have the hwnd you can get the process ID by using the WINAPI GetWindowThreadProcessId.
With that you can create a C# Process object. Next, you can iterate through the process' ProcessModule Collection to get the filename of the executable. Finally, you can use the WINAPI function ExtractIconEx to get the icon from the path
Pinvoke has information on the two WINAPI methods
http://www.pinvoke.net/default.aspx/user32/GetWindowThreadProcessId.html
http://www.pinvoke.net/default.aspx/shell32/ExtractIconEx.html
Related
I have a VB6 application with a ListView (MSCOMCTL.OCX). I need to get the window handle (hwnd) for a listviewitem, or preferably (if something like that exists) - of a specific subitem.
I know I can get the handle for the column headers using FindWindowEx and looking for the class msvb_lib_header, but I don't know how to get the handle for the item. Spy++ shows a msvb_lib_header window as the child of the listview, but does not show any other windows.
List view items and subitems are not window handles. They are internal children of the list view, exposed using the LVM_GETITEM and LVM_SETITEM messages and the LVITEM structure. (Subitems use the same interface; the iSubItem member of LVITEM would be nonzero in this case.)
ListView.ListItem object has not hwnd.
You should search ListView by class name, next use SendMessage() to send the message LVM_GETITEM and using the LVITEM structure to get info about items:
LVM_GETITEM message (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb774953(v=vs.85).aspx
LVITEM structure (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/bb774760(v=vs.85).aspx
this reference page may help you:
List-View Control Reference (Windows)
https://msdn.microsoft.com/en-us/library/windows/desktop/ff485973(v=vs.85).aspx
I have a DLL that I want to play sounds using Direct Sound. In order to play sounds, I need the HWND of the executable. I don't have a HWND of the executable that loads the DLL. How do I get that in the DLL without passing it in from the executable?
You could use GetCurrentProcessId to get the current process Id.
You could then call EnumWindows, and check each window with GetWindowThreadProcessId to find a window associated with your process.
However, an easier option might be to just generate your own Window. You can create a 1x1 pixel window that is not visible, and use it with Direct Sound.
This has the advantage of working even if your calling process doesn't have a usable window (or deletes window handles regularly).
Call GetGUIThreadInfo on the main thread. This gets you a bunch of HWNDs. If you need a top-level HWND, pick any valid one (not all values may be filled) and find its top level ancestor with GetAncestor(GA_ROOT).
Ho do I get hWnd of the current window/form in VB6?
If you're on the form: Me.hWnd. If you don't know which form is the current form: Screen.ActiveForm.hWnd
Using Windows API, GetForegroundWindow() will get the handle of the topmost window regardless of which application it is from, and GetActiveWindow() will get the handle of your application's active window. The Declare statements you will need:
Declare Function GetForegroundWindow Lib "user32.dll" () As Long
Declare Function GetActiveWindow Lib "user32.dll" () As Long
Calling either function will return a window handle as described above.
It's been a long time since I used VB6, but this is what I remember:
You'll want to open the API Viewer, which should be in the Start Menu around the VB6 entry. When you open it, you want to select win32api.txt, and you'll get a list of all of the Win32 API functions. This is the easiest way to not mess up the function signatures. Copy and paste the function declaration into one of your VB6 modules.
I always "cheated" and just looked for my window by caption name, instead of looping over all of the available windows with GetWindow. If you're okay with this, you want to use FindWindow and pass the caption name as the second parameter.
does anyone know if you can get the coordinates of a window using the .NET framework or via pinvoking?
I would have the processID or mainwindowhandle.
HI, you can use
System.Windows.Forms.Control cr= System.Windows.Forms.Control.FromHandle(System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle);
now u can get location of that control.
In Windows Forms API, form.Top and form.Left should do.
If all you have is the process Id, you can iterate through the process's widows using EnumWindowsProc windows API method. once you get the window's handle which you want, you can query for its size and position.
I have an HDC object and I'd like to use it to get the name of the program that created it (such as Notepad or Firefox). Is there a way to do this?
If not, how can I do this?
WindowFromDC to get the window handle from the HDC
GetWindowThreadProcessId to get the process ID from the window handle
OpenProcess to obtain a process handle from the process ID
GetProcessImageFileName to obtain the program's path from the process handle
Or, once you have the window handle, GetWindowText to get the window title.