天气与日历 切换到窄版

 找回密码
 立即注册
中国膜结构网
十大进口膜材评选 十大国产膜材评选 十大膜结构设计评选 十大膜结构公司评选
查看: 3|回复: 0

ObjectARX实现CAD截图

[复制链接]
  • TA的每日心情
    开心
    昨天 06:36
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    105

    主题

    11

    回帖

    1308

    积分

    管理员

    积分
    1308
    QQ
    发表于 2024-10-24 12:53:18 | 显示全部楼层 |阅读模式
    1. //框选区域
    2. ads_point p1, p2;
    3. if (RTNORM != ads_getpoint(NULL, _T("\n第一角点"), p1))
    4.     return;
    5. if (RTNORM != ads_getcorner(p1, _T("\n第二角点"), p2))
    6.     return;
    7. AcGePoint3d pt1, pt2;
    8. pt1 = asPnt3d(p1);
    9. pt2 = asPnt3d(p2);
    10. //用户坐标系转世界坐标系
    11. pt1 = PublicFunction::TransUcs2Wcs(pt1);
    12. pt2 = PublicFunction::TransUcs2Wcs(pt2);
    13. ads_name sset;
    14. if (RTNORM != acedSSGet(_T("W"), p1, p2, NULL, sset))
    15. {
    16.     return;
    17. }
    18. AcDbObjectIdArray idArray = PublicFunction::GetObjectids(sset);
    19. ads_ssfree(sset);
    20. if (idArray.isEmpty())
    21.     return;
    22. //bmp
    23. POINT cp1 = GetPointInAcadCoordinate(pt1);
    24. POINT cp2 = GetPointInAcadCoordinate(pt2);
    25. if (cp1.x > cp2.x)
    26.     std::swap(cp1, cp2);
    27. WriteToBmp(cp1, cp2);
    28. //wblock
    29. AcDbDatabase* pShortCutDwg = NULL;
    30. acdbCurDwg()->wblock(pShortCutDwg, idArray, AcGePoint3d(0.0, 0.0, 0.0));
    31. pShortCutDwg->saveAs(_T("D:\\a.dwg"));
    32. delete pShortCutDwg;
    33. pShortCutDwg = NULL;



    34. POINT GetPointInAcadCoordinate(const AcGePoint3d &pt)
    35. {
    36.     ads_point pnt;
    37.     ads_point_set(asDblArray(pt), pnt);
    38.     //获取视口编号
    39.     int wndNum = 0;
    40.     struct resbuf v;
    41.     if (acedGetVar(_T("cvport"), &v) == RTNORM)
    42.     {
    43.         wndNum = v.resval.rint;
    44.     }
    45.     //将pt坐标转为pixel屏幕坐标
    46.     CPoint ptInScreen;
    47.     acedCoordFromWorldToPixel(wndNum, pnt, ptInScreen);
    48.     //将ptInScreen转为在view上的点p3
    49.     POINT p3;
    50.     p3.x = ptInScreen.x; p3.y = ptInScreen.y;
    51.     ::ClientToScreen(acedGetAcadDwgView()->m_hWnd, &p3);
    52.     return p3;
    53. }

    54. void WriteToBmp(const POINT& pt1, const POINT& pt2)
    55. {
    56.     //获得屏幕的HWND
    57.     HWND hWnd = ::GetDesktopWindow();
    58.     //获得屏幕的HDC
    59.     HDC hScreenDC = ::GetDC(hWnd);
    60.     HDC MemDC = ::CreateCompatibleDC(hScreenDC);
    61.     RECT rect;
    62.     ::GetWindowRect(hWnd, &rect);
    63.     SIZE screensize;
    64.     screensize.cx = abs(pt1.x - pt2.x);
    65.     screensize.cy = abs(pt1.y - pt2.y);
    66.     HBITMAP hBitmap = ::CreateCompatibleBitmap(hScreenDC, screensize.cx, screensize.cy);
    67.     HGDIOBJ hOldBMP = ::SelectObject(MemDC, hBitmap);
    68.     ::BitBlt(MemDC, 0, 0, screensize.cx, screensize.cy, hScreenDC, pt1.x, pt1.y, SRCCOPY);
    69.     ::SelectObject(MemDC, hOldBMP);
    70.     ::DeleteObject(MemDC);
    71.     ::ReleaseDC(hWnd, hScreenDC);

    72.     HDC hDC = ::CreateDC(_T("DISPLAY"), NULL, NULL, NULL);
    73.     //当前分辨率下每个像素所占字节数
    74.     int iBits = ::GetDeviceCaps(hDC, BITSPIXEL) * ::GetDeviceCaps(hDC, PLANES);
    75.     ::DeleteDC(hDC);
    76.     //位图中每个像素所占字节数
    77.     WORD wBitCount;
    78.     if (iBits <= 1)
    79.         wBitCount = 1;
    80.     else if (iBits <= 4)
    81.         wBitCount = 4;
    82.     else if (iBits <= 8)
    83.         wBitCount = 8;
    84.     else if (iBits <= 24)
    85.         wBitCount = 24;
    86.     else
    87.         wBitCount = iBits;
    88.     //调色板大小, 位图中像素字节大小
    89.     DWORD dwPaletteSize = 0;
    90.     if (wBitCount <= 8)
    91.         dwPaletteSize = (1 << wBitCount) * sizeof(RGBQUAD);

    92.     //位图属性结构
    93.     BITMAP bm;
    94.     ::GetObject(hBitmap, sizeof(bm), (LPSTR)&bm);

    95.     //位图信息头结构
    96.     BITMAPINFOHEADER bi;
    97.     bi.biSize = sizeof(BITMAPINFOHEADER);
    98.     bi.biWidth = bm.bmWidth;
    99.     bi.biHeight = bm.bmHeight;
    100.     bi.biPlanes = 1;
    101.     bi.biBitCount = wBitCount;
    102.     //BI_RGB表示位图没有压缩
    103.     bi.biCompression = BI_RGB;
    104.     bi.biSizeImage = 0;
    105.     bi.biXPelsPerMeter = 0;
    106.     bi.biYPelsPerMeter = 0;
    107.     bi.biClrUsed = 0;
    108.     bi.biClrImportant = 0;

    109.     DWORD dwBmBitsSize = ((bm.bmWidth * wBitCount + 31) / 32) * 4 * bm.bmHeight;
    110.     //为位图内容分配内存
    111.     HANDLE hDib = ::GlobalAlloc(GHND, dwBmBitsSize + dwPaletteSize + sizeof(BITMAPINFOHEADER));
    112.     LPBITMAPINFOHEADER lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDib);
    113.     *lpbi = bi;
    114.     // 处理调色板
    115.     HANDLE hPal = ::GetStockObject(DEFAULT_PALETTE);
    116.     HANDLE  hOldPal = NULL;
    117.     if (hPal)
    118.     {
    119.         hDC = ::GetDC(NULL);
    120.         hOldPal = SelectPalette(hDC, (HPALETTE)hPal, FALSE);
    121.         RealizePalette(hDC);
    122.     }
    123.     // 获取该调色板下新的像素值
    124.     ::GetDIBits(hDC, hBitmap, 0, (UINT)bm.bmHeight, (LPSTR)lpbi + sizeof(BITMAPINFOHEADER) + dwPaletteSize, (BITMAPINFO*)lpbi, DIB_RGB_COLORS);
    125.     //恢复调色板
    126.     if (hOldPal)
    127.     {
    128.         SelectPalette(hDC, (HPALETTE)hOldPal, TRUE);
    129.         RealizePalette(hDC);
    130.         ::ReleaseDC(NULL, hDC);
    131.     }
    132.     //位图文件头结构
    133.     BITMAPFILEHEADER bmfHdr;
    134.     // "BM"
    135.     bmfHdr.bfType = 0x4D42;
    136.     // 设置位图文件头
    137.     DWORD dwDIBSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + dwPaletteSize + dwBmBitsSize;
    138.     bmfHdr.bfSize = dwDIBSize;
    139.     bmfHdr.bfReserved1 = 0;
    140.     bmfHdr.bfReserved2 = 0;
    141.     bmfHdr.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER) + dwPaletteSize;
    142.     //创建位图文件
    143.     HANDLE hFile = CreateFile(_T("D:\\a.bmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
    144.     DWORD dwWritten;
    145.     // 写入位图文件头
    146.     WriteFile(hFile, (LPSTR)&bmfHdr, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
    147.     // 写入位图文件其余内容
    148.     WriteFile(hFile, (LPSTR)lpbi, dwDIBSize, &dwWritten, NULL);
    149.     //清除
    150.     GlobalUnlock(hDib);
    151.     GlobalFree(hDib);
    152.     CloseHandle(hFile);
    153. }


    复制代码

     

     

     

     

    ObjectARX实现CAD截图
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|Archiver|中国膜结构网|中国膜结构协会|进口膜材|国产膜材|ETFE|PVDF|PTFE|设计|施工|安装|车棚|看台|污水池|中国膜结构网_中国空间膜结构协会

    GMT+8, 2024-11-1 10:38 , Processed in 0.155330 second(s), 24 queries .

    Powered by Discuz! X3.5

    © 2001-2024 Discuz! Team.

    快速回复 返回顶部 返回列表