#include #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; POINT rectExPoint; typedef struct rect { int a; int b; }; rect sides; int edge = 0; bool Inside (POINT mouse, POINT rectMidd, rect a_sides) { if (mouse.x > rectMidd.x-a_sides.a & mouse.x < rectMidd.x+a_sides.a & mouse.y > rectMidd.y-a_sides.b & mouse.y < rectMidd.y+a_sides.b) { return true; } else { return false; } } int OnEdge (POINT mouse, POINT rectMidd, rect a_sides) { int l_sides = 0; if (abs(mouse.x - rectMidd.x) == a_sides.a & mouse.y >= rectMidd.y-a_sides.b & mouse.y <= rectMidd.y+a_sides.b) l_sides = l_sides + 10; if (abs(mouse.y - rectMidd.y) == a_sides.b & mouse.x >= rectMidd.x-a_sides.a & mouse.x <= rectMidd.x+a_sides.a) l_sides = l_sides + 1; return l_sides; } rect Stretch (POINT mouse, POINT rectMidd, rect a_sides, int a_edge) { rect l_sides; l_sides.a = a_sides.a; l_sides.b = a_sides.b; if (a_edge % 10) l_sides.b = abs(mouse.y - rectMidd.y); if (a_edge >= 10) l_sides.a = abs(mouse.x - rectMidd.x); return l_sides; } int DrawRect(HDC hdc, POINT a_center, rect a_sides, COLORREF color) { HPEN hNPen=CreatePen(PS_SOLID,1, color); HPEN hOPen=(HPEN)SelectObject(hdc,hNPen); Rectangle(hdc, a_center.x-a_sides.a , a_center.y-a_sides.b, a_center.x+a_sides.a, a_center.y+a_sides.b); DeleteObject(SelectObject(hdc,hOPen)); return 0; } int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { static TCHAR szAppName[] = TEXT ("Connect") ; HWND hwnd ; MSG msg ; WNDCLASS wndclass ; wndclass.style = CS_HREDRAW | CS_VREDRAW ; wndclass.lpfnWndProc = WndProc ; wndclass.cbClsExtra = 0 ; wndclass.cbWndExtra = 0 ; wndclass.hInstance = hInstance ; wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ; wndclass.hCursor = LoadCursor (NULL, IDC_CROSS) ; wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ; wndclass.lpszMenuName = NULL ; wndclass.lpszClassName = szAppName ; rectExPoint.x = -1; rectExPoint.y = -1; sides.a = 10; sides.b = 20; if (!RegisterClass (&wndclass)) { MessageBox (NULL, TEXT (""), szAppName, MB_ICONERROR) ; return 0 ; } hwnd = CreateWindow (szAppName, TEXT (""), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL) ; ShowWindow (hwnd, iCmdShow) ; UpdateWindow (hwnd) ; while (GetMessage (&msg, NULL, 0, 0)>0) { TranslateMessage (&msg) ; DispatchMessage (&msg) ; } return (int)msg.wParam ; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static int iPrevX,iPrevY,cxClient,cyClient; static HDC hdc, hdcMem; static HBITMAP hBitmap , oldBitmap; PAINTSTRUCT ps; POINT rectPoint; TCHAR chText[30]; switch(message) { case WM_RBUTTONUP: hdc=GetDC(hwnd); rectExPoint.x = 100; rectExPoint.y = 100; DrawRect(hdc, rectExPoint, sides, RGB(0, 0, 0)); ReleaseDC(hwnd,hdc); return 0 ; case WM_LBUTTONUP: rectPoint.x = LOWORD (lParam); rectPoint.y = HIWORD (lParam); edge = 0; sprintf(chText, "x=%d, y=%d, x=%d, y=%d, a=%d, b=%d, e=%d", rectExPoint.x, rectExPoint.y, rectPoint.x, rectPoint.y, sides.a, sides.b, edge); SetWindowText(hwnd, chText); return 0 ; case WM_LBUTTONDOWN: rectPoint.x = LOWORD (lParam); rectPoint.y = HIWORD (lParam); if (!edge) edge = OnEdge(rectPoint, rectExPoint, sides); sprintf(chText, "x=%d, y=%d, x=%d, y=%d, a=%d, b=%d, e=%d", rectExPoint.x, rectExPoint.y, rectPoint.x, rectPoint.y, sides.a, sides.b, edge); SetWindowText(hwnd, chText); return 0 ; case WM_MOUSEWHEEL: if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) { hdc=GetDC(hwnd); DrawRect(hdc, rectExPoint, sides, RGB(255, 255, 255)); sides.a ++; sides.b ++; DrawRect(hdc, rectExPoint, sides, RGB(0, 0, 0)); ReleaseDC(hwnd,hdc); }; if (GET_WHEEL_DELTA_WPARAM(wParam) < 0) { hdc=GetDC(hwnd); DrawRect(hdc, rectExPoint, sides, RGB(255, 255, 255)); sides.a --; sides.b --; DrawRect(hdc, rectExPoint, sides, RGB(0, 0, 0)); ReleaseDC(hwnd,hdc); }; return 0 ; case WM_MOUSEMOVE: rectPoint.x = LOWORD (lParam); rectPoint.y = HIWORD (lParam); hdc=GetDC(hwnd); if((wParam & MK_LBUTTON)) { if (edge) { DrawRect(hdc, rectExPoint, sides, RGB(255, 255, 255)); sides = Stretch (rectPoint, rectExPoint, sides, edge); DrawRect(hdc, rectExPoint, sides, RGB(0, 0, 0)); ReleaseDC(hwnd,hdc); } else if (Inside(rectPoint, rectExPoint, sides)) { DrawRect(hdc, rectExPoint, sides, RGB(255, 255, 255)); rectExPoint = rectPoint; DrawRect(hdc, rectPoint, sides, RGB(0, 0, 0)); } ReleaseDC(hwnd,hdc); } sprintf(chText, "x=%d, y=%d, x=%d, y=%d, a=%d, b=%d, e=%d", rectExPoint.x, rectExPoint.y, rectPoint.x, rectPoint.y, sides.a, sides.b, edge); SetWindowText(hwnd, chText); return 0; case WM_CLOSE: SelectObject(hdcMem,hBitmap); DeleteObject (hBitmap); DeleteObject(hdcMem); PostQuitMessage (0); return 0 ; } return DefWindowProc (hwnd, message, wParam, lParam) ; }