I am making a windows Winform with a custom resizer(no border). I draw a resizer grip and handle some messages in WndProc. Just a heads up: I don't know what happens inside the WndProc, I just know it does what I want it to do(I pasted as snippet from somehwere).
Now I want to add a panel that is docked to the bottom of my form. When I do this, however, my resizing functionality is gone. Is there some way to restore this functionality without having to program my own resizer in.
I would think the functionality is lost because it only handles the resizing on the main form and not on any of its controls.
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x84)
{ // Trap WM_NCHITTEST
Point pos = new Point(m.LParam.ToInt32());
pos = this.PointToClient(pos);
if (
pos.X >= this.ClientSize.Width - cGrip &&
pos.Y >= this.ClientSize.Height - cGrip)
{
m.Result = (IntPtr)17; // HTBOTTOMRIGHT
return;
}
}
base.WndProc(ref m);
}
As I said in previous questions, I am not an experienced C# programmer at all. I have very little experience and the methods I use can probably be insanely optimized, so a detailed description of your answer would be very appreciated.
Related
I am having the Problem, that if I alter the LParam of a WM_MOVING message to keep my Form at a certain position, which is legal according to this , the Windows Aero Shake feature gets triggered and all other Windows minimize. The behaviour can be reproduced by creating a Windows Forms Project in Visual Studio and pasting the following code into the Form:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FormsTest
{
public partial class ShakeTest : Form
{
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 16)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public const int WM_MOVING = 0x0216;
public ShakeTest()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case WM_MOVING:
{
RECT rec;
rec.bottom = 500;
rec.left = 100;
rec.top = 100;
rec.right = 500;
Marshal.StructureToPtr(rec, m.LParam, true);
m.Result = new IntPtr(1);
}
break;
}
base.WndProc(ref m);
}
}
}
If you now grab the Window's title bar and move the mouse around a bit, the Shake gesture should get triggered, even though the Window isn't moving at all.
I tested this on Windows 10 only so far.
So my question is, can i disable the Shake feature for a certain Window or Process? If not, can i prevent Windows from thinking that im shaking the Window any other Way?
Thanks!
Frankly, I don't know if that is possible to disable Aero Shake for a particular window. But I can suggest a workaround that will prevent the Aero Shake from triggering.
There's a possibility to direct all the mouse input to only one window (and this will also hide the mouse events from the Aero Shake handler). Check the SetCapture description at MSDN. Also we will need a GetCapture and ReleaseCapture functions.
There's one remark: once you have set the mouse capture to a window - you are responsible for handling all the mouse input. So to achieve the goal you'll need to implement your own handler that will move the window. Fortunately, it's not that difficult.
Here's a code sample with a dialog that changes its size when it gets moving and restores the size when the moving is finished. Aero Shake is also never triggered.
public partial class ShakeTest : Form
{
[DllImport("user32.dll")]
static extern IntPtr SetCapture(IntPtr hWnd);
[DllImport("user32.dll")]
static extern IntPtr GetCapture();
[DllImport("user32.dll")]
static extern bool ReleaseCapture();
public const int WM_LBUTTONUP = 0x0202;
public const int WM_MOUSEMOVE = 0x0200;
public const int WM_NCLBUTTONDOWN = 0x00A1;
public const int HTCAPTION = 2;
private Point _lastCursorPos;
private Size _origianlSize;
public ShakeTest()
{
InitializeComponent();
}
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
// We need to get the moment when the user clicked on a non-client area
case WM_NCLBUTTONDOWN:
// We are interested only in a click on a title bar
if ((int) m.WParam == HTCAPTION)
{
// Set the capture so all the mouse input will be handled by this window
SetCapture(Handle);
// Keep the current cursor position to use it during the moving.
_lastCursorPos = Cursor.Position;
// Keep the original window size.
_origianlSize = Size;
// And change the dialog size to whatever you want
Size = new Size(300, 300);
}
break;
// Once we got the capture, we need to handle mouse moving by ourself
case WM_MOUSEMOVE:
// Check that our window has the capture
if (GetCapture() == Handle)
{
// Change the position of a window
Left += Cursor.Position.X - _lastCursorPos.X;
Top += Cursor.Position.Y - _lastCursorPos.Y;
_lastCursorPos = Cursor.Position;
}
break;
// When the left mouse button is released - it's time to release the mouse capture
case WM_LBUTTONUP:
// Check that our window has the capture
if (GetCapture() == Handle)
{
// Release the mouse capture
ReleaseCapture();
// Restore the size
Size = _origianlSize;
}
break;
}
base.WndProc(ref m);
}
}
I have a transparent WinForms app with GDI drawings (I use it as an overlay). The problem is that whenever I click on the GDI drawing the focus goes to the app window. How do I turn that of?
You need to use the right Color as the TransparencyKey!
Everything makes the Form clickable except Color.Fuchsia.
A not really explicable, let alone documented, 'feature', that may have started as a bug but now, hopefully forever and ever, let's us switch from clickable transparent forms, onto which we can draw and non-clickable ones through which we can interact with the background items..
// click-through:
this.BackColor = Color.Fuchsia;
this.TransparencyKey = this.BackColor;
// clickable:
this.BackColor = Color.FromArgb(255, 147, 151, 162); // any non-fuchsia color
this.TransparencyKey = this.BackColor;
As long as you implemented the overlay correctly (an owned window displayed with the Shown(owner) overload, example) then it just takes a little scrap of copy/paste code. Windows asks you what part of the window was clicked, you can respond with "it is transparent". So it will keep looking for anybody that is interested, its parent window is next.
Like this:
protected override void WndProc(ref Message m) {
const int WM_NCHITTEST = 0x84;
const int HTTRANSPARENT = -1;
if (m.Msg == WM_NCHITTEST) m.Result = new IntPtr(HTTRANSPARENT);
else base.WndProc(ref m);
}
I want to draw a margin line at 80 characters in a WinForms TextBox. Here is what I've tried, in my TextBox subclass:
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
const int WM_PAINT = 0x00F;
if (m.Msg == WM_PAINT) {
DrawMargin();
}
}
void DrawMargin()
{
using (Pen pen = new Pen(Color.Gray, 1)) {
using (Graphics graphics = CreateGraphics()) {
float charWidth = graphics.MeasureString("M", Font).Width;
graphics.DrawLine(pen, charWidth * 80, 0, charWidth * 80, Height);
}
}
}
There are at least three problems with this:
When the user enters some text, part of the line gets blanked out (goes white).
When the user selects some text with the mouse, the above happens again.
The line flickers when the TextBox is scrolled.
I notice that TED Notepad (which uses a Win32 EDIT control) is able to draw a margin without any problems, so it seems that it's possible to do what I want. Could anyone advise me how?
I am not sure about this method. But one thing you could look at trying is inserting an image into the text box. The image would of course be your margin, and the text would automatically start after the picture. To include a picture inside a text box see How can I insert an image into a RichTextBox?
Edit: I have also found this article http://www.codedblog.com/2007/09/17/owner-drawing-a-windowsforms-textbox/ which seems to facilitate painting in the background of a text box. The methods described here seems to take you a long way towards what you require.
Hope this helps.
As far as I can tell, the best way of doing this is simply to place a WinForms.Panel over the TextBox:
class FooTextBox : TextBox
{
public FooTextBox()
{
margin = new Panel();
margin.Enabled = false;
margin.BackColor = Color.LightGray;
margin.Top = 0;
margin.Height = ClientSize.Height;
margin.Left = <whatever>;
margin.Width = 1;
Controls.Add(margin);
}
Panel margin;
}
Since the panel is not Enabled, it doesn't take mouse input.
Simple, i want to move a windows pressing ALT+MOUSE, like linux os (ALT+drag).
It's possible to pass a win32 api (move api) to the windows interested clicking on it?
I have a windows services that hook key pressed (ALT button in specific).
When ALT key is pressed and a mouse down event is verified, i want to move window clicking anywhere, not only on the title bar!
Currently i move my form windows in this way:
using System.Runtime.InteropServices;
[DllImport( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )]
static extern IntPtr SendMessage( IntPtr hWnd, uint Msg, int wParam, int lParam );
[DllImportAttribute( "user32.dll", CharSet = CharSet.Auto, SetLastError = false )]
public static extern bool ReleaseCapture();
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
ReleaseCapture();
SendMessage( this.Handle, 0xa1, 0x2, 0 );
}
How can I get windows handle of the specific windows by clicking and after call SendMessage() on it?
It's possible?
You can do this by trapping the WM_NCHITTEST message that Windows sends to see what area of the window got clicked. You can fool it by returning HTCAPTION and it will dutifully perform the mouse actions you'd normally get when clicking the caption of a window. Including moving the window. Paste this code into your form:
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
// Trap WM_NCHITTEST when the ALT key is down
if (m.Msg == 0x84 && (Control.ModifierKeys == Keys.Alt)) {
// Translate HTCLIENT to HTCAPTION
if (m.Result == (IntPtr)1) m.Result = (IntPtr)2;
}
}
I worked this out my self, came up with something interesting of my own calculations, worked perfectly for me, for any window (any active foreground window). Kinda long, but really easy to understand if you follow along the comments, hope it helps :)
The way it works, is that you press a certain registered key-combo, like Ctrl+Alt+M
and the mouse will stick in the center of an active window, you move the mouse, the windows follows it, press the SAME combo again, to release, no need for mouse clicks or anything.
public void MoveWindow_AfterMouse()
{
// 1- get a handle to the foreground window (or any window that you want to move).
// 2- set the mouse pos to the window's center.
// 3- let the window move with the mouse in a loop, such that:
// win(x) = mouse(x) - win(width)/2
// win(y) = mouse(y) - win(height)/2
// This is because the origin (point of rendering) of the window, is at its top-left corner and NOT its center!
// 1-
IntPtr hWnd = WinAPIs.GetForegroundWindow();
// 2- Then:
// first we need to get the x, y to the center of the window.
// to do this, we have to know the width/height of the window.
// to do this, we could use GetWindowRect which will give us the coords of the bottom right and upper left corners of the window,
// with some math, we could deduce the width/height of the window.
// after we do that, we simply set the x, y coords of the mouse to that center.
RECT wndRect = new RECT();
WinAPIs.GetWindowRect(hWnd, out wndRect);
int wndWidth = wndRect.right - wndRect.left;
int wndHeight = wndRect.bottom - wndRect.top; // cuz the more you go down, the more y value increases.
Point wndCenter = new Point(wndWidth / 2, wndHeight / 2); // this is the center of the window relative to itself.
WinAPIs.ClientToScreen(hWnd, out wndCenter); // this will make its center relative to the screen coords.
WinAPIs.SetCursorPos(wndCenter.X, wndCenter.Y);
// 3- Moving :)))
while (true)
{
Point cursorPos = new Point();
WinAPIs.GetCursorPos(out cursorPos);
int xOffset = cursorPos.X - wndWidth / 2;
int yOffset = cursorPos.Y - wndHeight / 2;
WinAPIs.MoveWindow(hWnd, xOffset, yOffset, wndWidth, wndHeight, true);
Thread.Sleep(25);
}
}
And now:
int moveCommandToggle = 0;
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
int keyID = m.WParam.ToInt32();
if(keyID == some_key_combo_you_registered_for_the_moving)
{
if (moveCommandToggle++ % 2 == 0)
{
mover = new Thread(() => MoveWindow_AfterMouse());
mover.Start();
}
else mover.Abort();
}
}
}
If you're wondering about RECT:
public struct RECT
{
public int left; // xCoor of upper left corner.
public int top; // yCoor of upper left corner.
public int right; // xCoor of lower right corner.
public int bottom; // yCoor of lower right corner.
};
WinAPIs was just a static class that I did my DllImports in.
To detect rotation of the mouse wheel in .NET/WinForms, I can override OnMouseWheel. Clicking can be detected by overriding OnMouseDown (it's just the Middle button). But how do I detect tilting of the wheel (tilt to the left/right for horizontal scrolling)? Neither OnMouseWheel, not OnMouseDown is being called when I tilt the mouse wheel.
Covered here; in short, you need to handle the windows message manually (at it isn't handled directly in .NET - code is from the linked article):
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.HWnd != this.Handle)
{
return;
}
switch (m.Msg)
{
case Win32Messages.WM_MOUSEHWHEEL:
FireMouseHWheel(m.WParam, m.LParam);
m.Result = (IntPtr)1;
break;
default:
break;
}
}
...
abstract class Win32Messages
{
public const int WM_MOUSEHWHEEL = 0x020E;//discovered via Spy++
}
Based on this article, if you have the IntelliPoint drivers, you will get WM_MOUSEHWHEEL messages.