回 帖 发 新 帖 刷新版面

主题:[原创]实现类似AutoCAD中鼠标中键实时缩放、平移图形

#include <windows.h>
#include <windowsx.h>
#include <tchar.h>


#define WM_MOUSEWHEEL 0x020A

TCHAR szTitle[] = TEXT("Window Title");                    
TCHAR szClass[] = TEXT("Window Class");

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

    WNDCLASSEX wcex;
    HWND hwnd;
    MSG msg;    

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style            = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wcex.lpfnWndProc    = WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WINLOGO));
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;
    wcex.lpszClassName    = szClass;
    wcex.hIconSm        = NULL;

    if(!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, TEXT("Failed to Register the Window Class!"), szTitle, MB_ICONERROR);
        return 0;
    }


    hwnd = CreateWindowEx(WS_EX_APPWINDOW|WS_EX_WINDOWEDGE, 
                            szClass, szTitle, 
                            WS_OVERLAPPEDWINDOW, 
                            CW_USEDEFAULT, CW_USEDEFAULT, 
                            CW_USEDEFAULT, CW_USEDEFAULT, 
                            NULL, NULL, 
                            hInstance, NULL);

    if(!hwnd) return 0;

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

回复列表 (共4个回复)

沙发

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static POINT pt1,pt2,pt3,pt4;    
    static CHOOSEFONT cf;
    static LOGFONT lf;
    static XFORM xf;    
    PAINTSTRUCT ps;
    TCHAR text[100];    
    HFONT hFont;
    HDC hDC;
    int count;    
    int temp;

    switch(uMsg)
    {    
    case WM_MOUSEWHEEL:        
        if((short)HIWORD(wParam) > 0)
        {
            if(xf.eM11 > 50.0f) return 0;
            xf.eM11 *= 1.25f;
            xf.eM22 = xf.eM11;        
        }
        else
        {
            if(xf.eM11 < 0.2f) return 0;
            xf.eM11 *= 0.8f;
            xf.eM22 = xf.eM11;                
        }
        pt3.x = GET_X_LPARAM(lParam); 
        pt3.y = GET_Y_LPARAM(lParam);
        ScreenToClient(hwnd,&pt3);
        pt4 = pt3;
        hDC = GetDC(hwnd);
        DPtoLP(hDC,&pt4,1);
        SetWorldTransform(hDC,&xf);        
        LPtoDP(hDC,&pt4,1);    
        OffsetViewportOrgEx(hDC,pt3.x-pt4.x,pt3.y-pt4.y,NULL);        
        ReleaseDC(hwnd,hDC);
        InvalidateRect(hwnd,NULL,TRUE);            
        return 0;

板凳


    case WM_MBUTTONDOWN:
        SetCapture(hwnd);
        pt1.x = GET_X_LPARAM(lParam); 
        pt1.y = GET_Y_LPARAM(lParam);
        return 0;
    case WM_MBUTTONUP:
        pt2.x = GET_X_LPARAM(lParam); 
        pt2.y = GET_Y_LPARAM(lParam);
        hDC = GetDC(hwnd);
        OffsetViewportOrgEx(hDC,pt2.x-pt1.x,pt2.y-pt1.y,NULL);        
        ReleaseDC(hwnd,hDC);
        InvalidateRect(hwnd,NULL,TRUE);
        ReleaseCapture();
        return 0;
    case WM_RBUTTONDOWN:
        if(ChooseFont(&cf))
        {            
            InvalidateRect(hwnd,NULL,TRUE);
        }
        return 0;
    case WM_PAINT:
        hDC = BeginPaint(hwnd,&ps);    
        MoveToEx(hDC,0,0,NULL);
        LineTo(hDC,15000,-13000);
        MoveToEx(hDC,15000,0,NULL);
        LineTo(hDC,0,-13000);
        MoveToEx(hDC,7500,0,NULL);
        LineTo(hDC,7500,-13000);
        MoveToEx(hDC,0,-6500,NULL);
        LineTo(hDC,15000,-6500);
        temp = lf.lfHeight;                    
        lf.lfHeight = -(int)(cf.iPointSize * 3.53);//1磅(point)=0.353毫米(mm)
        hFont = CreateFontIndirect(&lf);
        lf.lfHeight = temp;
        SelectObject(hDC,hFont);
        DeleteObject(hFont);
        SetTextColor(hDC,cf.rgbColors);        
        count = wsprintf(text,TEXT("%s"),TEXT("字体样式"));
        SetTextAlign(hDC,TA_TOP);
        TextOut(hDC,3000,-2000,text,count);
        EndPaint(hwnd,&ps);
        return 0;

3 楼

case WM_CREATE:
        hDC = GetDC(hwnd);            
        SetGraphicsMode(hDC,GM_ADVANCED);
        SetBkMode(hDC,TRANSPARENT);
        SetMapMode(hDC,MM_HIMETRIC);
        GetWorldTransform(hDC,&xf);
        OffsetViewportOrgEx(hDC,200,100,NULL);    
        ReleaseDC(hwnd,hDC);

        lf.lfCharSet = GB2312_CHARSET;
        lstrcpy(lf.lfFaceName,TEXT("宋体"));
        lf.lfHeight = -19;        
        lf.lfWeight = FW_NORMAL;
        lf.lfOutPrecision = OUT_STROKE_PRECIS;
        lf.lfClipPrecision = CLIP_STROKE_PRECIS;
        lf.lfQuality = DRAFT_QUALITY;
        lf.lfPitchAndFamily = VARIABLE_PITCH;

        cf.lStructSize = sizeof(CHOOSEFONT);
        cf.hwndOwner = hwnd;
        cf.lpLogFont = &lf;
        cf.iPointSize = 140;
        cf.Flags = CF_INITTOLOGFONTSTRUCT|CF_BOTH|CF_EFFECTS;

        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

4 楼

好贴,记号
要慢慢看

我来回复

您尚未登录,请登录后再回复。点此登录或注册