I have WPF borderless Transparent window.
By using this.DragMove(); I can successfully move the window.
I wanted to restrict window within screen area.
It's also working using below snippet.
private void Window_LocationChanged(object sender, EventArgs e)
{
CheckBounds();
}
private void CheckBounds()
{
var height = System.Windows.SystemParameters.PrimaryScreenHeight;
var width = System.Windows.SystemParameters.PrimaryScreenWidth;
if (this.Left < 0)
this.Left = 0;
if (this.Top < 0)
this.Top = 0;
if (this.Top + this.Height > height)
this.Top = height - this.Height;
if (this.Left + this.Width > width)
this.Left = width - this.Width;
}
But using above code, whenever window reach its max bounds using mouse drag, It starts flickering.
Could anybody suggest how to avoid this flickering?
The best way I know to deal with this issue is to handle the WM_MOVING windows message in your window and adjust the position there. Since the WM_MOVING message is received before the window actually moves and allows the position to be modified, you never see any jitter. Here is an example code-behind for a Window.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
public partial class MainWindow : Window
{
private HwndSource mSource;
public MainWindow()
{
InitializeComponent();
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
mSource = (HwndSource)PresentationSource.FromVisual(this);
mSource.AddHook(WndProc);
}
protected override void OnClosed(EventArgs e)
{
mSource.RemoveHook(WndProc);
mSource.Dispose();
mSource = null;
base.OnClosed(e);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == (int)WindowsMessage.WM_MOVING)
{
// TODO: Substitute realistic bounds
RECT bounds = new RECT() { Left = 0, Top = 0, Right = 1000, Bottom = 800 };
RECT window = (RECT)Marshal.PtrToStructure(lParam, typeof(RECT));
if (window.Left < bounds.Left)
{
window.Right = window.Right + bounds.Left - window.Left;
window.Left = bounds.Left;
}
if (window.Top < bounds.Top)
{
window.Bottom = window.Bottom + bounds.Top - window.Top;
window.Top = bounds.Top;
}
if (window.Right >= bounds.Right)
{
window.Left = bounds.Right - window.Right + window.Left - 1;
window.Right = bounds.Right - 1;
}
if (window.Bottom >= bounds.Bottom)
{
window.Top = bounds.Bottom - window.Bottom + window.Top - 1;
window.Bottom = bounds.Bottom - 1;
}
Marshal.StructureToPtr(window, lParam, true);
handled = true;
return new IntPtr(1);
}
handled = false;
return IntPtr.Zero;
}
}
Here are the helper objects that are used in the code:
[StructLayout(LayoutKind.Sequential)]
struct RECT
{
public int Left, Top, Right, Bottom;
}
enum WindowsMessage
{
WM_MOVING = 0x0216
}
P.S. The LocationChanged event (and associated OnLocationChanged override) is called in response to WM_MOVE, which is not called until the window has already moved. There does not seem to be a corresponding OnLocationChanging event.
Related
i have a Windows Forms application which should be borderless (even without the titlebar) and be resizeable and moveable.
so far, i've set the BorderStyle to 'none' which removes all the borders and let my program look pretty.
Now i've added invisble borders with the following:
private const int cGrip = 16; // Grip size
private const int cCaption = 50; // Caption bar height;
protected override void OnPaint(PaintEventArgs e)
{
Rectangle rc = new Rectangle(this.ClientSize.Width - cGrip, this.ClientSize.Height - cGrip, cGrip, cGrip);
ControlPaint.DrawSizeGrip(e.Graphics, this.BackColor, rc);
rc = new Rectangle(0, 0, this.ClientSize.Width, cCaption);
//e.Graphics.FillRectangle(Brushes.DarkBlue, rc);
}
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.Y < cCaption)
{
m.Result = (IntPtr)2; // HTCAPTION
return;
}
if (pos.X >= this.ClientSize.Width - cGrip && pos.Y >= this.ClientSize.Height - cGrip)
{
m.Result = (IntPtr)17; // HTBOTTOMRIGHT
return;
}
}
base.WndProc(ref m);
}
Program Layout
The blue rectangle is rendered via the OnPaint() method and shows the field where the user is able to move the window while holding the left mouse button.
My problem is, that this rectangle is below my label.
Does anyone know how to get the rectangle in front of the label?
Another way would be disabling the label which than turns dark grey.
You would solve my problem if i could change the color of my disabled label.
Leave the Label enabled and make it so that dragging the label causes the form to be dragged as well:
public const int HT_CAPTION = 0x2;
public const int WM_NCLBUTTONDOWN = 0xA1;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
private void label1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
I'm building custom user control that is based on ScrollableControl.
Right now I'm trying to add border around my control (similar to border that DataGridView has)
I'm able to draw border using:
e.Graphics.TranslateTransform(AutoScrollPosition.X*-1, AutoScrollPosition.Y*-1);
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.DarkBlue, ButtonBorderStyle.Dashed);
but this draws border around ClientRectangle, not around whole control:
As you can see in the above picture, border isn't surrounding scrollbars as it does in DataGridView.
Can I draw border around entire control so that scrollbars get included in area surrounded by border?
EDIT:
Based on Textbox custom onPaint I am able to draw custom border, by overriding WndProc but I get this weird looking border flickering:
Here is full code I have so far:
internal class TestControl : ScrollableControl
{
private int _tileWidth = 100;
private int _tileHeight = 100;
private int _tilesX = 20;
private int _tilesY = 20;
public TestControl()
{
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.Opaque, true);
SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
UpdateStyles();
ResizeRedraw = true;
AutoScrollMinSize = new Size(_tilesX*_tileWidth, _tilesY*_tileHeight);
}
private bool _test = true;
[DefaultValue(true)]
public bool Test
{
get { return _test; }
set
{
if(_test==value) return;
_test = value;
Update();
}
}
[DllImport("user32")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
struct RECT
{
public int left, top, right, bottom;
}
struct NCCALSIZE_PARAMS
{
public RECT newWindow;
public RECT oldWindow;
public RECT clientWindow;
IntPtr windowPos;
}
int clientPadding = 1;
int actualBorderWidth = 1;
Color borderColor = Color.Black;
protected override void WndProc(ref Message m)
{
//We have to change the clientsize to make room for borders
//if not, the border is limited in how thick it is.
if (m.Msg == 0x83 && _test) //WM_NCCALCSIZE
{
if (m.WParam == IntPtr.Zero)
{
RECT rect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
rect.left += clientPadding;
rect.right -= clientPadding;
rect.top += clientPadding;
rect.bottom -= clientPadding;
Marshal.StructureToPtr(rect, m.LParam, false);
}
else
{
NCCALSIZE_PARAMS rects = (NCCALSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALSIZE_PARAMS));
rects.newWindow.left += clientPadding;
rects.newWindow.right -= clientPadding;
rects.newWindow.top += clientPadding;
rects.newWindow.bottom -= clientPadding;
Marshal.StructureToPtr(rects, m.LParam, false);
}
}
if (m.Msg == 0x85 && _test) //WM_NCPAINT
{
base.WndProc(ref m);
IntPtr wDC = GetWindowDC(Handle);
using (Graphics g = Graphics.FromHdc(wDC))
{
ControlPaint.DrawBorder(g, new Rectangle(0, 0, Size.Width, Size.Height), borderColor, actualBorderWidth, ButtonBorderStyle.Solid,
borderColor, actualBorderWidth, ButtonBorderStyle.Solid, borderColor, actualBorderWidth, ButtonBorderStyle.Solid,
borderColor, actualBorderWidth, ButtonBorderStyle.Solid);
}
return;
}
base.WndProc(ref m);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
e.Graphics.TranslateTransform(AutoScrollPosition.X, AutoScrollPosition.Y);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
var offsetX = (AutoScrollPosition.X*-1)/_tileWidth;
var offsetY = (AutoScrollPosition.Y*-1)/_tileHeight;
var visibleX = Width/_tileWidth + 2;
var visibleY = Height/_tileHeight + 2;
var x = Math.Min(visibleX + offsetX, _tilesX);
var y = Math.Min(visibleY + offsetY, _tilesY);
for (var i = offsetX; i < x; i++)
{
for (var j = offsetY; j < y; j++)
{
e.Graphics.FillRectangle(Brushes.Beige, new Rectangle(i*_tileWidth, j*_tileHeight, _tileWidth, _tileHeight));
e.Graphics.DrawString(string.Format("{0}:{1}", i, j), Font, Brushes.Black, new Rectangle(i*_tileWidth, j*_tileHeight, _tileWidth, _tileHeight));
}
}
using (var p = new Pen(Color.Black))
{
for (var i = offsetX + 1; i < x; i++)
{
e.Graphics.DrawLine(p, i*_tileWidth, 0, i*_tileWidth, y*_tileHeight);
}
for (var i = offsetY + 1; i < y; i++)
{
e.Graphics.DrawLine(p, 0, i*_tileHeight, x*_tileWidth, i*_tileHeight);
}
}
e.Graphics.FillRectangle(Brushes.White, AutoScrollPosition.X*-1 + 10, AutoScrollPosition.Y*-1 + 10, 35, 14);
e.Graphics.DrawString("TEST", DefaultFont, new SolidBrush(Color.Red), AutoScrollPosition.X*-1 + 10, AutoScrollPosition.Y*-1 + 10);
e.Graphics.TranslateTransform(AutoScrollPosition.X*-1, AutoScrollPosition.Y*-1);
ControlPaint.DrawBorder(e.Graphics, ClientRectangle, Color.Red, actualBorderWidth, ButtonBorderStyle.None,
Color.Red, actualBorderWidth, ButtonBorderStyle.None, Color.Red, actualBorderWidth, ButtonBorderStyle.Solid,
Color.Red, actualBorderWidth, ButtonBorderStyle.Solid);
}
protected override void OnScroll(ScrollEventArgs e)
{
if (DesignMode)
{
base.OnScroll(e);
return;
}
if (e.Type == ScrollEventType.First)
{
LockWindowUpdate(Handle);
}
else
{
LockWindowUpdate(IntPtr.Zero);
Update();
if (e.Type != ScrollEventType.Last) LockWindowUpdate(Handle);
}
}
protected override void OnMouseWheel(MouseEventArgs e)
{
if (VScroll && (ModifierKeys & Keys.Shift) == Keys.Shift)
{
VScroll = false;
LockWindowUpdate(Handle);
base.OnMouseWheel(e);
LockWindowUpdate(IntPtr.Zero);
Update();
VScroll = true;
}
else
{
LockWindowUpdate(Handle);
base.OnMouseWheel(e);
LockWindowUpdate(IntPtr.Zero);
Update();
}
}
[DllImport("user32.dll", SetLastError = true)]
private static extern bool LockWindowUpdate(IntPtr hWnd);
}
Can this flickering be fixed?
I was able to solve my problem by overriding CreateParams:
protected override CreateParams CreateParams
{
[SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= NativeMethods.WS_EX_CONTROLPARENT;
cp.ExStyle &= (~NativeMethods.WS_EX_CLIENTEDGE);
cp.Style &= (~NativeMethods.WS_BORDER);
cp.Style |= NativeMethods.WS_BORDER;
return cp;
}
}
and here is required NativeMethods class:
internal static class NativeMethods
{
public const int WS_EX_CONTROLPARENT = 0x00010000;
public const int WS_EX_CLIENTEDGE = 0x00000200;
public const int WS_BORDER = 0x00800000;
}
below is the result:
You can simply derive from UserControl. It has built-in support for showing scrollbars and also has built-in support for showing borders.
All of built-in features of UserControl can be added to your control which is derived from ScrollableControl but with some additional try and error effort or taking look at source code of UserControl. But using UserControl class you can simply have those features.
Show Border
To show border, it's enough to set its BorderStyle to FixedSingle to get desired feature:
Show Scrollbars
To gain scroll feature, it's enough to set its AutoScroll to true and also set a suitable AutoScrollMinSize for control. Then when the width or height of the control is less than width or height of given size, the suitable scrollbar will be shown.
Custom Border Color
I also suppose you want to have different border color for the control, then it's enough to override WndProc and handle WM_NCPAINT and draw custom border for the control like this:
In above example, I've used the same technique which I used for Changing BorderColor of TextBox with a small change, here I checked if the BorderStyle equals to FixedSingle the I drew the border with desired color.
Enable the designer to act like a Parent Control at design time
If you want to enable it's designer to be able to drop some controls onto your UserControl, it's enough to decorate it with [Designer(typeof(ParentControlDesigner))]. This way, when you drop your UserControl on form, it can host other controls like a panel control. If you don't like this feature, just don't decorate it with that attribute and it will use Control designer by default which doesn't act like a parent control.
I'm trying to write a magnifier application using a winform or wpf windows. The idea is to drag the window over a spot on the screen and magnify it. I know it exists commercially but need to build a customized version. The challenge I'm having is to capture the screen image behind the active application.
I have found code to capture a screen image below. But it includes the active window
{
Rectangle bounds = new Rectangle(this.Location, this.Size);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(this.Location, Point.Empty, bounds.Size);
Bitmap bp2 = new Bitmap(bitmap); // local copy of image...
pictureBox1.Image = bp2;
}
Adding statements to hide the active application correct the image capture, but introduce a screen flicker which I'd like to avoid. (modified code below)
{
this.StartPosition = FormStartPosition.Manual; // get current window location
Point cur = this.Location;
this.Location = new Point(-500, -500); // hide the active app off screen.
Rectangle bounds = new Rectangle(cur, this.Size);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(cur, Point.Empty, bounds.Size);
Bitmap bp2 = new Bitmap(bitmap); // local copy of image...
pictureBox1.Image = bp2;
this.Location = cur; // restore application location
}
Can someone suggest an alternative to capture a screen region, behind an active windows?
Thx.
Wrapping the Magnification API is pretty useful. I created a Winforms control that does this. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto a form.
The TrackMouse property controls which part of the screen is shown magnified. Set to False, it magnifies the area covered by the control, making it act like a magnifying glass. Set to True, it will operate like Windows' Magnifier, following the mouse.
The Magnification property controls the amount of magnification. You can already adjust it by using the mouse wheel.
The form you drop it should have its TopMost property set to True. You might want to tinker with its Region property to make it resemble a spyglass.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class Magnifier : Control {
public Magnifier() {
if (!MagInitialize()) throw new NotSupportedException();
timer = new Timer { Interval = 45 };
timer.Tick += (o, ea) => { if (trackMouse) setSource(false); else Invalidate(); };
}
[DefaultValue(false)]
public bool TrackMouse {
get { return trackMouse; }
set { trackMouse = value; setSource(false); }
}
[DefaultValue(2.0f)]
public float Magnification {
get { return magnification; }
set { magnification = Math.Max(1, value); setSource(true); }
}
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
if (!this.DesignMode) {
cp.ClassName = "Magnifier";
//cp.Style |= MS_SHOWMAGNIFIEDCURSOR;
this.SetStyle(ControlStyles.UserPaint, true);
}
return cp;
}
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (!this.DesignMode) {
setSource(true);
this.FindForm().LocationChanged += ParentLocationChanged;
timer.Start();
}
}
protected override void Dispose(bool disposing) {
if (disposing) {
var frm = this.FindForm();
if (frm != null) frm.LocationChanged -= ParentLocationChanged;
timer.Dispose();
MagUninitialize();
}
base.Dispose(disposing);
}
private void ParentLocationChanged(object sender, EventArgs e) {
if (!trackMouse) setSource(false);
}
protected override void OnSizeChanged(EventArgs e) {
setSource(false);
base.OnSizeChanged(e);
}
protected override void OnMouseWheel(MouseEventArgs e) {
this.Magnification += e.Delta / 100f;
((HandledMouseEventArgs)e).Handled = true;
}
private void setSource(bool newmag) {
if (!this.IsHandleCreated || this.DesignMode) return;
if (newmag) {
var xform = new MAGTRANSFORM();
xform.v11 = xform.v22 = magnification;
xform.v33 = 1.0f;
MagSetWindowTransform(this.Handle, ref xform);
}
Point center;
if (trackMouse) center = Cursor.Position;
else {
var rc = this.RectangleToScreen(this.Bounds);
center = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
}
var scr = Screen.FromPoint(center);
var rect = new RECT();
rect.left = Math.Max(scr.Bounds.Left, center.X - (int)(this.Width / magnification / 2));
rect.top = Math.Max(scr.Bounds.Top, center.Y - (int)(this.Height / magnification / 2));
rect.right = rect.left + (int)(this.Width / magnification);
if (rect.right > scr.Bounds.Right) {
rect.right = scr.Bounds.Right;
rect.left = rect.right - (int)(this.Width / magnification);
}
rect.bottom = center.Y + (int)(this.Height / magnification);
if (rect.bottom > scr.Bounds.Bottom) {
rect.bottom = scr.Bounds.Bottom;
rect.top = rect.bottom - (int)(this.Height / magnification);
}
MagSetWindowSource(this.Handle, ref rect);
this.Invalidate();
}
private Timer timer;
private bool trackMouse;
private float magnification = 2.0f;
private struct RECT {
public int left, top, right, bottom;
}
private struct MAGTRANSFORM {
public float v11, v12, v13;
public float v21, v22, v23;
public float v31, v32, v33;
}
[DllImport("magnification.dll")]
private static extern bool MagInitialize();
[DllImport("magnification.dll")]
private static extern bool MagUninitialize();
[DllImport("magnification.dll")]
private static extern bool MagSetWindowSource(IntPtr hWnd, ref RECT rc);
[DllImport("magnification.dll")]
private static extern bool MagSetWindowTransform(IntPtr hWnd, ref MAGTRANSFORM xform);
private const int MS_SHOWMAGNIFIEDCURSOR = 1;
private const int MS_CLIPAROUNDCURSOR = 2;
private const int MS_INVERTCOLORS = 4;
}
I'm trying to write a magnifier application using a winform or wpf windows. The idea is to drag the window over a spot on the screen and magnify it. I know it exists commercially but need to build a customized version. The challenge I'm having is to capture the screen image behind the active application.
I have found code to capture a screen image below. But it includes the active window
{
Rectangle bounds = new Rectangle(this.Location, this.Size);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(this.Location, Point.Empty, bounds.Size);
Bitmap bp2 = new Bitmap(bitmap); // local copy of image...
pictureBox1.Image = bp2;
}
Adding statements to hide the active application correct the image capture, but introduce a screen flicker which I'd like to avoid. (modified code below)
{
this.StartPosition = FormStartPosition.Manual; // get current window location
Point cur = this.Location;
this.Location = new Point(-500, -500); // hide the active app off screen.
Rectangle bounds = new Rectangle(cur, this.Size);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
Graphics g = Graphics.FromImage(bitmap);
g.CopyFromScreen(cur, Point.Empty, bounds.Size);
Bitmap bp2 = new Bitmap(bitmap); // local copy of image...
pictureBox1.Image = bp2;
this.Location = cur; // restore application location
}
Can someone suggest an alternative to capture a screen region, behind an active windows?
Thx.
Wrapping the Magnification API is pretty useful. I created a Winforms control that does this. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto a form.
The TrackMouse property controls which part of the screen is shown magnified. Set to False, it magnifies the area covered by the control, making it act like a magnifying glass. Set to True, it will operate like Windows' Magnifier, following the mouse.
The Magnification property controls the amount of magnification. You can already adjust it by using the mouse wheel.
The form you drop it should have its TopMost property set to True. You might want to tinker with its Region property to make it resemble a spyglass.
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
class Magnifier : Control {
public Magnifier() {
if (!MagInitialize()) throw new NotSupportedException();
timer = new Timer { Interval = 45 };
timer.Tick += (o, ea) => { if (trackMouse) setSource(false); else Invalidate(); };
}
[DefaultValue(false)]
public bool TrackMouse {
get { return trackMouse; }
set { trackMouse = value; setSource(false); }
}
[DefaultValue(2.0f)]
public float Magnification {
get { return magnification; }
set { magnification = Math.Max(1, value); setSource(true); }
}
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
if (!this.DesignMode) {
cp.ClassName = "Magnifier";
//cp.Style |= MS_SHOWMAGNIFIEDCURSOR;
this.SetStyle(ControlStyles.UserPaint, true);
}
return cp;
}
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (!this.DesignMode) {
setSource(true);
this.FindForm().LocationChanged += ParentLocationChanged;
timer.Start();
}
}
protected override void Dispose(bool disposing) {
if (disposing) {
var frm = this.FindForm();
if (frm != null) frm.LocationChanged -= ParentLocationChanged;
timer.Dispose();
MagUninitialize();
}
base.Dispose(disposing);
}
private void ParentLocationChanged(object sender, EventArgs e) {
if (!trackMouse) setSource(false);
}
protected override void OnSizeChanged(EventArgs e) {
setSource(false);
base.OnSizeChanged(e);
}
protected override void OnMouseWheel(MouseEventArgs e) {
this.Magnification += e.Delta / 100f;
((HandledMouseEventArgs)e).Handled = true;
}
private void setSource(bool newmag) {
if (!this.IsHandleCreated || this.DesignMode) return;
if (newmag) {
var xform = new MAGTRANSFORM();
xform.v11 = xform.v22 = magnification;
xform.v33 = 1.0f;
MagSetWindowTransform(this.Handle, ref xform);
}
Point center;
if (trackMouse) center = Cursor.Position;
else {
var rc = this.RectangleToScreen(this.Bounds);
center = new Point(rc.Left + rc.Width / 2, rc.Top + rc.Height / 2);
}
var scr = Screen.FromPoint(center);
var rect = new RECT();
rect.left = Math.Max(scr.Bounds.Left, center.X - (int)(this.Width / magnification / 2));
rect.top = Math.Max(scr.Bounds.Top, center.Y - (int)(this.Height / magnification / 2));
rect.right = rect.left + (int)(this.Width / magnification);
if (rect.right > scr.Bounds.Right) {
rect.right = scr.Bounds.Right;
rect.left = rect.right - (int)(this.Width / magnification);
}
rect.bottom = center.Y + (int)(this.Height / magnification);
if (rect.bottom > scr.Bounds.Bottom) {
rect.bottom = scr.Bounds.Bottom;
rect.top = rect.bottom - (int)(this.Height / magnification);
}
MagSetWindowSource(this.Handle, ref rect);
this.Invalidate();
}
private Timer timer;
private bool trackMouse;
private float magnification = 2.0f;
private struct RECT {
public int left, top, right, bottom;
}
private struct MAGTRANSFORM {
public float v11, v12, v13;
public float v21, v22, v23;
public float v31, v32, v33;
}
[DllImport("magnification.dll")]
private static extern bool MagInitialize();
[DllImport("magnification.dll")]
private static extern bool MagUninitialize();
[DllImport("magnification.dll")]
private static extern bool MagSetWindowSource(IntPtr hWnd, ref RECT rc);
[DllImport("magnification.dll")]
private static extern bool MagSetWindowTransform(IntPtr hWnd, ref MAGTRANSFORM xform);
private const int MS_SHOWMAGNIFIEDCURSOR = 1;
private const int MS_CLIPAROUNDCURSOR = 2;
private const int MS_INVERTCOLORS = 4;
}
I am building a Windows Forms applicaton in C# and have a TrackBar on my Form. How can I compute the (pixel) position of the tip of the tracker? I would like to draw a line from there to another point on my form.
Additionally I also would like to compute the lowest and highest possible x position of the tip.
The native Windows control that is wrapped by TrackBar has an awkward restriction, you cannot get the positions for the first and last tic marks. You can however get the display rectangles for the channel and the slider. This class returns them:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;
class MyTrackBar : TrackBar {
public Rectangle Slider {
get {
RECT rc = new RECT();
SendMessageRect(this.Handle, TBM_GETTHUMBRECT, IntPtr.Zero, ref rc);
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
}
public Rectangle Channel {
get {
RECT rc = new RECT();
SendMessageRect(this.Handle, TBM_GETCHANNELRECT, IntPtr.Zero, ref rc);
return new Rectangle(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
}
}
private const int TBM_GETCHANNELRECT = 0x400 + 26;
private const int TBM_GETTHUMBRECT = 0x400 + 25;
private struct RECT { public int left, top, right, bottom; }
[DllImport("user32.dll", EntryPoint = "SendMessageW")]
private static extern IntPtr SendMessageRect(IntPtr hWnd, int msg, IntPtr wp, ref RECT lp);
}
Here is a sample usage in a form, it draws a line from the first tick mark to the slider pointer:
private void myTrackBar1_ValueChanged(object sender, EventArgs e) {
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e) {
var chan = this.RectangleToClient(myTrackBar1.RectangleToScreen(myTrackBar1.Channel));
var slider = this.RectangleToClient(myTrackBar1.RectangleToScreen(myTrackBar1.Slider));
e.Graphics.DrawLine(Pens.Black, chan.Left + slider.Width / 2, myTrackBar1.Bottom + 5,
slider.Left + slider.Width / 2, myTrackBar1.Bottom + 5);
}