Windows forms move window

MoveWindow function (winuser.h)

Changes the position and dimensions of the specified window. For a top-level window, the position and dimensions are relative to the upper-left corner of the screen. For a child window, they are relative to the upper-left corner of the parent window’s client area.

Syntax

Parameters

A handle to the window.

The new position of the left side of the window.

The new position of the top of the window.

The new width of the window.

The new height of the window.

Indicates whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

If the bRepaint parameter is TRUE, the system sends the WM_PAINT message to the window procedure immediately after moving the window (that is, the MoveWindow function calls the UpdateWindow function). If bRepaint is FALSE, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing.

Move window without border

How do I move a window that does not have a border. There is no empty space on the application, all that is available is a webbrowser and a menustrip. I would like the users to be able to move the window by dragging the menu strip. How do I code this? I have tried a few code blocks I have found online, but none of them worked.

8 Answers 8

This Code Project article should help you accomplish this. I’ve used this myself with no problems. This is the jist of it:

This will basically «trick» the window manager into thinking that it is grabbing the title bar of the winform.

To apply it to your project, just use the MouseDown event from the MenuStrip.

Here is the .Net Way

Just put the start point into an 2D Array like this:

You can fake your menustrip, for example using a panel with a label instead. And then you can handle this manually: when the user clicks the label, a popup menu will open, and when the user drags the label, the window will move. But I would advise against such workarounds, because it’s not a standard GUI behavior, and you might get your users confused.

Читайте также:  Python3 dev mac os

I haven’t tried it, but if you can handle the «OnMouseDown» and «onMouseUp» events on the menu bar:

  • On mouse down — Move the window according to the mouse movement
  • Stop tracking the mouse movement on mouse up, or mouse out

If you are using a Panel you have to add this in the

YourForm.Designer.cs

and this in the

YourForm.cs

Mbithi Kioko is on the right track but i would do it this way.

I had to use System.Runtime.InteropServices.DllImportAttribute — just thought I would comment and let you all know.

Not the answer you’re looking for? Browse other questions tagged c# move menustrip borderless or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

C# Windows Forms — move element when resizing the form

I need some help with a responsive form in WinForms. I have a Checkbox at Position xy. When i resize the form to a smaller size, this Checkbox should move to the left side, closer to the other elements.

You can see it on the pictures I made. I do not know, which Property I have to change there, to make this happen. The checkbox to move is marked by the red box.

When I enlarge the form, this element has to stay at its default position. When I reduce it, the checkbox has to move to the left. When I enlarge it again, the Checkbox has to move back to its default position.

3 Answers 3

You need to set the Anchor property:

This states that the right border of the control should always have the same distance to the right border of the containing control (your form).

You can also set this property in the designer window.

Update after your clarification:

This is a complicated situation. One solution I found (though I’m not sure it’s the best) could be to use a Panel to contain the CheckBox .

  • Place the Panel at the left-most position the CheckBox could have
  • Set the size of the Panel so that the right border is at the right-most border (plus a few pixels) the CheckBox should have
  • Set the MaximumSize of the Panel to this exact size
  • Set the Anchor property of the Panel to Top | Left | Right
  • Place the CheckBox inside the Panel at the right edge
  • Set the Anchor property of the CheckBox to Top | Right

Now, if you enlarge the Form , the Panel keeps its size because of the MaximumSize value.

If you shrink the Form , the Panel will also shrink because of its AnchorStyle.Right . The CheckBox moves to the left because of its own AnchorStyle.Right .

When you enlarge the Form again, the Panel also grows, but only it reaches its MaximumSize again. The CheckBox moves to the right to keep up with the growing Panel .

Читайте также:  Скорость wifi windows режется

Hope this does what you want. I can later add screenshots if necessary.

How do you prevent a windows from being moved?

How would i go about stopping a form from being moved. I have the form border style set as FixedSingle and would like to keep it this way because it looks good in vista 🙂

15 Answers 15

Take a look at this link. You might be interested in option #3. It will require you to wrap some native code, but should work. There’s also a comment at the bottom of the link that shows an easier way to do it. Taken from the comment (can’t take credit for it, but I’ll save you some searching):

You can set the FormBorderStyle property of the Form to None

