I want to design a textbox which has index of each line on a separate list-box beside itself.everything is almost done but scrolling is still a problem for me.when I use WndProc to control scrolling, WM_VSCROLL works very well and makes both control scroll in same time,but when I want to send WM_MOUSEWHEEL via List-box,it doesn't work.I thought I can use a trick and iterate WM_VSCROLL when user moves mouse wheel but It doesn't work too.
private const int WM_VSCROLL = 0x115;
private const int WM_MOUSEWHEEL = 0x20A;
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_VSCROLL )
{
Message newMessage = Message.Create(Sequence.Handle, m.Msg, m.WParam, m.LParam);
Sequence.DirectMessage(newMessage);//turn to thread
}
else if(m.Msg == WM_MOUSEWHEEL)
{
if((int)m.WParam<0)
{
Message newMessage = Message.Create(Sequence.Handle, WM_VSCROLL,(IntPtr)1,(IntPtr) 0);
Message newMessage2 = Message.Create(this.Handle, WM_VSCROLL, (IntPtr)1, (IntPtr)0);
Sequence.DirectMessage(newMessage);
base.WndProc(ref newMessage2);
}
else
{
Message newMessage = Message.Create(Sequence.Handle, WM_VSCROLL, (IntPtr)0, (IntPtr)0);
Message newMessage2 = Message.Create(this.Handle, WM_VSCROLL, (IntPtr)0, (IntPtr)0);
Sequence.DirectMessage(newMessage);
base.WndProc(ref newMessage2);
}
}
}
Sequence is the name of my list-box and this infers to my textbox.I would appreciate any kind of help... .
Do the same as WM_SCROLL:
[DllImport("user32.dll", EntryPoint = "SendMessage")]
internal static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam);
private const int WM_VSCROLL = 0x115;
private const int WM_MOUSEWHEEL = 0x20A;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_VSCROLL || m.Msg == WM_MOUSEWHEEL)
{
SendMessage(Sequence.Handle, (UInt32)m.Msg, m.WParam, m.LParam);
}
base.WndProc(ref m);
}
Related
I wrote the WndProc method for a moveable control such this:
protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x0084;
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
if ((int)m.Result == 0x1)
m.Result = (IntPtr)0x2;
return;
}
base.WndProc(ref m);
}
and setted SizeAll cursor for the cursor property. but when we set m.Result as i did, the cursor will be Default in any case. How can i do?
You should handle WM_SETCURSOR too.
Also you may want to hanlde WM_NCLBUTTONDBLCLK to prevent your control from being maximized when you double click on it:
protected override void WndProc(ref Message m)
{
const int WM_NCHITTEST = 0x84;
const int WM_SETCURSOR = 0x20;
const int WM_NCLBUTTONDBLCLK = 0xA3;
const int HTCAPTION = 0x2;
if (m.Msg == WM_NCHITTEST)
{
base.WndProc(ref m);
m.Result = (IntPtr)HTCAPTION;
return;
}
if (m.Msg == WM_SETCURSOR)
{
if ((m.LParam.ToInt32() & 0xffff) == HTCAPTION)
{
Cursor.Current = Cursors.SizeAll;
m.Result = (IntPtr)1;
return;
}
}
if ((m.Msg == WM_NCLBUTTONDBLCLK))
{
m.Result = (IntPtr)1;
return;
}
base.WndProc(ref m);
}
Please guide me how to use this WndProc in Windows Forms application:
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == NativeCalls.APIAttach && (uint)lParam == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
{
// Get the current handle to the Skype window
NativeCalls.HWND_BROADCAST = wParam;
handled = true;
return new IntPtr(1);
}
// Skype sends our program messages using WM_COPYDATA. the data is in lParam
if (msg == NativeCalls.WM_COPYDATA && wParam == NativeCalls.HWND_BROADCAST)
{
COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT));
StatusTextBox.AppendText(data.lpData + Environment.NewLine);
// Check for connection
if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
ConnectButton.IsEnabled = false;
// Check for calls
IsCallInProgress(data.lpData);
handled = true;
return new IntPtr(1);
}
return IntPtr.Zero;
}
I have seen people use the above code in this way in WPF like
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// Attach WndProc
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
You can use Application.AddMessageFilter Method.
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class TestMessageFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
// Blocks all the messages relating to the left mouse button.
if (m.Msg >= 513 && m.Msg <= 515)
{
Console.WriteLine("Processing the messages : " + m.Msg);
return true;
}
return false;
}
}
The prototype for WndProc in C# is:
protected virtual void WndProc(ref Message m)
So, you need to override this procedure in your class, assumed that it's derived from Control.
protected override void WndProc(ref Message m)
{
Boolean handled = false; m.Result = IntPtr.Zero;
if (m.Msg == NativeCalls.APIAttach && (uint)m.Param == NativeCalls.SKYPECONTROLAPI_ATTACH_SUCCESS)
{
// Get the current handle to the Skype window
NativeCalls.HWND_BROADCAST = m.WParam;
handled = true;
m.Result = new IntPtr(1);
}
// Skype sends our program messages using WM_COPYDATA. the data is in lParam
if (m.Msg == NativeCalls.WM_COPYDATA && m.WParam == NativeCalls.HWND_BROADCAST)
{
COPYDATASTRUCT data = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
StatusTextBox.AppendText(data.lpData + Environment.NewLine);
// Check for connection
if (data.lpData.IndexOf("CONNSTATUS ONLINE") > -1)
ConnectButton.IsEnabled = false;
// Check for calls
IsCallInProgress(data.lpData);
handled = true;
m.Result = new IntPtr(1);
}
if (handled) DefWndProc(ref m); else base.WndProc(ref m);
}
I've been browsing and it's unclear to me how to simply send in the Ctrl+x or Ctrl+c command to copy/cut data(strings), but I'm pretty sure this is the pInvoke needed. A quick hand, anyone?
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
possibly along with this:
[DllImport("user32.dll")]
static extern IntPtr SetClipboardData(uint uFormat, IntPtr hMem);
not sure how to use either of these correctly, please help.
IntPtr nextClipboardAppWindow;
public frmMain()
{
nextClipboardAppWindow = (IntPtr)SetClipboardViewer((int)this.Handle);
}
protected override void WndProc(ref System.Windows.Forms.Message m)
{
// defined in winuser.h
const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x030D;
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
//DisplayClipboardData();
SendMessage(nextClipboardAppWindow, m.Msg, m.WParam,
m.LParam);
break;
case WM_CHANGECBCHAIN:
if (m.WParam == nextClipboardAppWindow)
nextClipboardAppWindow = m.LParam;
else
SendMessage(nextClipboardAppWindow, m.Msg, m.WParam,
m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
If you are using .Net and have access to System.Windows.Forms then you can use Clipboard class to Set or Get clipboard data.
Here is the link to MSDN with example.
http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard.aspx
Clipboard class actually posts data to System clipboard. It is not bound to application. It is simple as
System.Windows.Forms.Clipboard.SetText("This will be available across all applications");
In WinForms I used the following block of code to stop the application gaining focus by being clicked on:
private const int WM_MOUSEACTIVATE = 0x0021;
private const int MA_NOACTIVATEANDEAT = 0x0004;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_MOUSEACTIVATE)
{
m.Result = (IntPtr)MA_NOACTIVATEANDEAT;
return;
}
base.WndProc(ref m);
}
Is there any alternative to this in a WPF application? Can I use a HwndSource? Here is what I have so far (WM_MOUSEACTIVATE alone does not work as required and the application still gets focus):
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
source.AddHook(WndProc);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle messages...
int WM_MOUSEACTIVATE = 0x0021;
int WM_LBUTTONDOWN = 0x0201; //513
int WM_LBUTTONUP = 0x0202; //514
int WM_LBUTTONDBLCLK = 0x0203; //515
if (msg == WM_MOUSEACTIVATE || msg == MA_NOACTIVATEANDEAT || msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP || msg == WM_LBUTTONDBLCLK)
{
handled = true;
}
return IntPtr.Zero;
}
How can I get the Message Result?
Resolved by using:
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle messages...
int WM_MOUSEACTIVATE = 0x0021;
if (msg == WM_MOUSEACTIVATE )
{
handled = true;
return new IntPtr(MA_NOACTIVATEANDEAT);
}
return IntPtr.Zero;
}
I use the following code to drag a borderless form, by clicking and dragging the form itself. It works, but it doesn't for when you click and drag a control located on the form. I need to be able to drag it when clicked on some of the controls but not others - drag by labels, but don't by buttons and text boxes. How do I do it?
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_NCHITTEST = 0x84;
const int HTCLIENT = 0x1;
const int HTCAPTION = 0x2;
if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
}
Actually, I found the solution here.
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
// Paste the below code in the your label control MouseDown event
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
it works.
Also, in my code above, if resizing is desired, if statement should be changed to
if (m.Msg == WM_NCHITTEST)
if ((int)m.Result == HTCLIENT)
m.Result = (IntPtr)HTCAPTION;
Use Spy++ to analyse what controls are receiving what Windows Messages, you'll then know what you need to be capturing.
Without looking deeply at your code I'm imagining that child controls on the main Window are receiving messages rather than the form and you want to respond to some of these specifically.