Popup Move in WPF - c#

I created a WPF Popup Control on my WPF Form.When I click,the Popup Opens and it also moves properly. but when I Clicked on textbox in popup window then popup moves slightly towards the edge of main window. I don't want to move popup when I clicked on textbox in Popup?
private void MyAddJobPopup_MouseMove(object sender,System.Windows.Input.MouseEventArgs e) {
if (!MyAddJobPopup.IsOpen)
MyAddJobPopup.IsOpen = true;
if (e.LeftButton == MouseButtonState.Pressed) {
var mousePosition = e.GetPosition(this.HardwareElectricalTbl);
this.MyAddJobPopup.HorizontalOffset = mousePosition.X;
this.MyAddJobPopup.VerticalOffset = mousePosition.Y;
}
}

When setting the HorizontalOffset and VerticalOffset they use the top left location of the window, which is not the location you want to set it to, you want to set it to the mouse position minus the mouses previous relative position to the window.
To do this save the following locations when you start clicking (for example inside of an onClick event):
//both should be negative so adding will actually remove from the location which is what you want
float offsetX = this.MyAddJobPopup.HorizontalOffset - mousePosition.X;
float offsetY = this.MyAddJobPopup.VerticalOffset- mousePosition.Y;
then when you set the location add them:
this.MyAddJobPopup.HorizontalOffset = mousePosition.X + offsetX;
this.MyAddJobPopup.VerticalOffset = mousePosition.Y + offsetY;

Related

Re-position WPF window after resizing to mouse location

I am creating a small game that resizes the main window to fit a growing board. I want to re-position the window to keep the button that was clicked under the mouse after resizing. Currently, when clicked the board gets wider and moves the button away from the mouse.
How do I define the button as the anchor point when I move the window?
I don't know if there is a way to use the button as an anchor point. But after a little thinking, i came up with a code that can work for you.
Basically, i'm using the mouse relative to the button position before and after the resize to move the window accordingly. I hope it helps.
private void Btn1Click(object sender, RoutedEventArgs e)
{
int widthGrowth = 50;
int heightGrowth = 80;
Button btn = sender as Button;
Point oldMousePosition = Mouse.GetPosition(btn);
Width += widthGrowth;
Height += heightGrowth;
Point newMousePosition = Mouse.GetPosition(btn);
Left += newMousePosition.X - oldMousePosition.X;
Top += newMousePosition.Y - oldMousePosition.Y;
}

Panel transparency over different panels

I am a developing a c# app that will be used in a touch computer. It has a secret combination to close it in case something goes wrong. TopRight, TopLeft, TopRight.
To achieve that, I created a class that add two panels to the form:
public enum Location
{
Top,
Bottom
};
public SecretCode(Form form, int boxSize, int resetTimer, Location location, bool debug)
{
timerReset.Interval = resetTimer;
timerReset.Tick += TimerReset_Tick;
Panel panelRight = new Panel();
if (debug) panelRight.BackColor = Color.Yellow;
panelRight.Size = new Size(boxSize, boxSize);
panelRight.Location = new Point(form.Width - boxSize, location == Location.Top ? 0 : form.Height - boxSize);
panelRight.Click += PanelTopRight_Click;
Panel panelLeft = new Panel();
if (debug) panelLeft.BackColor = Color.Red;
panelLeft.Size = new Size(boxSize, boxSize);
panelLeft.Location = new Point(0, location == Location.Top ? 0 : form.Height - boxSize);
panelLeft.Click += PanelTopLeft_Click;
form.Controls.Add(panelRight);
form.Controls.Add(panelLeft);
panelLeft.BringToFront();
panelRight.BringToFront();
}
On my main form, I have different panels (they are all fullscreen) that I show / hide. Every panel has its own background image.
Panel Intro
Panel Signup
When the user finishes reading the intro, the whole panel disappears and a the Signup Panel shows up.
Is there any easy way to ensure that the SecretCode panels would always be transparent or do I need to change its parent every time a show a new panel?

Drag Ovalshape across form

