00 Win32 SDK程序中拖动Thin边框窗口来改变窗口大小

Post date: 2012/4/24 上午 06:49:56

主要需要添加处理WM_NCHITTEST,WM_SETCURSOR和WM_NCLBUTTONDOWN消息

[cpp] view plaincopy

  1. case WM_NCHITTEST:
  2. POINT point;
  3. point.x = LOWORD(lParam);
  4. point.y = HIWORD(lParam);
  5. RECT rect;
  6. GetWindowRect(hWnd,&rect);
  7. if(point.x <= rect.left+5)
  8. {
  9. return HTLEFT;
  10. }
  11. else if(point.x >= rect.right-5)
  12. {
  13. return HTRIGHT;
  14. }
  15. else if(point.y <= rect.top+5)
  16. {
  17. return HTTOP;
  18. }
  19. else if(point.y >= rect.bottom-5)
  20. {
  21. return HTBOTTOM;
  22. }
  23. else if(point.x <= rect.left+10 && point.y <= rect.top+10)
  24. {
  25. return HTTOPLEFT;
  26. }
  27. else if(point.x >= rect.right-10 && point.y <= rect.top+10)
  28. {
  29. return HTTOPRIGHT;
  30. }
  31. else if(point.x <= rect.left+10 && point.y >= rect.bottom-10)
  32. {
  33. return HTBOTTOMLEFT;
  34. }
  35. else if(point.x >= rect.right-10 && point.y >= rect.bottom-10)
  36. {
  37. return HTBOTTOMRIGHT;
  38. }
  39. return ::DefWindowProc(hWnd, message, wParam, lParam);
  40. case WM_SETCURSOR:
  41. {
  42. UINT nHitTest = LOWORD(lParam);
  43. switch(nHitTest)
  44. {
  45. case HTTOP:
  46. case HTBOTTOM:
  47. SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
  48. break;
  49. case HTLEFT:
  50. case HTRIGHT:
  51. SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
  52. break;
  53. case HTTOPLEFT:
  54. case HTBOTTOMRIGHT:
  55. SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
  56. break;
  57. case HTTOPRIGHT:
  58. case HTBOTTOMLEFT:
  59. SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
  60. break;
  61. case HTCAPTION:
  62. case HTSYSMENU:
  63. case HTMENU:
  64. case HTCLIENT:
  65. default:
  66. SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
  67. break;
  68. }
  69. }
  70. return TRUE;
  71. case WM_NCLBUTTONDOWN:
  72. switch(wParam)
  73. {
  74. case HTTOP:
  75. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, lParam);
  76. break;
  77. case HTBOTTOM:
  78. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, lParam);
  79. break;
  80. case HTLEFT:
  81. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, lParam);
  82. break;
  83. case HTRIGHT:
  84. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, lParam);
  85. break;
  86. case HTTOPLEFT:
  87. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, lParam);
  88. break;
  89. case HTTOPRIGHT:
  90. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, lParam);
  91. break;
  92. case HTBOTTOMLEFT:
  93. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, lParam);
  94. break;
  95. case HTBOTTOMRIGHT:
  96. SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, lParam);
  97. break;
  98. default:
  99. break;
  100. }
  101. return DefWindowProc(hWnd, message, wParam, lParam);