Solved

get the path of an opened window before paste

Posted on 2011-03-07
10
609 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 250 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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