345 lines
8.1 KiB
C++
345 lines
8.1 KiB
C++
// BmpDlg.cpp : implementation file
|
||
//
|
||
|
||
#include "stdafx.h"
|
||
#include "BmpDlg.h"
|
||
// Zyu add on 100722,支持64位
|
||
#include "ximage.h"
|
||
|
||
#ifdef _DEBUG
|
||
#define new DEBUG_NEW
|
||
#undef THIS_FILE
|
||
static char THIS_FILE[] = __FILE__;
|
||
#endif
|
||
|
||
/////////////////////////////////////////////////////////////////////////////
|
||
// CBmpDlg dialog
|
||
|
||
// Zyu modify on 100722,支持64位
|
||
//extern int WriteJpeg( FILE * input_file, FILE * output_file);
|
||
//extern int WriteJpeg( FILE * input_file, FILE * output_file, int quality);
|
||
|
||
CBmpDlg::CBmpDlg(CWnd* pParent /*=NULL*/)
|
||
: GlwCAcUiDialog(CBmpDlg::IDD, pParent)
|
||
{
|
||
//{{AFX_DATA_INIT(CBmpDlg)
|
||
// NOTE: the ClassWizard will add member initialization here
|
||
//}}AFX_DATA_INIT
|
||
}
|
||
CBmpDlg::~CBmpDlg()
|
||
{
|
||
bitmap.Detach();
|
||
bitmap.DeleteObject();
|
||
}
|
||
|
||
|
||
void CBmpDlg::DoDataExchange(CDataExchange* pDX)
|
||
{
|
||
CDialog::DoDataExchange(pDX);
|
||
//{{AFX_DATA_MAP(CBmpDlg)
|
||
// NOTE: the ClassWizard will add DDX and DDV calls here
|
||
//}}AFX_DATA_MAP
|
||
}
|
||
|
||
|
||
BEGIN_MESSAGE_MAP(CBmpDlg, GlwCAcUiDialog)
|
||
//{{AFX_MSG_MAP(CBmpDlg)
|
||
ON_WM_PAINT()
|
||
//}}AFX_MSG_MAP
|
||
END_MESSAGE_MAP()
|
||
|
||
/////////////////////////////////////////////////////////////////////////////
|
||
// CBmpDlg message handlers
|
||
CPoint Lcs2Mcs2(XPoint point, BOOL bVector=FALSE)
|
||
{
|
||
CRect rect;
|
||
CPoint pt;
|
||
acedGetAcadDwgView()->GetClientRect(&rect);
|
||
|
||
pt.x = AS_INT(point.x*rect.Width());
|
||
if (!bVector) {
|
||
pt.y = rect.bottom - AS_INT(point.y*rect.Height());
|
||
} else {
|
||
pt.y = - AS_INT(point.y*rect.Height());
|
||
}
|
||
|
||
return pt;
|
||
}
|
||
BOOL CBmpDlg::OnInitDialog()
|
||
{
|
||
CDialog::OnInitDialog();
|
||
ads_point lowerpt, upperpt;
|
||
CPoint pt1,pt2;
|
||
int cvport=0;
|
||
VERIFY(xg_getvar(_T("CVPORT"), &cvport)==RTNORM);
|
||
|
||
AT_ScrCorner(lowerpt, upperpt);
|
||
acedCoordFromWorldToPixel(cvport,lowerpt,pt1);
|
||
acedCoordFromWorldToPixel(cvport,upperpt,pt2);
|
||
|
||
m_pWndAcadView = acedGetAcadDwgView();
|
||
//CRect rect;
|
||
//m_pWndAcadView->GetClientRect(rect);
|
||
DList<TViewportInfo> ss;
|
||
ReadVports(ss);
|
||
long lCount=ss.Length();
|
||
for(long i=0L; i<lCount; i++) {
|
||
|
||
TViewportInfo &ent=*ss[i];
|
||
if (ent.VpID != cvport)
|
||
continue;
|
||
XRectAngle box(ent.ptLeft, ent.ptRight);
|
||
XPoint ptCorners[4];
|
||
CPoint ptCorners2[4];
|
||
ptCorners[0]=box.GetLowLeft();
|
||
ptCorners[1]=box.GetLowRight();
|
||
ptCorners[2]=box.GetUpRight();
|
||
ptCorners[3]=box.GetUpLeft();
|
||
for(int n=0; n<4; n++) {
|
||
//if (fabs(ptCorners[n].x)<1.0E-6 || fabs(ptCorners[n].y-1.0)<1.0E-6)
|
||
// continue;
|
||
//CPoint ptOnView=Lcs2Mcs2(ptCorners[n]);
|
||
ptCorners2[n]=Lcs2Mcs2(ptCorners[n]);
|
||
}
|
||
CRect rect2(ptCorners2[3], ptCorners2[1]);
|
||
if (rect2.Width()>4 && rect2.Height()>4)
|
||
rect2.DeflateRect(2,2,2,2);
|
||
rectVP = rect2;
|
||
break;
|
||
}
|
||
GetRectBmpFromBmp(m_pWndAcadView, rectVP);
|
||
MoveDlg();
|
||
|
||
|
||
return TRUE; // return TRUE unless you set the focus to a control
|
||
// EXCEPTION: OCX Property Pages should return FALSE
|
||
}
|
||
|
||
|
||
void CBmpDlg::GetRectBmpFromBmp( CWnd *pWnd ,CRect rectClient)
|
||
{
|
||
CClientDC dc(pWnd);
|
||
CDC memDC;
|
||
CRect rect;
|
||
memDC.CreateCompatibleDC(&dc);
|
||
//pWnd->GetWindowRect(rect);
|
||
bitmap.CreateCompatibleBitmap(&dc, rectClient.Width(),rectClient.Height());
|
||
BITMAP BM;
|
||
bitmap.GetObject(sizeof(BM),&BM);
|
||
w_CBitmap=rectClient.Width();
|
||
h_CBitmap=rectClient.Height();
|
||
|
||
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
|
||
|
||
memDC.BitBlt(0, 0, rectClient.Width(),rectClient.Height(), &dc, rectClient.left,rectClient.top,SRCCOPY);
|
||
|
||
memDC.SelectObject(pOldBitmap);
|
||
}
|
||
|
||
void CBmpDlg::ReadAcad( CWnd *pWnd , BOOL bIsAll)
|
||
{
|
||
CClientDC dc(pWnd);
|
||
CDC memDC;
|
||
CRect rect;
|
||
memDC.CreateCompatibleDC(&dc);
|
||
pWnd->GetWindowRect(rect);
|
||
if (bIsAll)
|
||
bitmap.CreateCompatibleBitmap(&dc, w_CBitmap/2,h_CBitmap/2 );
|
||
else
|
||
bitmap.CreateCompatibleBitmap(&dc, rect.Width(),rect.Height() );
|
||
|
||
BITMAP BM;
|
||
// int w_CBitmap,h_CBitmap;
|
||
|
||
bitmap.GetObject(sizeof(BM),&BM);
|
||
w_CBitmap=BM.bmWidth;
|
||
h_CBitmap=BM.bmHeight;
|
||
|
||
CBitmap* pOldBitmap = memDC.SelectObject(&bitmap);
|
||
if (bIsAll)
|
||
memDC.BitBlt(0, 0, rect.Width(),rect.Height(), &dc, 0,0,SRCCOPY);
|
||
else
|
||
memDC.BitBlt(0, 0, rect.Width()/2,rect.Height()/2, &dc, w_CBitmap/2,h_CBitmap/2,SRCCOPY);
|
||
|
||
|
||
memDC.SelectObject(pOldBitmap);
|
||
|
||
}
|
||
|
||
void CBmpDlg::HideDialog(BOOL Flag)
|
||
{
|
||
if (Flag) {
|
||
GetParent()->EnableWindow(TRUE);
|
||
GetParent()->SetFocus();
|
||
GetParent()->SetActiveWindow();
|
||
ShowWindow(SW_HIDE);
|
||
}
|
||
else {
|
||
GetParent()->EnableWindow(FALSE);
|
||
EnableWindow(TRUE);
|
||
SetActiveWindow();
|
||
ShowWindow(SW_SHOW);
|
||
}
|
||
}
|
||
|
||
void CBmpDlg::DrawBmp(CBitmap* Bitmap,CDC *pDC,int x,int y,int wide,int high,int DrawMode)
|
||
{
|
||
int wide1,high1;
|
||
int w_CBitmap,h_CBitmap;
|
||
|
||
BITMAP BM;
|
||
CDC pDC1;
|
||
Bitmap->GetObject(sizeof(BM),&BM);
|
||
w_CBitmap=rectVP.Width();
|
||
h_CBitmap=rectVP.Height();
|
||
if (DrawMode == 1)
|
||
{
|
||
wide1= w_CBitmap;
|
||
high1= h_CBitmap;
|
||
}
|
||
else if (DrawMode == 2)
|
||
{
|
||
float bl1 = (float)wide/w_CBitmap;
|
||
float bl2 = (float)high/h_CBitmap;
|
||
if (bl1 > bl2)
|
||
{
|
||
high1 = high;
|
||
wide1 = (int)(w_CBitmap*bl2);
|
||
}
|
||
else
|
||
{
|
||
wide1=wide;
|
||
high1=(int)(bl1*h_CBitmap);
|
||
}
|
||
}
|
||
else if (DrawMode == 3)
|
||
{
|
||
wide1=wide;
|
||
high1=high;
|
||
}
|
||
pDC1.CreateCompatibleDC(pDC);
|
||
pDC1.SelectObject(Bitmap);
|
||
pDC->StretchBlt(x,y,wide1,high1,&pDC1,0,0,w_CBitmap,h_CBitmap,SRCCOPY);
|
||
// Bitmap->DeleteObject();
|
||
}
|
||
|
||
void CBmpDlg::OnPaint()
|
||
{
|
||
CWnd *pWnd = GetDlgItem(IDC_STATIC_BMP);
|
||
CPaintDC dc(this); // device context for painting
|
||
CPaintDC dc2(pWnd); // device context for painting
|
||
CRect rect2;
|
||
pWnd->GetClientRect(rect2);
|
||
|
||
DrawBmp(&bitmap,&dc2,0,0,rect2.Width(),rect2.Height(),1);
|
||
|
||
// Do not call CDialog::OnPaint() for painting messages
|
||
}
|
||
GEOAPI int SaveBitmapToFile(HBITMAP hBitmap,LPTSTR lpFileName); //hBitmap 为刚才的屏幕位图句柄 lpFileName 为位图文件名
|
||
|
||
void CBmpDlg::MoveDlg()
|
||
{
|
||
CWnd *pWnd = GetDlgItem(IDC_STATIC_BMP);
|
||
|
||
int w_CBitmap2,h_CBitmap2;
|
||
|
||
CRect rect, rectOld;
|
||
|
||
w_CBitmap2=rectVP.Width();
|
||
h_CBitmap2=rectVP.Height();
|
||
|
||
pWnd->GetWindowRect(rect);
|
||
ScreenToClient(rect);
|
||
rectOld = rect;
|
||
rect.bottom = rect.top + h_CBitmap +3;
|
||
rect.right = rect.left + w_CBitmap +3;
|
||
int moveX, moveY;
|
||
moveX = rect.Width() - rectOld.Width();
|
||
moveY = rect.Height() - rectOld.Height();
|
||
pWnd->MoveWindow(rect);
|
||
|
||
CWnd *pWndOk = GetDlgItem(IDOK);
|
||
CWnd *pWndCan = GetDlgItem(IDCANCEL);
|
||
CRect rectDlg, rectOk, rectCan;
|
||
pWndOk->GetWindowRect(rectOk);
|
||
pWndCan->GetWindowRect(rectCan);
|
||
ScreenToClient(rectOk);
|
||
ScreenToClient(rectCan);
|
||
rectOk.top = rectOk.top + moveY;
|
||
rectOk.bottom = rectOk.bottom + moveY;
|
||
rectCan.top = rectCan.top + moveY;
|
||
rectCan.bottom = rectCan.bottom + moveY;
|
||
pWndOk->MoveWindow(rectOk);
|
||
pWndCan->MoveWindow(rectCan);
|
||
|
||
|
||
this->GetWindowRect(rectDlg);
|
||
//ScreenToClient(rectDlg);
|
||
rectDlg.right= rectDlg.right + moveX;
|
||
rectDlg.bottom = rectDlg.bottom + moveY;
|
||
rectDlg.top = rectDlg.top;
|
||
rectDlg.left = rectDlg.left ;
|
||
this->MoveWindow(rectDlg);
|
||
}
|
||
|
||
void CBmpDlg::OnOK()
|
||
{
|
||
CWnd *pWnd = GetDlgItem(IDC_STATIC_BMP);
|
||
//ReadAcad(pWnd, TRUE);
|
||
|
||
// GetRectBmpFromBmp(pWnd, rectVP);
|
||
|
||
HBITMAP hBitmap = (HBITMAP) bitmap.GetSafeHandle();
|
||
XString sFile;
|
||
if (GetFileDia(sFile, 1, _T("bmp;jpg"), _T("输入文件名称"))==RTNORM) {
|
||
XPathParser parser(sFile);
|
||
if (_tcsicmp(parser.m_sExt, _T(".bmp"))==0) {
|
||
SaveBitmapToFile(hBitmap, sFile);
|
||
} else if (_tcsicmp(parser.m_sExt, _T(".jpg"))==0) {
|
||
XString sTempFile=GetTempFileName(_T(".bmp"));
|
||
SaveBitmapToFile(hBitmap, sTempFile);
|
||
/*
|
||
FILE *fpIn=_tfopen(sTempFile, _T("rb"));
|
||
if (!fpIn) return;
|
||
FILE *fpOut=_tfopen(sFile, _T("wb"));
|
||
if (!fpOut) {fclose(fpIn); return;}
|
||
int bOk=WriteJpeg(fpIn, fpOut, 75);
|
||
fclose(fpIn); fclose(fpOut);
|
||
*/
|
||
int bOk = CTangentImage::WriteJpeg(sTempFile, sFile, CXIMAGE_FORMAT_BMP, 75.0);
|
||
_tremove(sTempFile);
|
||
}
|
||
CDialog::OnOK();
|
||
}
|
||
|
||
|
||
|
||
// CDialog::OnOK();
|
||
}
|
||
|
||
|
||
void SaveView()
|
||
{
|
||
CAcModuleResourceOverride vTemp;
|
||
CBmpDlg dlg;
|
||
dlg.DoModal();
|
||
}
|
||
|
||
/*
|
||
|
||
|
||
回复贴子:
|
||
davidprg() 回复于2001-7-17 17:19:05
|
||
从另一个DC COPY 过去啊。EASY。
|
||
gwolf() 回复于2001-7-17 17:22:09
|
||
能具体些吗?
|
||
WhiteWaterBlueSky(疯狂数码) 回复于2001-7-17 17:45:14
|
||
1。Use GetDC to get window dc
|
||
2。Use CreateCompatibleDC to create a compatible dc
|
||
3。Use CreateCompatibleBitmap to create compatible bitmap
|
||
4。Select compatible bitmap into compatible dc
|
||
5。Use StretchBlt or BitBlt to copy bitmap from window dc to compatible dc
|
||
|
||
OK!
|
||
|
||
*/ |