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


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

    版本1.0(本版本有问题,右下角不响应消息)

    case WM_NCHITTEST:
                POINT point;
                point.x = LOWORD(lParam);
                point.y = HIWORD(lParam);
                RECT rect;        
                GetWindowRect(hWnd,&rect);
                if(point.x <= rect.left+5)
                {
                     return HTLEFT;
                }
                else if(point.x >= rect.right-5)
                {
                    return HTRIGHT;
                }
                else if(point.y <= rect.top+5)
                {    
                    return HTTOP;
                }
                else if(point.y >= rect.bottom-5)
                {
                    return HTBOTTOM;
                }
                else if(point.x <= rect.left+10 && point.y <= rect.top+10)
                {
                    return HTTOPLEFT;
                }
                else if(point.x >= rect.right-10 && point.y <= rect.top+10)
                {
                    return HTTOPRIGHT;
                }
                else if(point.x <= rect.left+10 && point.y >= rect.bottom-10)
                {
                    return HTBOTTOMLEFT;
                }
                else if(point.x >= rect.right-10 && point.y >= rect.bottom-10)
                {
                    return HTBOTTOMRIGHT;
                }
                return ::DefWindowProc(hWnd, message, wParam, lParam);
    
            case WM_SETCURSOR:
                {
                    UINT nHitTest = LOWORD(lParam);
                    switch(nHitTest)
                    {
                    case HTTOP:
                    case HTBOTTOM:
                        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
                        break;
                    case HTLEFT:
                    case HTRIGHT:
                        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
                        break;
                    case HTTOPLEFT:
                    case HTBOTTOMRIGHT:
                        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
                        break;
                    case HTTOPRIGHT:
                    case HTBOTTOMLEFT:
                        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
                        break;
                    case HTCAPTION:
                    case HTSYSMENU:
                    case HTMENU:
                    case HTCLIENT:
                    default:
                        SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
                        break;
                    }
                }
                return TRUE;
    
            case WM_NCLBUTTONDOWN:
                switch(wParam)
                {
                case HTTOP:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOP, lParam);
                    break;
                case HTBOTTOM:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOM, lParam);
                    break;
                case HTLEFT:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_LEFT, lParam);
                    break;
                case HTRIGHT:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_RIGHT, lParam);
                    break;
                case HTTOPLEFT:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPLEFT, lParam);
                    break;
                case HTTOPRIGHT:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_TOPRIGHT, lParam);
                    break;
                case HTBOTTOMLEFT:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMLEFT, lParam);
                    break;
                case HTBOTTOMRIGHT:
                    SendMessage(hWnd, WM_SYSCOMMAND, SC_SIZE | WMSZ_BOTTOMRIGHT, lParam);
                    break;
                default:
                    break;
                }
                return DefWindowProc(hWnd, message, wParam, lParam);

    版本2.0(修正版本1.0的问题)

    	switch (message)
    	{	
    	case WM_NCHITTEST:
    		POINT point;
    		point.x = LOWORD(lParam);
    		point.y = HIWORD(lParam);
    		RECT rect;        
    		GetWindowRect(&rect);
    		if(point.x <= rect.left+5)
    		{
    			return HTLEFT;
    		}
    		else if(point.x >= rect.right-5 && point.y < rect.bottom-10)
    		{
    			return HTRIGHT;
    		}
    		else if(point.y <= rect.top+5)
    		{    
    			return HTTOP;
    		}
    		else if(point.y >= rect.bottom-5&& point.x < rect.right-10)
    		{
    			return HTBOTTOM;
    		}
    		else if(point.x <= rect.left+10 && point.y <= rect.top+10)
    		{
    			return HTTOPLEFT;
    		}
    		else if(point.x >= rect.right-10 && point.y <= rect.top+10)
    		{
    			return HTTOPRIGHT;
    		}
    		else if(point.x <= rect.left+10 && point.y >= rect.bottom-10)
    		{
    			return HTBOTTOMLEFT;
    		}
    		else if(point.x >= rect.right-10 && point.y >= rect.bottom-10)
    		{
    			return HTBOTTOMRIGHT;
    		}
    
    	case WM_SETCURSOR:
    		{
    			UINT nHitTest = LOWORD(lParam);
    			switch(nHitTest)
    			{
    			case HTTOP:
    			case HTBOTTOM:
    				SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENS)));
    				break;
    			case HTLEFT:
    			case HTRIGHT:
    				SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZEWE)));
    				break;
    			case HTTOPLEFT:
    			case HTBOTTOMRIGHT:
    				SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENWSE)));
    				break;
    			case HTTOPRIGHT:
    			case HTBOTTOMLEFT:
    				SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_SIZENESW)));
    				break;
    			case HTCAPTION:
    			case HTSYSMENU:
    			case HTMENU:
    			case HTCLIENT:
    			default:
    				SetCursor(LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW)));
    				break;
    			}
    		}
    		return TRUE;



  • 相关阅读:
    深入理解JavaScript系列(15):函数(Functions)
    深入理解JavaScript系列(8):S.O.L.I.D五大原则之里氏替换原则LSP
    深入理解JavaScript系列(2):揭秘命名函数表达式
    深入理解JavaScript系列(3):全面解析Module模式
    深入理解JavaScript系列(21):S.O.L.I.D五大原则之接口隔离原则ISP
    深入理解JavaScript系列(18):面向对象编程之ECMAScript实现(推荐)
    理解Javascript_13_执行模型详解
    深入理解JavaScript系列(6):S.O.L.I.D五大原则之单一职责SRP
    深入理解JavaScript系列(7):S.O.L.I.D五大原则之开闭原则OCP
    深入理解JavaScript系列(11):执行上下文(Execution Contexts)
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13318784.html
Copyright © 2020-2023  润新知