?
Solved

get the path of an opened window before paste

Posted on 2011-03-07
10
Medium Priority
?
623 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

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:…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

777 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