Windows Develop Bookmark and Share   
 index > Windows Forms General > Clipboard SetClipboardViewer identify source Appliation
 

Clipboard SetClipboardViewer identify source Appliation

Hi,

I am using the SetClipboardViewer function to register for clipboard events. Now in the

WndProc event that gets firedI am listning for events. This is the signature of the WndProc method.

protected override void WndProc(ref System.Windows.Forms.Message m)

Is there any way for me to find out which Application placed the item on the clipboard with this event.

Thanks

Arjuna.

Arjuna_M  Friday, June 29, 2007 5:44 PM
No.
nobugz  Saturday, June 30, 2007 7:04 AM
That's a fun and useful little project. This worked well, I hope I got the chaining right. Drop a picturebox and a label on a form, then paste this code:

using System;
using System.Windows.Forms;

namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
this.FormClosing += Form1_Closing;
this.Load += Form1_Load;
}
private IntPtr mPrevViewer;
private void Form1_Load(object sender, EventArgs e) {
// Register notification
mPrevViewer = SetClipboardViewer(this.Handle);
updateClipboard();
}
private void Form1_Closing(object sender, FormClosingEventArgs e) {
// Remove notification
ChangeClipboardChain(this.Handle, mPrevViewer);
}
private void updateClipboard() {
// Called when clipboard contents changed
pictureBox1.Image = null;
label1.Text = "";
if (Clipboard.ContainsText()) label1.Text = Clipboard.GetText();
if (Clipboard.ContainsImage()) pictureBox1.Image = Clipboard.GetImage();
}
protected override void WndProc(ref Message m) {
if (m.Msg == WM_CHANGECBCHAIN) {
if (m.WParam == mPrevViewer) mPrevViewer = m.LParam;
else if (mPrevViewer != null) SendMessage(mPrevViewer, m.Msg, m.WParam, m.LParam);
}
else if (m.Msg == WM_DRAWCLIPBOARD) {
updateClipboard();
if (mPrevViewer != null) SendMessage(mPrevViewer, m.Msg, m.WParam, m.LParam);
}
base.WndProc(ref m);
}
// P/Invoke declarations
private static int WM_CHANGECBCHAIN = 0x30d;
private static int WM_DRAWCLIPBOARD = 0x308;
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SetClipboardViewer(IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ChangeClipboardChain(IntPtr hRemove, IntPtr hNext);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}
}
nobugz  Friday, June 29, 2007 7:14 PM

Hi nobugz,

Thanks for the information, but unfortunately this is not what I am looking for. What I want is the name of the program that put the item on the clipboard in the first place. For example if I open notepad type something in it and press Ctrl+C then this will call the attached event on the sample code posted by you, more specifically the "WndProc" event. Is there any way from this method for me to figure out that this item was placed on the clipboard by the application notepad.

Thanks

Arjuna.

Arjuna_M  Saturday, June 30, 2007 6:13 AM
No.
nobugz  Saturday, June 30, 2007 7:04 AM

You can use google to search for other answers

Custom Search

More Threads

• listbox error:system.data.datarowview
• getting window client area coordinates in large windows
• WS-Security Problems with a firewall?
• Searching text in html document
• Picture Box Image in the parent form
• How do i get the direction of the selection in a RichTextBox?
• Dynamically create object determined at run time
• Formatted text - No RTF
• On-screen Keyboard: Active Window Loses Focus for External Apps
• Find a DataGridViewComboBoxColumn in the DGV