zoukankan      html  css  js  c++  java
  • MFC Windows 程序设计>实现客户区拖动

    One problem with a window without a title bar is that it can't be repositioned with the mouse. Windows are dragged by their title bars, and when there's no title bar, the user has nothing to grab onto. Clock solves this little dilemma by playing a trick with the window's WM_NCHITTEST handler so that the window can be dragged by its client area, a feature Windows programmers call client-area drag.

    In Windows, every mouse message is preceded by a WM_NCHITTEST message containing screen coordinates identifying the cursor location. The message is normally handled by ::DefWindowProc, which returns a code that tells Windows what part of the window the cursor is over. Windows uses the return value to decide what type of mouse message to send. For example, if the left mouse button is clicked over the window's title bar, ::DefWindowProc's WM_NCHITTEST handler returns HTCAPTION and Windows sends the window a WM_NCLBUTTONDOWN message. If ::DefWindowProc returns HTCLIENT instead, Windows converts the cursor coordinates from screen coordinates to client coordinates and passes them to the window in a WM_LBUTTONDOWN message.

    The fact that an application sees mouse messages in raw form makes for some interesting possibilities. The following OnNcHitTest handler implements client-area drag by fooling Windows into thinking that the mouse is over the title bar when in fact it's over the window's client area:

     

    With this OnNcHitTest handler in place, a window is as easily dragged by its client area as by its title bar. And it works even if the window doesn't have a title bar. Try it: click the left mouse button in Clock's client area, and move the mouse with the button held down. The window should go wherever the mouse goes.

    Clock uses an OnNcHitTest handler similar to the one shown above. The only difference is that Clock verifies that the left mouse button is down before replacing an HTCLIENT return code with HTCAPTION so that other mouse messages—particularly right-button mouse messages that precede WM_CONTEXTMENU messages—will get through unscathed:

     

    The call to ::GetAsyncKeyState checks the left mouse button and returns a negative value if the button is currently down.

  • 相关阅读:
    .net C# 利用Session防重复点击防重复提交
    子报表修改后需要重新导入,0.00显示.00的调整方法
    svn错误 svnserve.conf:12: Option expected解决办法
    mysql远程访问 登录ERROR 1130: is not allowed to connect to this MySQL server解决办法
    phpmyadmin新加用户登陆不了,测试解决方案。
    自己封装的php Curl并发处理,欢迎提出问题优化。
    js和php计算图片自适应宽高算法实现
    jquery获取浏览器宽高
    swftools中的pdf2swf转换Error overflow ID 65535 解决办法
    php 根据ip获取城市以及网络运营商名称(利用qqwry.dat)
  • 原文地址:https://www.cnblogs.com/marryZhan/p/2213914.html
Copyright © 2011-2022 走看看