In window application, using c# i created one form and put visible false minimize, maximize button and formborder to none, i place one panel at top of the form, in that panel i place close, minimize buttons. Now how can i drag the window form. Any reference please. my code is
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.FormBorderStyle = FormBorderStyle.None;
Thank you.
Simply register the MouseDown, MouseMove and MoueUp events for your Panel
bool MouseDownFlag = false;
Point start = new Point(0, 0);
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
start = new Point(e.X, e.Y);
MouseDownFlag = true;
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (MouseDownFlag)
{
Point newPoint = new Point();
newPoint.X = this.Location.X - (start.X - e.X);
newPoint.Y = this.Location.Y - (start.Y - e.Y);
this.Location = newPoint;
}
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
MouseDownFlag = false;
}
Related
I want to simulate a drag/drop event in WPF.
For this I'll need to gain access to the data stored in the "Drag/Drop buffer" and also I'll need to create a DragEventArgs.
I noticed that the DragEventArgs is sealed and has no public ctor.
So my questions are:
1. how can I create an instance of DragEventArgs?
2. How can I gain access to the drag/drop buffer?
i recently do this! i simulated drag/drop with MouseDown, MouseMove and MouseUp events. for example for my application, i have some canvases that i want to drag and drop them. every canvas has an id. in MouseDown event, i buffer its id and use it in MouseMove and MouseUp event. Desktop_Canvas is my main Canvas that contains some canvases. these canvases are in my dictionary (dic).
here is my code:
private Dictionary<int, Win> dic = new Dictionary<int, Win>();
private Point downPoint_Drag = new Point(-1, -1);
private int id_Drag = -1;
private bool flag_Drag = false;
public class Win
{
public Canvas canvas = new Canvas();
public Point downpoint = new Point();
public Win()
{
canvas.Background = new SolidColorBrush(Colors.Gray);
}
}
private void Desktop_Canvas_MouseMove(object sender, MouseEventArgs e)
{
try
{
Point movePoint = e.GetPosition(Desktop_Canvas);
if (flag_Drag && downPoint_Drag != new Point(-1, -1))
{
double dy1 = movePoint.Y - downPoint_Drag.Y, x = -1, dx1 = movePoint.X - downPoint_Drag.X, y = -1;
downPoint_Drag = movePoint;
if (x == -1)
x = Canvas.GetLeft(dic[id_Drag].canvas) + dx1;
if (y == -1)
y = Canvas.GetTop(dic[id_Drag].canvas) + dy1;
Canvas.SetLeft(dic[id_Drag].canvas, x);
Canvas.SetTop(dic[id_Drag].canvas, y);
}
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
downPoint_Drag = new Point(-1, -1);
id_Drag =-1;
flag_Drag = false;
}
catch
{
MouseEventArgs ee = new MouseEventArgs((MouseDevice)e.Device, 10);
Desktop_Canvas_MouseLeave(null, ee);
}
}
private void Desktop_Canvas_MouseLeave(object sender, MouseEventArgs e)
{
MouseButtonEventArgs ee = new MouseButtonEventArgs((MouseDevice)e.Device, 10, MouseButton.Left);
Desktop_Canvas_MouseLeftButtonUp(null, ee);
}
void canvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
downPoint_Drag = e.GetPosition(Desktop_Canvas);
int hoverId = HoverWin(downPoint_Drag);
flag_Drag = true;
id_Drag = hoverId;
dic[id_Drag].downpoint = new Point(downPoint_Drag.X, downPoint_Drag.Y);
}
private int HoverWin(Point p)
{
foreach (int i in dic.Keys)
{
if (dic[i].canvas.IsMouseOver)
return i;
}
return -1;
}
In my Application I have two buttons "open" and "close".
When I click open button window will be opened, when I click close button window will be closed.
When I click open button 3 times, 3 windows will be opened. I want to close all window when I click close button.
Here is my code [Please don't try to Change the Thread because that is my requirement in my Application]
public partial class MainWindow : Window
{
Window ProgressWindow;
Thread ProgressThread;
public MainWindow()
{
InitializeComponent();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
ProgressThread = new Thread(() =>
{
ProgressWindow = new Window();
ProgressWindow.Margin = new Thickness(0, 0, 50, 0);
ProgressWindow.WindowState = WindowState.Normal;
ProgressWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
ProgressWindow.Height = 180;
ProgressWindow.Width = 180;
ProgressWindow.Content = "Hello WPF";
ProgressWindow.ShowInTaskbar = false;
ProgressWindow.Show();
ProgressWindow.Closed += (sender2, e2) =>
ProgressWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
});
ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
if (ProgressThread.IsAlive == true)
{
ProgressThread.Abort();
}
}
}
I would recommend to store references to created windows, your code can look like this:
Stack<Window> ProgressWindow=new Stack<Window>();
Thread ProgressThread;
private void Open_Click(object sender, RoutedEventArgs e)
{
ProgressThread = new Thread(() =>
{
var window = new Window();
window.Margin = new Thickness(0, 0, 50, 0);
window.WindowState = WindowState.Normal;
window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
window.Height = 180;
window.Width = 180;
window.Content = "Hello WPF";
window.ShowInTaskbar = false;
window.Show();
window.Closed += (sender2, e2) =>
window.Dispatcher.InvokeShutdown();
ProgressWindow.Push(window);
System.Windows.Threading.Dispatcher.Run();
});
ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
while (ProgressWindow.Count > 0)
{
ProgressWindow.Pop().Close();
}
}
thread aborting is not recommended if it is "normal" workflow of your application, i.e. window wasn't closed because of some critical error
I wouldn't recommend what you are doing and actually I don't really know if it works like this, but since you stated that it's your (strange) requirement to use threads like this, I will only comment on the actual problem:
You should save the threads in a List and then close all the threads from this list.
Edit:
public partial class MainWindow : Window
{
Window ProgressWindow;
List<Thread> ProgressThreads = new List<Thread>();
public MainWindow()
{
InitializeComponent();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
ProgressThreads.Add(new Thread(() =>
{
ProgressWindow = new Window();
ProgressWindow.Margin = new Thickness(0, 0, 50, 0);
ProgressWindow.WindowState = WindowState.Normal;
ProgressWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
ProgressWindow.Height = 180;
ProgressWindow.Width = 180;
ProgressWindow.Content = "Hello WPF";
ProgressWindow.ShowInTaskbar = false;
ProgressWindow.Show();
ProgressWindow.Closed += (sender2, e2) =>
ProgressWindow.Dispatcher.InvokeShutdown();
System.Windows.Threading.Dispatcher.Run();
}));
ProgressThread.SetApartmentState(ApartmentState.STA);
ProgressThread.Start();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
foreach(var ProgressThread in ProgressThreads)
{
if (ProgressThread.IsAlive == true)
{
ProgressThread.Abort();
}
}
}
}
You will need to keep a record of all threads you have opened when you click "Open". Then in your "Close" method loop over that list closing each one.
Member variable:
List<Thread> allThreads = new List<Thread>();
Then in your open handler add:
allThreads.Add(ProgressThread);
Then your close handler becomes:
foreach (Thread thread in allThreads)
{
if (thread.IsAlive)
{
thread.Abort();
}
}
That what you are trying is unorthodox should go without saying.
How would I be able to submit events manually to be received by InkCanvas ?
What I need to do, is to set the mode of InkCanvas to ink mode, and then, send virtual events to InkCanvas so that I get a drawing behavior as if user used the real mouse.
Thanks
The following code snippet shows an example of drawing a shape in InkCanvas:
StylusPointCollection stroke1Points = new StylusPointCollection();
stroke1Points.Add(new StylusPoint(50,10));
stroke1Points.Add(new StylusPoint(90,50));
stroke1Points.Add(new StylusPoint(10,50));
stroke1Points.Add(new StylusPoint(50,10));
Stroke stroke1 = new Stroke(stroke1Points);
canvas.Strokes.Add(stroke1);
Where canvas is of type InkCanvas. The above generates a triangle in the canvas.
"And yes, you may accept the answer if it helps you."
Something like this?
private void inkSurface_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
inkSurface.CaptureMouse();
_inkStroke = new Stroke(
e.StylusDevice.GetStylusPoints(inkSurface));
_inkStroke.DrawingAttributes.Width = 5;
_inkStroke.DrawingAttributes.Height = 5;
_inkStroke.DrawingAttributes.Color = Colors.Black;
inkSurface.Strokes.Add(_inkStroke);
e.Handled = true;
}
private void inkSurface_MouseMove(object sender, MouseEventArgs e)
{
if (_inkStroke != null)
{
_inkStroke.StylusPoints.Add(
e.StylusDevice.GetStylusPoints(inkSurface));
}
e.Handled = true;
}
private void inkSurface_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
inkSurface.ReleaseMouseCapture();
e.Handled = true;
}
I create a new Windows Forms Application. I drag a button on to the form.
I need to drag and drop this button to another location within this form at run time.
any code snippets or links are appreciated.
I spent half an hour searching before coming here.
You can start with something like this:
bool isDragged = false;
Point ptOffset;
private void button1_MouseDown( object sender, MouseEventArgs e )
{
if ( e.Button == MouseButtons.Left )
{
isDragged = true;
Point ptStartPosition = button1.PointToScreen(new Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = button1.Location.X - ptStartPosition.X;
ptOffset.Y = button1.Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
private void button1_MouseMove( object sender, MouseEventArgs e )
{
if ( isDragged )
{
Point newPoint = button1.PointToScreen(new Point(e.X, e.Y));
newPoint.Offset(ptOffset);
button1.Location = newPoint;
}
}
private void button1_MouseUp( object sender, MouseEventArgs e )
{
isDragged = false;
}
I have a problem again. when I click button, window appears. when I click button again same window appears again. I want when I click button first time, page appears. but I want to prevent second click. can anyone help me with this problem? thanks in advance.
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
}
set a simple boolean value to check if the window is already open?
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
isWindowAlreadyOpen = true;
}
}
Should do the trick.
EDIT
You'll have to register the closed event of the window to unset the boolean:
private bool isWindowAlreadyOpen = false;
private void Dictionary_Click(object sender, RoutedEventArgs e)
{
if (!isWindowAlreadyOpen)
{
Dictionary dic = new Dictionary();
dic.Show();
dic.Topmost = true;
dic.Closed += Dictionary_Closed;
isWindowAlreadyOpen = true;
}
}
private void Dictionary_Closed(object sender, EventArgs e)
{
isWindowAlreadyOpen = false;
}
EDIT2
Alternatively, you can use dic.ShowDialog() if you want this window to be topmost and only one instance.