So I have a bit of a problem with moving Ovalshapes around a form. The goal is to have a circle move within the bounds of two other circles, one placed inside the other and the moving circle essentially moving around them.
I am having trouble moving the circle with the mouse. Whenever I click and hold the circle, the circle moves to the coordinates of the location I clicked on the circle, such that if I click in the middle of an Ovalshape of size 10, it would set the circle's location to (5,5).
Here is what I have:
public partial class Form1 : Form
{
int smallRadius;
int largeRadius;
int movingRadius;
int distanceFromCenterToLocation;
bool mouseDown;
Point movingCenter;
Point returnPoint;
public Form1()
{
mouseDown = false;
InitializeComponent();
smallRadius = (ovalShape1.Right - ovalShape1.Left) / 2;
largeRadius = (ovalShape2.Right - ovalShape2.Left) / 2;
Point center = new Point(ovalShape1.Left + smallRadius, ovalShape1.Top + smallRadius);
ovalShape3.Height = largeRadius - smallRadius;
ovalShape3.Width = largeRadius - smallRadius;
movingRadius = (ovalShape3.Right - ovalShape3.Left) / 2;
ovalShape3.Location = new Point(center.X - (movingRadius), center.Y - largeRadius);
movingCenter = new Point(ovalShape3.Left + movingRadius, ovalShape3.Top + movingRadius);
distanceFromCenterToLocation = Convert.ToInt32(Math.Sqrt(Math.Pow(movingRadius, 2.0) + Math.Pow(movingRadius, 2.0)));
int middleRadius = center.X - movingCenter.X;
}
private void ovalShape3_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void ovalShape3_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void ovalShape3_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
ovalShape3.Location = e.Location;
}
}
}
For some reason OvalShape isn't derived from Control and doesn't behave like a control.
When a control receives a mouse event the Location property of the MouseEventArgs holds the coordinates relative to the upper-left corner of the form. Shapes however receive the coordinates relative to their own upper-left corner.
When a mouse button is pressed over a control it will capture the mouse so that subsequent events are sent to the same control until you release the button. Shapes only receive mouse events if the mouse is over the shape no matter if a mouse button is pressed. Once you move the mouse to the top or left the shape doesn't receive any mouse events. Therefore the shape doesn't follow the mouse and also the MouseUp event is not handled.
All shapes in a form are embedded in a single control of type ShapeContainer. This is created automatically when you add a shape to a form in the Visual Studio designer. To get the expected behavior you need to find that ShapeContainer (probably called shapeContainer1) and handle its mouse events instead:
public Form1()
{
InitializeComponent();
// do your initialization
...
// assign the events to the ShapeContainer
// don't forget to remove the handlers from ovalShape3 in the designer!!!
ShapeContainer container = ovalShape3.Parent;
container.MouseUp += ovalShape3_MouseUp;
container.MouseDown += ovalShape3_MouseDown;
container.MouseMove += ovalShape3_MouseMove;
}

DynamicDataDisplay D3:ChartPlotter zoom proplem

I'm using a simple ChartPlotter in my C# WPF application. When I zoom in/out by mouse scrolling, both Axis are changed. How can I control the zooming by mouse scrolling so it will affect only the X-Axis?
This feature is already built into D3, if you hover your mouse over one of the axes, and do a mouse wheel scroll, the zoom is only pertained to the axis you were hovered over. If you want to replicate this in your code, you can see examples of it in the source code.
The zoom feature is implemented in "MouseNavigation.cs". The MouseWheel handler will call underneath function:
Viewport.Visible = Viewport.Visible.Zoom(zoomTo, zoomSpeed);
And fortunately, there is a ZoomX function for your needs.
Thus just remove MouseNavigation from your plotter, then re-implement your own as below:
// Remove mouse navigation
plotter.Children.Remove(plotter.MouseNavigation);
// ZoomX when wheeling mouse
private void plotter_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
if (!e.Handled)
{
Point mousePos = e.GetPosition(this);
Point zoomTo = mousePos.ScreenToViewport(plotter.Viewport.Transform);
double zoomSpeed = Math.Abs(e.Delta / Mouse.MouseWheelDeltaForOneLine);
zoomSpeed *= 1.2;
if (e.Delta < 0)
{
zoomSpeed = 1 / zoomSpeed;
}
plotter.Viewport.SetChangeType(ChangeType.Zoom);
plotter.Viewport.Visible = plotter.Viewport.Visible.ZoomX(zoomTo, zoomSpeed);
plotter.Viewport.SetChangeType();
e.Handled = true;
}
}

How to make the ToolTip follow the mouse?

I want the ToolTip to follow my mouse moving over one control. For example, let's take a panel. When mouse location is inside the Rectangle(100, 100, 50, 50) I want ToolTip to be visible and always on the right down of the mouse. When it's outside this rectangle, I want ToolTip to be invisible.
I tried to do this like that:
ToolTip toolTip = new ToolTip();
int x, y;
protected override void OnMouseMove(MouseEventArgs e)
{
if ((x == e.X) && (y == e.Y) && (new Rectangle(100, 100, 50, 50).Contains(e.Location))
toolTip.Show("some text", this, x + 10, y + 10);
else
{
x = e.X;
y = e.Y;
toolTip.Hide(this);
}
}
But there's a problem - when my toolTip shows up - it gets the focus and after that OnMouseMove(MouseEventArgs e) doesn't work any more. I tried to get the focus to the panel in the end of that function, but it doesn't work. I also tried some tricks with OnMouseHover, but it was the same effect.
Don't use a ToolTip for that - if the Panel is drawn on, draw your own ToolTip; otherwise, use a Panel and respond to MouseMove events from both, but ignore e.Location and instead use System.Windows.Forms.Cursor.Position and PointToClient.

Categories

Resources