[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

How to prevent window moving

Posted on 2000-01-26
8
Medium Priority
?
518 Views
Last Modified: 2013-11-20
I want prevent the user from being able to move the main (and only) window of my application by grabbing the caption bar.  I can't figure out how to do this.

In the PreCreate method of the MainFrm
is set the CREATESTRUCT as follows:

      cs.style = WS_OVERLAPPED | WS_CAPTION | FWS_ADDTOTITLE| WS_SYSMENU | WS_MAXIMIZE;

In the InitiInstance of the application, I set:
 m_pMainWnd->ShowWindow(SW_SHOWMAXIMIZED);

This creates the window that I want: a maximized window with a title and an X to close it, with no min or max box.  It also creates a system menu that I don't need but I found that if I remove that, the top right X to close the application disappears.  (I can live with the system menu).

Despite my SHOW_MAXIMIZED, the user can still grab the window by the caption bar and move it around.  I would like to prevent that from happening.  I also want to keep the window maximized and not allow a min or restore box.

Everything I've thought of doesn't work.  Any suggestions?
0
Comment
Question by:NormB062799
  • 5
  • 3
8 Comments
 
LVL 5

Accepted Solution

by:
Wyn earned 280 total points
ID: 2389685
Using WM_WINDOWPOSCHANGING
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2389702
Handle the WM_WINDOWPOSCHANGING message and add the SWP_NOMOVE to the "flags" field:
((LPWINDOWPOS) lParam)->flag = ((LPWINDOWPOS) lParam)->flag|SWP_NOMOVE;

 
0
 

Author Comment

by:NormB062799
ID: 2390367
So why doesn't this work?

void CMainFrame::OnWindowsPosChanging(WINDOWPOS* lpwndpos)
{
      lpwndpos->flags = lpwndpos->flags | SWP_NOMOVE;
      CFrameWnd::OnWindowPosChanged(lpwndpos);
}
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:Wyn
ID: 2391689
it's

OnWindowPosChanging,not OnWindowsPosChanging.

e.g:
void CMainFrame::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
lpwndpos->flags = lpwndpos->flags | SWP_NOMOVE;
CFrameWnd::OnWindowPosChanging(lpwndpos);
}

Sure it works.I have tested.

Free to ask if you have probs.

Work it out.

Regards
Wyn


0
 

Author Comment

by:NormB062799
ID: 2391740
I corrected my spelling error and had great hopes this would work.  Unfortunately not.  I can still grab the application window from the caption bar and move it all over the screen.  There must must something else I'm doing wrong.  I have this code in MainFrm.cpp.  Is that where it belongs?

As an alternative, I tried using WS_OVERLAPPEDWINDOW instead of WS_OVERLAPPED.  That seems to prevent the movement if I maximize the window.  However, it introduces a minimize and restore button that I don't want and can't seem to remove without creating further problems.

Learning this MFC is like trying to climb the North face of Mt Everest!
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2392904
Okay,let me ask:
1:
have you declared the
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)
in your mainframe .h
?
2:
have you added
ON_WM_WINDOWPOSCHANGING( )
in your mainframe .cpp message macro
?
3:
have you defined your function like this in mainframe.cpp:

void CMainFrame::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
lpwndpos->flags = lpwndpos->flags | SWP_NOMOVE;
CFrameWnd::OnWindowPosChanging(lpwndpos); }
?

If all you have properly done ,it should work ,check it carefully!!

>>There must something else I'm doing >>wrong

not likely,check above carefully.
btw,it's CFrameWnd::OnWindowPosChanging,not changed.I mention it 'cause i find your past code used the latter.


If still dont work.Mail your code and I correct it for you but I think if you follow above,it should work!!!

MFC? Yes ,I do think so but it's shortcut for efficiency without losing  control...
Best Regards
Wyn.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2392908
sorry,the first question is
declare this in mainframe.h
afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos);

:-)
0
 

Author Comment

by:NormB062799
ID: 2395321
Finally!  I feel like a real dummy with this.  I had neglected #2, adding ON_WM_WINDOWPOSCHANGING( )
to the message macro.  Thanks for sticking with me on what should be a rather elementary problem for you.

Actually, the code as it exists caused another problem.  It seems to prevent the app window from positioning itself at 0,0 as it did before.  The app seems to start at any old place.  Of course, you can't just use SetWindowPos since it's intercepted by the new message handler.  I solved it by using a flag like this:


void CMainFrame::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
      if (!juststarted)
            lpwndpos->flags = lpwndpos->flags | SWP_NOMOVE;
      else
            juststarted = 0;
      CFrameWnd::OnWindowPosChanging(lpwndpos);
}

In OnCreate I set juststarted = 1 then immediately call SetWindowPos to set the window at the origin.  If there is a more elegant way, I'd appreciate hearing about it.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question