Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

get the path of an opened window before paste

Posted on 2011-03-07
10
Medium Priority
?
633 Views
Last Modified: 2013-12-17
Get the path of an opened window before paste;

Sample,
When I paste files to desktop, it message > The path before paste
is: C:\Documents and Settings\Administrator\Desktop\

When I paste files to my documents folder, it message > The path before paste
is: C:\Documents and Settings\Administrator\My Documents\
0
Comment
Question by:systan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 14

Author Comment

by:systan
ID: 35066457
Ok; help me with this;
This gets the path, BUT gets the last opened windows, if you switch from another windows to another?, the last windows opened still detected.


private string s;
private void button1_Click(object sender, EventArgs e)
{

//Shell32 is located in Interop.Shell32 assembly
 Shell32.ShellClass shell = new ShellClass();

//Shellwindows is located in SHDocVw.dll under C:\windows
ShellWindows shellWindows = (ShellWindows)shell.Windows();

//Don't know how to apply this to shellWindow
////////IntPtr activeHwnd = GetForegroundWindow();

foreach (ShellBrowserWindow shellWindow in shellWindows)
{
//this is the path        
s= shellWindow.LocationURL;


//This is the object that holds Explorer window information.
ShellFolderView view = (ShellFolderView)shellWindow.Document;

//Add error handling and null checks here
foreach (FolderItem item in view.SelectedItems())
{
    MessageBox.Show(item.Path.ToString());
}

}


label1.Text = s;
}
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 35069176
ShellBrowserWindow has HWND property. You could compare handle from GetForegroundWindow with it.
0
 
LVL 14

Author Comment

by:systan
ID: 35070464
Don't know how to apply, please help, a code snippet would be great.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Author Comment

by:systan
ID: 35090025
GetForegroundWindow Can't handle it, even GetLastActivePopup.
There must be a way to get the current or last folder opened.
0
 
LVL 14

Author Comment

by:systan
ID: 35091227
//Shell32 is located in Interop.Shell32 assembly
Shell32.ShellClass shell = new ShellClass();

//Shellwindows is located in SHDocVw.dll under C:\windows
ShellWindows shellWindows = (ShellWindows)shell.Windows();

foreach (ShellBrowserWindow shellWindow in shellWindows)
{
//This is the object that holds Explorer window information.
ShellFolderView view = (ShellFolderView)shellWindow.Document;
//Get ALL currently active windows path
MessageBox.Show(view.FocusedItem.Path);
}


The problem is, is ALL, it list all,  BUT I want to get ONLY the last active window.

Please, someone, help, it's very impossible that a longtime experts here did not encounter this.
I already provided the code snippet, just a little line to solve the problem.
0
 
LVL 19

Accepted Solution

by:
Rimvis earned 1000 total points
ID: 35092789
In this case, you will have to monitor windows and detect, when focus is changing. The problem with this approach is that you don't know, which window was active before you application was launched.

Here's sample code:

        delegate void WinEventDelegate(
            IntPtr hWinEventHook,uint eventType, IntPtr hwnd, int idObject,int idChild, 
            uint dwEventThread, uint dwmsEventTime);

        const uint WINEVENT_OUTOFCONTEXT = 0;
        const uint EVENT_SYSTEM_FOREGROUND = 3;

        [DllImport("user32.dll", SetLastError = true)]
        static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
        [DllImport("user32.dll")]
        static extern bool UnhookWinEvent(IntPtr hWinEventHook);
        [DllImport("user32.dll")]
        static extern IntPtr SetWinEventHook(
            uint eventMin,uint eventMax, IntPtr hmodWinEventProc,WinEventDelegate lpfnWinEventProc, 
            uint idProcess, uint idThread, uint dwFlags);

        IntPtr m_hhook;
        WinEventDelegate _WinEventHook = new WinEventDelegate(WinEventProc);
        static IntPtr m_explHwnd = IntPtr.Zero;


....

        private void Form1_Load(object sender, EventArgs e)
        {
            m_hhook = SetWinEventHook(EVENT_SYSTEM_FOREGROUND,
            EVENT_SYSTEM_FOREGROUND, IntPtr.Zero,
            _WinEventHook, 0, 0, WINEVENT_OUTOFCONTEXT);
        }

...

        private void cmdSomeTest_Click(object sender, EventArgs e)
        {
            string s = "x";

            //Shell32 is located in Interop.Shell32 assembly
            Shell32.ShellClass shell = new ShellClass();

            //Shellwindows is located in SHDocVw.dll under C:\windows
            ShellWindows shellWindows = (ShellWindows)shell.Windows();

            foreach (ShellBrowserWindow shellWindow in shellWindows)
            {
                if (shellWindow.HWND == (int)m_explHwnd)
                {
                    //This is the object that holds Explorer window information.
                    ShellFolderView view = (ShellFolderView)shellWindow.Document;
                    //Get ALL currently active windows path
                    Debug.Print(view.FocusedItem.Path);
                }
            }

            label1.Text = s;
        }
.....
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            UnhookWinEvent(m_hhook);
        }

Open in new window

0
 
LVL 14

Author Comment

by:systan
ID: 35093669
Error      1      The name 'WinEventProc' does not exist in the current context ?
0
 
LVL 14

Author Comment

by:systan
ID: 35096010
Ok, I've made some research, here it is;
http://social.msdn.microsoft.com/Forums/en/vcmfcatl/thread/0dbbc02e-cfd1-4183-a252-c1e9d6166fd8
http://social.msdn.microsoft.com/Forums/en/clr/thread/c04e343f-f2e7-469a-8a54-48ca84f78c28
http://ctp.social.msdn.microsoft.com/Forums/en-US/winforms/thread/30ae2081-17ed-497a-942e-efb75bca6295

The problem is?, it only gets the title of the window by getText,  i'LL try to see if there is a fix, please help also.

Please note, that I am getting the LAST/current path of last/current explored window.


Thanks
0
 
LVL 14

Author Closing Comment

by:systan
ID: 35100443
I think I got it,
thank you
0
 
LVL 19

Expert Comment

by:Rimvis
ID: 35144777
Looks like I forgot to paste code for the event handler :o( . Sorry abuot that. Here it is:

        static void WinEventProc(
            IntPtr hWinEventHook, uint eventType,IntPtr hwnd, int idObject, int idChild,
            uint dwEventThread, uint dwmsEventTime)
        {
            if (eventType == EVENT_SYSTEM_FOREGROUND)
            {
                int pid;
                Process[] pc = Process.GetProcessesByName("explorer");
                uint processID = 0;
                uint threadID = GetWindowThreadProcessId(hwnd, out processID);

                foreach (Process p in pc)
                {
                    if (p.Id == processID) m_explHwnd = hwnd;
                }
            }
        }

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question