Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hey guys,

I'm working in VB.net 2010, and I'm trying to prevent a window from moving less than 140pixels from the top of the screen.

I'm trying to use Wndproc but so far I've failed.

I've tried modifiying the X,Y paramaters in the following messages:

WM_SYSCOMMAND with a wparam of SC_MOVE
WM_MOVING
WM_WINDOWPOSCHANGING
WM_GETMINMAXINFO

I've also tried using the form methods for Move, Resize, and Shown as suggested below, but they cause the form to flash between the mouse cursor location and my boundary(140px).

The project consists of multiple forms. One form is fixed at the top of the screen with a height of 140, set to always be on top (lets call it the navigation bar). I'm trying to prevent the other forms from being moved behind this "navigation bar".

What is the correct way to prevent a form from moving in a certain direction.

Thanks!
Posted
Updated 6-Jun-12 16:22pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Jun-12 18:54pm    
WPF, Forms? Anything else? Tag it.
--SA

1 solution

To do what your try to do, you apparently have to use P/Invoke. By doing this, you totally loose platform compatibility. Did you know that your application, even the Forms application (but not WPF) if written accurately, can run on many systems without recompilations, such as Linux, Mac OS X and more? You are going to loose it. No need.

All you need is using the property Top (or whatever) and fixing position/size; for example, in the event handler of the event System.Windows.Forms.Form.Shown:
VB
Me.Shown += Function(sender, eventArgs)
    If Me.Top < 140 Then
        Me.Top = 140
    End If
End Function


Something like that. Try to forget about using Windows API unless this is absolutely needed. You probably will be amazed how rarely it is the case.

[EDIT]

And I agree with AspNetDev who reminded us that it would require adding appropriate handlers to events like SizeChanged and Move. Please see our discussion in comments below.

—SA
 
Share this answer
 
v5
Comments
AspDotNetDev 6-Jun-12 19:10pm    
Probably also a good idea to hook up to a few more events, such as move and resize. Here is some info for the OP on the move event: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.move(v=vs.100).aspx
Sergey Alexandrovich Kryukov 6-Jun-12 19:13pm    
Sure, but the whole idea to keep a window away from certain area is a sign of poop UI design to me...
--SA
AspDotNetDev 6-Jun-12 19:34pm    
Maybe, maybe not. Maybe they are building screen capture software and have some reason not to have the window near the edge of the screen, or maybe they are building a remote desktop utility and want to avoid having the window near the hover location that causes the toolbar to appear, or maybe they are building a puzzle game out of windows that involves the user placing windows in certain configurations without violating certain rules (e.g., not within 140px of the edge of the screen). I don't think we can make assumptions like this being poop UI design when we have no idea what is being designed.
Sergey Alexandrovich Kryukov 6-Jun-12 20:12pm    
Well, I have to agree with you. I just said "a sign of", not a sure fact...
--SA
Sergey Alexandrovich Kryukov 6-Jun-12 20:16pm    
I credited your useful note in my answer, after [EDIT].
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900