How to prevent window moving

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?
NormB062799Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WynCommented:
Using WM_WINDOWPOSCHANGING
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WynCommented:
Handle the WM_WINDOWPOSCHANGING message and add the SWP_NOMOVE to the "flags" field:
((LPWINDOWPOS) lParam)->flag = ((LPWINDOWPOS) lParam)->flag|SWP_NOMOVE;

 
0
NormB062799Author Commented:
So why doesn't this work?

void CMainFrame::OnWindowsPosChanging(WINDOWPOS* lpwndpos)
{
      lpwndpos->flags = lpwndpos->flags | SWP_NOMOVE;
      CFrameWnd::OnWindowPosChanged(lpwndpos);
}
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

WynCommented:
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
NormB062799Author Commented:
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
WynCommented:
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
WynCommented:
sorry,the first question is
declare this in mainframe.h
afx_msg void OnWindowPosChanging(WINDOWPOS* lpwndpos);

:-)
0
NormB062799Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.