I found this to stop the form from moving (its in c#)

Try to override WndProc:

It’s not all pretty (there is some flashing going on when you try to move the form), but you can use the LocationChanged property to keep the form where you want it:

Out of curiousity; why would you want to do this?

In Windows, the WS_CAPTION style is the non-client area that allows your window to be moved with a mouse. So the easiest way to do what you want is to remove this style from your window.

However, if you need to have a caption and still achieve what you want, then the next style would be to capture the WM_NCHITTEST message and check for HTCAPTION. If the code is HTCAPTION, return NTNOWHERE instead. This will prevent the default window procedure from executing the default move window thing.

It’s not a good practice to make your form immovable. I’d think agfain about it if I were you.
Anyway, you can do this by overridding the WinProc to disable the [Move] menuitem from the system menu.

Also, you can handle OnMove event of your form. But I think this will cause some flickering:

Практическое руководство. Перемещение объектов ToolStripMenuItem How to: Move ToolStripMenuItems

Во время разработки можно перемещать целые меню верхнего уровня и их пункты меню в другое место MenuStrip . At design time, you can move entire top-level menus and their menu items to a different place on the MenuStrip. Можно также перемещать отдельные пункты меню между меню верхнего уровня или изменять положение пунктов меню в меню. You can also move individual menu items between top-level menus or change the position of menu items within a menu.

Перемещение меню верхнего уровня и его пунктов меню в другое расположение верхнего уровня To move a top-level menu and its menu items to another top-level location

Щелкните и удерживайте левую кнопку мыши в меню, которое требуется переместить. Click and hold down the left mouse button on the menu that you want to move.

Перетащите точку вставки в меню верхнего уровня, расположенное перед предполагаемым новым расположением, и отпустите левую кнопку мыши. Drag the insertion point to the top-level menu that is before the intended new location and release the left mouse button.

Выбранное меню перемещается справа от точки вставки. The selected menu moves to the right of the insertion point.

Читайте также:  Как ускорить работу линукс минт 20

Перемещение меню верхнего уровня и его пунктов меню в раскрывающийся список To move a top-level menu and its menu items to a drop-down location

Щелкните правой кнопкой мыши меню, которое нужно переместить, и нажмите клавиши CTRL + X, либо щелкните меню в контекстном меню и выберите команду Вырезать . Left-click the menu that you want to move and press CTRL+X, or right-click the menu and select Cut from the shortcut menu.

В меню назначения верхнего уровня щелкните левой кнопкой мыши пункт меню над предполагаемым новым расположением и нажмите клавиши CTRL + V или щелкните правой кнопкой пункт меню над нужным новым расположением и выберите в контекстном меню команду Вставить . In the destination top-level menu, left-click the menu item above the intended new location and press CTRL+V, or right-click the menu item above the intended new location and select Paste from the shortcut menu.

Вырезанное меню вставляется после выбранного пункта меню. The menu that you cut is inserted after the selected menu item.

Перемещение пункта меню в меню с помощью редактора коллекции элементов To move a menu item within a menu using the Items Collection Editor

Щелкните правой кнопкой мыши меню, содержащее пункт меню, который требуется переместить. Right-click the menu that contains the menu item you want to move.

В контекстном меню выберите изменить дропдовнитемс. From the shortcut menu, choose Edit DropDownItems.

В редакторе коллекции элементов щелкните элемент меню, который нужно переместить, левой кнопкой мыши. In the Items Collection Editor, left-click the menu item you want to move.

Нажмите клавиши со СТРЕЛКАми вверх и вниз, чтобы переместить пункт меню в меню. Click the UP and DOWN ARROW keys to move the menu item within the menu.

Нажмите кнопку ОК. Click OK.

Перемещение пункта меню в меню с помощью клавиатуры To move a menu item within a menu using the keyboard

Нажмите и удерживайте клавишу ALT. Press and hold down the ALT key.

Щелкните и удерживайте левую кнопку мыши в пункте меню, которое требуется переместить. Click and hold the left mouse button on the menu item that you want to move.

Перетащите элемент меню в новое место и отпустите левую кнопку мыши. Drag the menu item to the new location and release the left mouse button.

Перемещение пункта меню в другое меню To move a menu item to another menu

Щелкните правой кнопкой мыши пункт меню, который нужно переместить, и нажмите клавиши CTRL + X, либо щелкните элемент меню в контекстном меню и выберите Вырезать . Left-click the menu item that you want to move and press CTRL+X, or right-click the menu item and choose Cut from the shortcut menu.

Щелкните левой кнопкой мыши меню, в котором будет содержаться вырезанный пункт меню. Left-click the menu that will contain the menu item that you cut.

Щелкните левой кнопкой мыши пункт меню, который находится перед предполагаемым новым расположением, и нажмите клавиши CTRL + V или щелкните элемент меню, находящееся перед нужным новым расположением, и выберите в контекстном меню команду Вставить . Left-click the menu item that is before the intended new location and press CTRL+V, or right-click the menu item that is before the intended new location and select Paste from the shortcut menu.

Вырезанный пункт меню вставляется после выбранного пункта меню. The menu item that you cut is inserted after the selected menu item.

Оцените статью