I want to make a simple arcanoid. misrepresented the problem that I can not catch the moment when the left edge of the button to the left crane hits the screen.
private void button1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.A)
{
if (ActiveForm.Left - button1.Left<10)
{
button1.Left -= 10;
}
}
if (e.KeyData == Keys.D)
{
button1.Left += 10;
}
}
I think the problem is in your if for the button1.left code.
I suppose that the ActiveForm is the form showing the game itself, and you want to move your bar to the left until it hits the left side of ActiveForm.
Take a look at this picture, basically the Form.Left is the distance between the left side of your screen and the form itself:
Instead of checking the bar position with the Form.Left, check it against "0 - bar.width", the left side of your form is always 0, so you can move the bar to the left as far as it's value is bigger than the width of the bar (bigger than 10 I presume).
Related
Inside my page, I have a frame that loads another page. I am using this to detect if they want to move the frame around the screen:
private void Frame_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
}
}
How do I move the frame itself though? I have been trying to look for it and I cant seem to find the variables that control its position.
Try positioning using the frame's margin:
AppFrame.Margin = new Thickness(Mouse.X, Mouse.Y, 0, 0);
Alternatively, some frames are able to be moved with their Left and Top variables.
frame.Left = Mouse.X; // or whatever
frame.Top = Mouse.Y; // or whatever
In order to move it proportionally to the mouse, record the mouse and frame's original positions when the mouse was first dragged, and referenced them when positioned:
frame.Left = originalFrameX + (Mouse.X - originalMouseX);
frame.Top = originalFrameY + (Mouse.Y - originalMouseY);
I'm currently making a borderless form with a Doubleclick event to maximize form. But I realized that the form wouldn't maximize on the two other screens, only my main middle.
So my code is currently:
private void Form1_DoubleClick(object sender, EventArgs e)
{
if ((this.Height == Screen.PrimaryScreen.WorkingArea.Height) && (this.Width == Screen.PrimaryScreen.WorkingArea.Width))
{
this.Width = 534;
this.Height = 600;
CenterToScreen();
}
else
{
this.Height = Screen.PrimaryScreen.WorkingArea.Height;
this.Width = Screen.PrimaryScreen.WorkingArea.Width;
this.Location = Screen.PrimaryScreen.WorkingArea.Location;
}
}
It might look weird, but I use it to not cover the taskbar.
I need a code like this to dock it to the side, and use it to calculate where the form should be. Looking like this: half right screen dock
when I click one of those 9 buttons, it will dock the screen in different places of the screen. In corner, half of the screen or in the middle.
I tried using a code where the form would detect which screen it was on, and using that again to maximize the form on that screen, but I got a bunch of red lines, and it didn't work in the end.
I have 3 monitors.
Please help.
You hardcoded it to primary screen, which is the screen with the task bar. To allow other screens get the screen the form is currently on an adjust to that.
private void Form1_DoubleClick(object sender, EventArgs e)
{
if ((this.Height == Screen.FromControl(this).WorkingArea.Height) && (this.Width == Screen.FromControl(this).WorkingArea.Width))
{
this.Width = 534;
this.Height = 600;
CenterToScreen();
}
else
{
this.Height = Screen.FromControl(this).WorkingArea.Height;
this.Width = Screen.FromControl(this).WorkingArea.Width;
this.Location = Screen.FromControl(this).WorkingArea.Location;
}
}
I am making a windows form animation where a animation of a character is walking left and then walk right once he reached the end of the windows form.
and i have to put a play/pause button for the animation to stop and continue where he left off
I have 16 frames each of the character walking right and left
I would like to ask how to put a boundary on the picturebox and how to get the picturebox to change to the walking right animation once it reach the left edge of the windows form and change the walking left animation once it reach the right edge of the windows form
public partial class Form1 : Form
{
//Declare a new integer for frame and set it to 1
int frame = 1;
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
//Increase the frame per tick
frame++;
//Loop : If the frame exceeds 16, set the frame back to 1
if (frame > 16)
{
frame = 1;
}
//REtrieve the image fromfile base on the value of the ineger "frame"
pictureBox1.Image = Image.FromFile(Application.StartupPath +
"\\left" + frame + ".png");
int x = pictureBox1.Location.X;
int y = pictureBox1.Location.Y;
x -= 5;
//else if (e.KeyCode == Keys.Left) x -= 1;
pictureBox1.Location = new Point(x, y);
}
PictureBox has a border property. Check if it works for you.
You can check if PictureBox Left matches Forms Left that would mean it needs to move right and change the image on this condition.
You will need 2 different images 1 directed to left and 1 directed to right...
create an extra variable stating direction
then you can turn the image direction if the folowing criteria is met
if direction = right and picture.location >= (how far you want it to go probably border)
thendirection = leftchange to the left directed image change your movement variable to negative
if direction = left and picture.location <= 0
then direction = rightchange to the rightdirected image change your movement variable to positive
To detect when the control has reached the boundaries, you may check if the Location.X property + Width is equal to or greater than the PictureBox container (in this case, Form)'s width.
if(pictureBox1.Location.X + pictureBox1.Width >= pictureBox1.parent.Width)
{
direction = "left";//reached the end so go left
Of course you can easily test if we're at the starting position by checking if pictureBox1.Location.X == 0
i have one panel and i put a flow layout panel in the main panel and flow layout panel has many images. my UI looks like this
now i got a code which scroll the container in the panel. i mean the flow layout will scroll in main panel when i will place my mouse at the left or right most area on the main panel. their code is working i checked but when i place their code in my project then that is not working, i means scrolling is not working when i place my mouse at the left or right most area on the main panel.
here i am attaching main code which causes to scroll the flow layout panel inside in main panel
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
int _myval = 5;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (e.Y < metroPanel1.Top || e.Y > metroPanel1.Top + metroPanel1.Height) return;
if (e.X <= metroPanel1.Left && e.X >= metroPanel1.Left - 40)
{
if (metroPanel1.HorizontalScroll.Value <= _myval)
{
metroPanel1.HorizontalScroll.Value = _myval;
}
else
{
metroPanel1.HorizontalScroll.Value -= _myval;
}
}
if (e.X <= (metroPanel1.Left + metroPanel1.Width + 40) && e.X >= (metroPanel1.Left + metroPanel1.Width))
{
metroPanel1.HorizontalScroll.Value += _myval;
}
}
i just do not understand this value 40 used here if (e.X <= metroPanel1.Left && e.X >= metroPanel1.Left - 40)
i guess i need to use different value rather than 40 but i used 10 & 20 but did not work.
here is my full project link from where anyone can download and see what is wrong in my routine which prevent the scrolling. here is the link https://onedrive.live.com/embed?cid=C4A6F16F34D7540A&resid=C4A6F16F34D7540A%21134&authkey=AM5Fq2gcFLtcw_A
so it is my request that please some one see my code and guide me what i need to change in code and why. thanks
When you want to scroll dont use
metroPanel1.HorizontalScroll.Value -= _myval;
or
metroPanel1.HorizontalScroll.Value += _myval;
but instead
_myval -= 5;
or _myval += 5;
metroPanel1.HorizontalScroll.Value = _myval;
metroPanel1.Refresh();
valter
Yes.
Yes, programmatically scrolling a FlowLayoutPanel is not working, if the Scrollbars are not shown, due to its AutoScroll being off.
Neither setting HorizontalScroll.Value (in any way) nor using
[DllImport("user32.dll")]
static extern int SetScrollPos(IntPtr hWnd, int nBar, int nPos, bool bRedraw);
will do anything.
Not even doing this has any effect:
flowLayoutPanel1ScrollControlIntoView(someControlThatsOutOfSight);
So, as far as I can tell: Without active Scrollbar(s) no scrolling a FlowLayoutPanel!
The containing Panel can however indeed be scrolled the way valter shows. However my first tests showed even when using a double-buffered Panel subclass a terrible flicker..
Update:
I can't get the Panel to scroll reliably. I have checked up on the link and found that the behaviour is really much simpler than anything I had written: No up down scrolling, no edge detection, no speed detection, no direction detection. In fact there are two scroll zones and when the mouse is in one, it scrolls.. It also relies on the 'constant firing of the mousemove event bug', which only works for me when I don't want it..!
So, here is a solution that implements this behaviour and is, of course much shorter, than the original code.
I include a screenshot to show you the layout: You need two Panels and the FlowLayoutPanel.
The outer Panel is a little wider so that its right and left part work as the scroll zones.
The inner Panel contains the FLP; the FLP has AutoSize=true.
There is a timer scrollTimer with a fast Intervall of 5-10ms.
Here is the code:
int speed = 10;
int delta = 0;
private void panOuter_MouseMove(object sender, MouseEventArgs e)
{
delta = e.X < panOuter.Width / 2 ? speed : -speed;
scrollTimer.Start();
}
private void panOuter_MouseLeave(object sender, EventArgs e)
{
scrollTimer.Stop();
}
private void scrollTimer_Tick(object sender, EventArgs e)
{
int newLeft = FLP.Left + delta;
int alpha = panInner.ClientRectangle.Width - FLP.ClientRectangle.Width;
if (newLeft > 0) { newLeft = 0; scrollTimer.Stop(); }
else if ( newLeft < alpha)
{ newLeft = alpha; scrollTimer.Stop(); }
FLP.Left = newLeft;
}
That's practically all you need. Only in case you Dock or Anchor the outer Panel you should script the Resize event to keep the inner Panel centered!
I am a new C# developer and I want to create a hotspot in an image that I put in my winform application. I followed the solution posted HERE, but I did not know where I should put the coordinates to make this method works:
protected override void OnMouseMove(MouseEventArgs mouseEvent)
{
string X = mouseEvent.X.ToString();
string Y = mouseEvent.Y.ToString();
}
Where should I put the coordinates? I have two coordinates (X,Y): 110, 45
Hotspot I feel should be a small rectangular area rather than just a coordinate. Suppose you want it to be a small square area of width 20 then you would write something like this:
EDIT:
Suppose you have a PictureBox on your form called PictureBox1 and you want that a small rectangle of say 20x20 size starting from Top-Left corner of the picturebox become a hotspot (i.e. when you take mouse over it you would see a HAND cursor) then on the MouSeMove event of the PictureBox write this:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X > 0 && e.X < 20 && e.Y > 0 && e.Y < 20)
this.Cursor = Cursors.Hand;
else
this.Cursor = Cursors.Default;
}
Please remember, we are just showing the Hand Cursor to denote a hotspot we have not yet handled a Click for that matter, to make it really a web kind hotspot. If you want to do something on Click, try using the MouseUp event, in the MouseUp event the same IF clause as above would give you the condition that user has clicked on the hotspot region.