Win Api Рисование

Тема в разделе "C/C++/C#", создана пользователем 123456789igor, 21 окт 2012.

  1. 123456789igor

    123456789igor Active Member

    Регистрация:
    12 янв 2011
    Сообщения:
    29
    Симпатии:
    0
    Не могу разобраться как сделать что бы при рисовании фигуры изменение размера фигур прорисовывалось, знаю что делается это посредством WM_MOUSEMOVE но как именно не пойму.
    main.cpp
    Код (C++):
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    OPENFILENAME ofn;
    char szFileName[MAX_PATH];
    LPTSTR szFile = L"sdfsjd;lfksjd;flksjdfl;skdjf;sldkfjs;ldkfjs;dlfkjs;dflksdjf;lskdjf;lsdkfjs;l
    dkfjs;ldfl"
    ;
    static HBRUSH hBrush;
    static HENHMETAFILE file1 = 0;
    static int nextshape = 0;
    static vector <Shape *> allshape;
    static vector <Shape *> deletedshape;
    static int countshape = 0;
    //static BOOL mLine=FALSE, mRecttangle=FALSE, Ellips=FALSE, mCircle=FALSE, PAINTING=FALSE;
    static Shape *shape = NULL;
    POINT xy;

    switch (message)
    {
    case WM_PAINT:
    for( int i = 0; i < countshape; i++ )
    allshape[i]->draw(hdc);
    break;
    case WM_LBUTTONDOWN:
    if( shape == NULL )
    {
    deletedshape.clear();
    switch (nextshape)
    {
    case 0: shape = new mRectangle();   break;
    case 1: shape = new mCircle();      break;
    case 2: shape = new mTriangle();    break;
    case 3: shape = new mPolygon();    
    allshape.push_back(shape);
    countshape++;                       break;
    case 4: shape = new mText();        break;
    case 5: shape = new mLine();        break;
    case 6: shape = new elips();        break;
    };
    };
    xy.x=LOWORD(lParam); //узнаём координаты
    xy.y=HIWORD(lParam);
    shape->addpoint(xy,1);
    break;
    case WM_LBUTTONUP:
    xy.x=LOWORD(lParam); //узнаём координаты
    xy.y=HIWORD(lParam);
    shape->addpoint(xy, 1);
    if( shape->allow() )
    shape->addpoint(xy,1);
    else
    {
    allshape.push_back( shape );
    countshape++;
    shape = NULL;
    };

    InvalidateRect(hWnd, NULL, false);
    UpdateWindow(hWnd);

    break;
    case WM_RBUTTONDOWN:
    if( (nextshape == 3) && (shape != NULL) )
    {
    xy.x = 0;
    xy.y = 0;
    shape->addpoint( xy, 0 );
    shape = NULL;
    InvalidateRect(hWnd, NULL, false);
    UpdateWindow(hWnd);
    };
    break;

    case WM_MOUSEMOVE:
    if(MK_LBUTTON==wParam)
    {
    if (nextshape = 5)
    {
    xy.x=LOWORD(lParam); //узнаём координаты
    xy.y=HIWORD(lParam);   
    hdc = GetDC(hWnd);
    MoveToEx(hdc, xy.x, xy.y, NULL);
    LineTo(hdc, xy.x, xy.y);
    ReleaseDC(hWnd, hdc);
    }
    }

    break;

    case WM_COMMAND:
    switch(LOWORD(wParam))
    {
    case 1201: nextshape = 1;       break;
    case 1211: nextshape = 0;       break;
    case 1202: nextshape = 5;       break;
    case 1203: nextshape = 6;       break;
    case 1213: nextshape = 3;       break;
    case 1103: PostQuitMessage(0);  break;
    case 1301: if(countshape <= 0 ) break;
    deletedshape.push_back(allshape[countshape-1]);
    countshape--;
    allshape.pop_back();
    InvalidateRect(hWnd, NULL, false);
    UpdateWindow(hWnd);         break;
    case 1302: if( deletedshape.size() <= 0 ) break;
    allshape.push_back(deletedshape[deletedshape.size()-1]);
    countshape++;
    deletedshape.pop_back();
    InvalidateRect(hWnd, NULL, false);
    UpdateWindow(hWnd);         break;
    case 1104:
    RECT rc;

    rc.left = 0;
    rc.right = GetDeviceCaps(hdc, HORZRES);
    rc.top = 0;
    rc.bottom = GetDeviceCaps(hdc, VERTRES);
    int iWidthMM;
    iWidthMM = GetDeviceCaps(hdc, HORZSIZE);
    int iHeightMM;
    iHeightMM = GetDeviceCaps(hdc, VERTSIZE);
    int iWidthPels;
    iWidthPels = GetDeviceCaps(hdc, HORZRES);
    int iHeightPels;
    iHeightPels = GetDeviceCaps(hdc, VERTRES);
    rc.left = (rc.left * iWidthMM * 100)/iWidthPels;
    rc.top = (rc.top * iHeightMM * 100)/iHeightPels;
    rc.right = (rc.right * iWidthMM * 100)/iWidthPels;
    rc.bottom = (rc.bottom * iHeightMM * 100)/iHeightPels;

    OPENFILENAME ofn;

    char szFileName[MAX_PATH];

    ZeroMemory(&ofn, sizeof(ofn));

    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = (LPCWSTR)L"EMF (*.emf)\0*.emf\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = (LPWSTR)szFileName;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = (LPCWSTR)L"emf";
    GetSaveFileName(&ofn);

    filehdc = CreateEnhMetaFile( NULL, ofn.lpstrFile, &rc, NULL);
    break;
    case 1102:
    hBrush = ( HBRUSH ) GetStockObject( HOLLOW_BRUSH );//настройка кисти
    SelectObject( filehdc, hBrush );
    for( int j = 0; j < countshape; j++ )
    {
    allshape[j]->draw(filehdc);
    }
    CloseEnhMetaFile(filehdc);
    break;
    case 1101:     
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(ofn);
    ofn.hwndOwner = NULL;
    ofn.lpstrFilter = (LPCWSTR)L"EMF (*.emf)\0*.emf\0All Files (*.*)\0*.*\0";
    ofn.lpstrFile = (LPWSTR)szFileName;
    ofn.lpstrFile[0] = '\0';
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = (LPCWSTR)L"emf";
    GetOpenFileName( &ofn );
    file1 = GetEnhMetaFile(ofn.lpstrFile);

    RECT newrc;
    newrc.left = 0;
    newrc.right = GetDeviceCaps(hdc, HORZRES);
    newrc.top = 0;
    newrc.bottom = GetDeviceCaps(hdc, VERTRES);
    PlayEnhMetaFile(hdc, file1, &newrc);
    CloseEnhMetaFile(filehdc);
    break;
    case 5555:
    //RECT lpRect;
    ENHMFENUMPROC lpEnhMetaFunc;
    LPVOID lpData;
    EnumEnhMetaFile(hdc, file1, lpEnhMetaFunc, lpData, &rc);
    break;
    };
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
    break;
    }

    return 0;
    }

    int CALLBACK EnhMetaFileProc (HDC hdc, HANDLETABLE * lpHTable, const ENHMETARECORD * lpEMFR, int nObj, LPARAM lpData)
    {
    PostQuitMessage(0);
    return 0;
    };

    void writeEMF(vector <Shape *> allshape, HDC filehdc)
    {

    }
    shape.cpp
    Код (C++):
    #include "Shape.h"

    Shape::Shape(void)
    {
    countSide = 0;
    side.clear();
    };

    Shape::~Shape(void)
    {
    side.clear();
    };

    void Shape::errase(HDC hdc)
    {
    //HPEN hPen = CreatePen(PS_SOLID, 3, RGB(0,0,0));
    //SelectObject(hdc, hPen);
    //draw(hdc);
    //delete hPen;
    //hPen = CreatePen(PS_SOLID, 1, RGB(255,255,255));
    //SelectObject(hdc, hPen);
    //delete hPen;
    };

    int Shape::allow(void)
    {
    if( countSide < 2 )
    return 1;
    else
    return 0;
    };

    void Shape:: addpoint(POINT xy, int fin)
    {
    if( countSide < 2 )
    {
    countSide ++;
    side.push_back(xy);
    };
    };

    mPolygon:: mPolygon(void)
    {
    h = NULL;
    fin = 1;
    };

    int mPolygon:: allow(void)
    {
    return fin;
    };

    void mPolygon:: addpoint(POINT xy, int finish)
    {
    if( finish == 1 )
    {
    countSide++;
    side.push_back(xy);
    }
    else
    {
    h = new POINT[countSide+1];
    for( int i = 0; i < countSide; i++ )
    {
    h[i].x = side[i].x;
    h[i].y = side[i].y;
    };
    h[countSide].x = side[0].x;
    h[countSide].y = side[0].y;
    countSide++;
    side.clear();
    fin = 0;
    };
    };

    void mPolygon:: draw(HDC hdc)
    {
    if( (h != NULL) && (fin == 1) )
    {
    delete h;
    h = NULL;
    };
    if( fin == 0 )
    Polyline(hdc, h, countSide);
    else
    {
    h = new POINT[countSide];
    for( int i = 0; i < countSide; i++ )
    {
    h[i].x = side[i].x;
    h[i].y = side[i].y;
    };
    Polyline(hdc, h, countSide);
    };
    };

    void mRectangle:: draw(HDC hdc)
    {
    if( countSide > 1 )
    Rectangle(hdc, side[0].x, side[0].y, side[1].x, side[1].y);
    };

    void mCircle:: draw(HDC hdc)
    {
    Ellipse(hdc, side[0].x, side[0].y, side[1].x, side[1].y);
    };

    void mTriangle:: draw(HDC hdc)
    {
    POINT h[4];
    for( int i = 0; i < 3; i++ )
    {
    h[i].x = side[i].x;
    h[i].y = side[i].y;
    };
    h[3].x = side[0].x;
    h[3].y = side[0].y;
    Polyline(hdc, h, 4);
    };

    void mTriangle:: addpoint(POINT, int fin)
    {

    };

    void mLine:: draw(HDC hdc)
    {
    if( countSide == 2 )
    Polyline(hdc, h, 2);
    };

    void mLine:: addpoint(POINT xy, int fin)
    {
    if( countSide < 2 )
    {
    h[countSide].x = xy.x;
    h[countSide].y = xy.y;
    };
    countSide++;
    };
     
  2. rrrFer

    rrrFer Well-Known Member
    Команда форума C\C++ Team

    Регистрация:
    6 сен 2011
    Сообщения:
    1.324
    Симпатии:
    36
    чето я непонял что ты хочешь
    WM_MOUSEMOVE - Это событие перемещения мыши, скорее всего.
    Что значит "изменение размера прорисовывалось" ?
     
Загрузка...
Похожие Темы - Win Api Рисование
  1. sergg
    Ответов:
    11
    Просмотров:
    4.695
  2. bestguru
    Ответов:
    0
    Просмотров:
    913
  3. DreamForse
    Ответов:
    0
    Просмотров:
    2.214
  4. Vadik(R)
    Ответов:
    1
    Просмотров:
    1.438
  5. Vadik(R)
    Ответов:
    1
    Просмотров:
    1.404

Поделиться этой страницей