📄 mainwindow.cpp
字号:
Koef = AbsHeight / AbsWidht;
CADDraw.R.top = 0;
CADDraw.R.left = 0;
CADDraw.R.bottom = int(CADDraw.R.bottom * scale);
CADDraw.R.right = int(CADDraw.R.right * scale);
CADDraw.R.bottom = int(CADDraw.R.top + CADDraw.R.right * Koef);
CADDraw.DrawMode = DrwMode;
HANDLE Hnd = DrawCADtoJpeg(CADImage, &CADDraw);
if (Hnd)
{
DWORD Size = GlobalSize(Hnd);
void *P = GlobalLock(Hnd);
HANDLE FHnd = CreateFile(FileName, GENERIC_WRITE, FILE_SHARE_READ,
NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (FHnd)
{
DWORD Wrt;
WriteFile(FHnd, P, Size, &Wrt, NULL);
CloseHandle(FHnd);
}
GlobalUnlock(Hnd);
GlobalFree(Hnd);
}
}
}
void CMainWindow::ChangeView(BYTE View)
{
HMENU hMenu;
if ((DrwMode != View) && (CADImage))
{
hMenu = GetMenu(hWnd);
CheckMenuItem(hMenu, ID_VIEW + DrwMode + 1, MF_BYCOMMAND | MF_UNCHECKED);
DrwMode = View;
CheckMenuItem(hMenu, ID_VIEW + View + 1, MF_BYCOMMAND | MF_CHECKED);
RePaint();
}
}
void CMainWindow::SetDefColor()
{
CHOOSECOLOR cc;
static COLORREF acrCustClr[16];
static DWORD rgbCurrent;
ZeroMemory(&cc, sizeof(CHOOSECOLOR));
cc.lStructSize = sizeof(CHOOSECOLOR);
cc.hwndOwner = hWnd;
cc.hwndOwner = 0;
cc.lpCustColors = (LPDWORD) acrCustClr;
cc.rgbResult = rgbCurrent;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;
if (ChooseColor(&cc))
{
SetDefaultColor(CADImage, cc.rgbResult);
}
RePaint();
}
void CMainWindow::DoCreateStatusBar(HWND hwndParent, HINSTANCE hInst)
{
::InitCommonControls();
hwndStatusBar = CreateWindowEx(
0,
STATUSCLASSNAME,
(LPCTSTR) NULL,
WS_CHILD | WS_BORDER,
0, 0, 0, 0,
hwndParent,
NULL,
hInst,
NULL);
SplitStatusBar();
SetTextToStatusBar("Demo");
ShowWindow(hwndStatusBar, SW_SHOWNORMAL);
}
void CMainWindow::SetTextToStatusBar(LPSTR str, int part)
{
SendMessage(hwndStatusBar, SB_SETTEXT, part, (LPARAM) str);
}
void CMainWindow::DoCreateToolBar(HWND hwndParent, HINSTANCE hInst)
{
TBADDBITMAP tbAddBitMap[QUANTITY_OF_BUTTONS];
TBBUTTON tbButton[QUANTITY_OF_BUTTONS];
::InitCommonControls();
hwndToolBar = ::CreateWindowEx(
0,
TOOLBARCLASSNAME,
(LPCTSTR) NULL,
CCS_TOP | WS_CHILD,
0, 0, 0, 0,
hwndParent,
NULL,
hInst,
NULL);
::SendMessage(hwndToolBar, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON) , (LPARAM)0 );
for (int i=0; i< QUANTITY_OF_BUTTONS; i++)
tbAddBitMap[i].hInst = hInst;
tbAddBitMap[0].nID = IDB_BMORBITUPX;
tbAddBitMap[1].nID = IDB_BMORBITDOWNX;
tbAddBitMap[2].nID = IDB_BMORBITUPY;
tbAddBitMap[3].nID = IDB_BMORBITDOWNY;
tbAddBitMap[4].nID = IDB_BMORBITUPZ;
tbAddBitMap[5].nID = IDB_BMORBITDOWNZ;
tbAddBitMap[6].nID = IDB_BMOPTIONSLAYERS;
tbAddBitMap[7].nID = IDB_BMDRAWINGBOX;
tbAddBitMap[8].nID = IDB_BMROTATE;
tbButton[0].idCommand = IDM_ORBITUPX;
tbButton[1].idCommand = IDM_ORBITDOWNX;
tbButton[2].idCommand = IDM_ORBITUPY;
tbButton[3].idCommand = IDM_ORBITDOWNY;
tbButton[4].idCommand = IDM_ORBITUPZ;
tbButton[5].idCommand = IDM_ORBITDOWNZ;
tbButton[6].idCommand = IDM_OPTIONSLAYERS;
tbButton[7].idCommand = IDM_OPTIONSDRAWINGBOX;
tbButton[8].idCommand = IDM_ROTATE;
::SendMessage(hwndToolBar, TB_SETBITMAPSIZE, (WPARAM) 0 , (LPARAM) MAKELONG(TOOLBAR_BUTTON_SIZE, TOOLBAR_BUTTON_SIZE));
for (i=0; i< QUANTITY_OF_BUTTONS; i++)
{
tbButton[i].iBitmap = i;
tbButton[i].fsState = TBSTATE_ENABLED;
tbButton[i].fsStyle = TBSTYLE_AUTOSIZE | TBSTYLE_BUTTON;
tbButton[i].dwData = 0;
tbButton[i].iString = 0;
::SendMessage(hwndToolBar, TB_ADDBITMAP, (WPARAM) 0 , (LPARAM)&tbAddBitMap[i]);
}
::SendMessage(hwndToolBar, TB_SETBUTTONSIZE, (WPARAM) 0 , (LPARAM) MAKELONG(TOOLBAR_BUTTON_SIZE, TOOLBAR_BUTTON_SIZE));
::SendMessage(hwndToolBar, TB_ADDBUTTONS , (WPARAM)i , (LPARAM)&tbButton);
::SendMessage(hwndToolBar, TB_AUTOSIZE, (WPARAM)0 , (LPARAM)0 );
::ShowWindow(hwndToolBar, SW_SHOWNORMAL);
}
void CMainWindow::DoCreateComboBox(HINSTANCE hInst)
{
if (!hwndToolBar) return;
if (CADImage)
{
char LayoutName[100];
int i, defaultLayout, Count;
Count = CADLayoutsCount(CADImage);
defaultLayout = DefaultLayoutIndex(CADImage);
if (hwndComboBox)
DestroyWindow(hwndComboBox);
hwndComboBox = ::CreateWindowEx(
0,
"COMBOBOX",
(LPCTSTR) NULL,
CBS_DROPDOWNLIST | WS_CHILD,
(TOOLBAR_BUTTON_SIZE+8)*QUANTITY_OF_BUTTONS, 4, 120, 40+COMBOBOX_ITEM_SIZE*Count,
hwndToolBar,
NULL,
hInst,
NULL);
for (i= 0; i < Count; ++i)
{
CADLayoutName(CADImage, i, LayoutName, 100);
::SendMessage(hwndComboBox, (UINT) CB_ADDSTRING, 0, (LPARAM) (LPCTSTR) LayoutName);
}
::SendMessage(hwndComboBox, (UINT) CB_SETCURSEL, (WPARAM) defaultLayout, 0);
RePaint();
::ShowWindow(hwndComboBox, SW_SHOWNORMAL);
}
}
int CMainWindow::SetBorder()
{
if (CADImage != NULL)
{
SetCADBorderType(CADImage, iBorderType);
if (iBorderType == 1)
SetCADBorderSize(CADImage, dBorderSize / 100.0);
else
SetCADBorderSize(CADImage, dBorderSize);
SetCurrentLayout();
}
return 1;
}
bool CMainWindow::SetCurrentLayout()
{
int lindex;
lindex = ::SendMessage(hwndComboBox, (UINT) CB_GETCURSEL, 0, 0);
ResetDrawingBox();
CurrentLayoutCAD(CADImage, lindex, TRUE);
RecalculateExtents();
SetScale(100);
RePaint();
return true;
}
bool CMainWindow::Is3D()
{
int is3d;
GetIs3dCAD(CADImage, &is3d);
if (bRotated3D || (is3d == 1) )
return true;
return false;
}
void CMainWindow::DoMousePosition(POINTS PointOnScr)
{
if ((!CADImage) || (ScaleRect.x < 0 )) return;
FPOINT newmousePt;
POINT NearestPoint;
char str[36];
char NearestEntityName[100];
float sX, sY;
if (Is3D())
{
SetTextToStatusBar("Is 3D drawing", 1);
return;
}
NearestPoint.x = PointOnScr.x - offset.x;
NearestPoint.y = PointOnScr.y - offset.y;
sX = (float)((PointOnScr.x - offset.x) * ScaleRect.x / fAbsWidth);
sY = (float)(1 - (PointOnScr.y - offset.y) * ScaleRect.y / fAbsHeight);
GetCADCoords(CADImage, sX, sY, &newmousePt);
sprintf(str, "(%6.6f, %6.6f)\0", newmousePt.x, newmousePt.y);
SetTextToStatusBar(str, 1);
if (GetIsNearestPointMode())
{
GetNearestEntity(CADImage, NearestEntityName, 100, &CADDraw.R, (LPPOINT)&NearestPoint);
DrawNearestMark(NearestPoint, &OldNearestPoint);
SetTextToStatusBar(NearestEntityName, 2);
}
}
void CMainWindow::SplitStatusBar(int nParts)
{
RECT rcClient;
HLOCAL hloc;
LPINT lpParts;
int i, nWidth;
GetClientRect(hWnd, &rcClient);
hloc = LocalAlloc(LHND, sizeof(int) * nParts);
lpParts = (LPINT)LocalLock(hloc);
nWidth = rcClient.right / nParts;
for (i = 0; i < nParts; i++)
{
lpParts[i] = nWidth;
nWidth+= nWidth;
}
::SendMessage(hwndStatusBar, SB_SETPARTS, (WPARAM) nParts, (LPARAM) lpParts);
LocalUnlock(hloc);
LocalFree(hloc);
}
void CMainWindow::ReSize(WPARAM wParam,LPARAM lParam)
{
SplitStatusBar();
::SendMessage(hwndStatusBar, WM_SIZE, wParam, lParam);
::SendMessage(hwndToolBar, TB_AUTOSIZE, (WPARAM)0 , (LPARAM)0 );
}
void CMainWindow::RotateCAD(const AXES axis, const float angle)
{
if (CADImage && !optionsCAD.IsDrawingBox)
{
SetRotateCAD(CADImage, angle, int(axis));
bIsRotated = true;
if (axis != axisZ)
{
bRotated3D = true;
SetTextToStatusBar("", 2);
}
RePaint();
}
}
void CMainWindow::SetBgrndColor(const COLORREF color)
{
colorBgrnd = color;
RECT rect;
GetClientRect(hWnd, &rect);
LOGBRUSH brush;
brush.lbStyle = BS_SOLID;
brush.lbColor = colorBgrnd;
brush.lbHatch = 0;
brushBackground = CreateBrushIndirect(&brush);
FillRect(hMainWndDC, &rect, brushBackground);
DeleteObject(brushBackground);
RePaint();
}
void CMainWindow::ShowAboutDlg()
{
::InitCommonControls();
DialogBox(hInstance, MAKEINTRESOURCE(IDD_ABOUT), hWnd, AboutDialogProc);
}
void CMainWindow::ShowProgressDlg(bool Visible)
{
if (hwndProgressDlg == NULL)
hwndProgressDlg = CreateDialog(hInstance, MAKEINTRESOURCE(IDD_PROGRESS), hWnd, ProgressDialogProc);
if (Visible)
{
SetWindowLong(hwndProgressDlg, GWL_USERDATA, (LONG) this);
SendMessage(GetDlgItem(hwndProgressDlg, IDC_PROGRESS), (UINT) PBM_SETRANGE, (WPARAM) 0, (LPARAM) MAKELPARAM (0, 100));
SendMessage(GetDlgItem(hwndProgressDlg, IDC_PROGRESS), (UINT) PBM_SETPOS, (WPARAM) 0, (LPARAM) 0);
ShowWindow(hwndProgressDlg, SW_SHOW);
SetFocus(hwndProgressDlg);
}
else
{
ShowWindow(hwndProgressDlg, SW_HIDE);
SetFocus(hWnd);
}
}
void CMainWindow::SetProgressValue(BYTE PercentDone)
{
SendMessage(GetDlgItem(hwndProgressDlg, IDC_PROGRESS), (UINT) PBM_SETPOS, (WPARAM) PercentDone, (LPARAM) 0);
UpdateWindow(GetDlgItem(hwndProgressDlg, IDC_PROGRESS));
}
bool CMainWindow::StretchDrawDIB (HGLOBAL hMemDIB, HDC hDC, RECT * R)
{
HBITMAP hBmp;
BITMAPINFO *BitmapInfo;
HDC hCompatibleDC;
OSVERSIONINFO vx;
int bltMode;
int BmpWidth, BmpHeight;
int NumColor;
void *P = NULL;
if (hMemDIB)
{
P = GlobalLock(hMemDIB);
BitmapInfo = (BITMAPINFO *) malloc(sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD));
memcpy(&BitmapInfo->bmiHeader, P, sizeof(BITMAPINFOHEADER));
P = (void *)(int(P) + sizeof(BITMAPINFOHEADER));
NumColor = BitmapInfo->bmiHeader.biClrUsed;
if (!NumColor)
{
NumColor = BitmapInfo->bmiHeader.biBitCount;
if (NumColor > 8) NumColor = 0;
else NumColor = 1 << NumColor;
}
void * Colors = (void *)(int(BitmapInfo) + BitmapInfo->bmiHeader.biSize);
memcpy(Colors, P, NumColor * sizeof(RGBQUAD));
P = (void *)(int(P) + NumColor * sizeof(RGBQUAD));
BmpWidth = BitmapInfo->bmiHeader.biWidth;
BmpHeight = BitmapInfo->bmiHeader.biHeight;
if (BmpHeight < 0) BmpHeight = -BmpHeight;
void *BitsMem = NULL;
hBmp = CreateDIBSection(hDC, BitmapInfo, DIB_RGB_COLORS, &BitsMem, 0, 0);
NumColor = BmpHeight * ((BmpWidth * BitmapInfo->bmiHeader.biBitCount + 31 & -32) >> 3);
memcpy(BitsMem, P, NumColor);
free(BitmapInfo);
hCompatibleDC = CreateCompatibleDC(hDC);
HGDIOBJ OldObject = SelectObject(hCompatibleDC, hBmp);
bltMode = GetStretchBltMode(hDC);
GetVersionEx(&vx);
if(vx.dwPlatformId==VER_PLATFORM_WIN32_NT)
SetStretchBltMode(hDC,HALFTONE);
else
SetStretchBltMode(hDC,COLORONCOLOR);
StretchBlt(hDC, 0, 0, R->right, R->bottom, hCompatibleDC, 0, 0, BmpWidth, BmpHeight, SRCCOPY);
SetStretchBltMode(hDC, bltMode);
SelectObject(hCompatibleDC, OldObject);
DeleteObject(hBmp);
DeleteDC(hCompatibleDC);
}
if (hMemDIB)
GlobalFree(hMemDIB);
return TRUE;
}
void CMainWindow::ShowPictureDlg(bool Visible)
{
if (hwndPictureDlg == NULL)
{
hwndPictureDlg = CreateDialog(hInstance, (LPCTSTR)IDD_PICTURE, hWnd, (DLGPROC)PictureDialogProc);
SetWindowLong(hwndPictureDlg, GWL_USERDATA, (LONG) this);
}
if (Visible)
{
ShowWindow(hwndPictureDlg, SW_SHOW);
SetFocus(hwndPictureDlg);
}
else
{
ShowWindow(hwndPictureDlg, SW_HIDE);
SetFocus(hWnd);
}
}
void CMainWindow::ShowLayersDlg(bool Visible)
{
if (hwndLayersDlg == NULL)
hwndLayersDlg = CreateDialog(hInstance, (LPCTSTR)IDD_LAYERS, hWnd, (DLGPROC)LayersDialogProc);
if (Visible)
{
ShowWindow(hwndLayersDlg, SW_SHOW);
SetFocus(hwndLayersDlg);
}
else
{
ShowWindow(hwndLayersDlg, SW_HIDE);
SetFocus(hWnd);
}
}
void CMainWindow::ShowPropertiesDlg(bool Visible)
{
if (hwndPropertiesDlg == NULL)
hwndPropertiesDlg = CreateDialog(hInstance, (LPCTSTR)IDD_PROPERTIES, hWnd, (DLGPROC) PropertiesDialogProc);
if (Visible)
{
SetWindowLong(hwndPropertiesDlg, GWL_USERDATA, (LONG) this);
SendMessage(hwndPropertiesDlg, WM_INITDIALOG, 0, 0);
ShowWindow(hwndPropertiesDlg, SW_SHOW);
SetFocus(hwndPropertiesDlg);
}
else
{
ShowWindow(hwndPropertiesDlg, SW_HIDE);
SetFocus(hWnd);
}
}
void CMainWindow::ResetDrawingBox()
{
if ((CADImage != NULL) && optionsCAD.IsDrawingBox && !bIsRotated)
{
ResetDrawingBoxCAD(CADImage);
RecalculateExtents();
optionsCAD.IsDrawingBox = false;
RePaint();
}
}
void CMainWindow::SetDrawingBox()
{
optionsCAD.IsDrawingBox = true;
if ((CADImage != NULL) && !bIsRotated)
{
rectDrawingBox.Points.Left = (frectExtentsCAD.Points.Left + frectExtentsCAD.Points.Right)/2;
rectDrawingBox.Points.Top = frectExtentsCAD.Points.Top;
rectDrawingBox.Points.Z1 = 0;
rectDrawingBox.Points.Right = frectExtentsCAD.Points.Right;
rectDrawingBox.Points.Bottom = frectExtentsCAD.Points.Bottom;
rectDrawingBox.Points.Z2 = 0;
SetDrawingBoxCAD(CADImage, &rectDrawingBox);
RecalculateExtents();
RePaint();
}
}
void CMainWindow::DrawNearestMark(POINT NewPoint, LPPOINT OldPoint)
{
RECT NewR, OldR;
HPEN hPen = CreatePen(PS_SOLID, 1, 0xFF0000);
HPEN hOldPen = (HPEN)SelectObject(hMainWndDC, hPen);
HBRUSH hBrush;
HBRUSH hOldBrush;
LOGBRUSH LogBrush;
int Index = SaveDC(hMainWndDC);
LogBrush.lbStyle = BS_SOLID;
LogBrush.lbHatch = 0;
LogBrush.lbColor = 0xFF0000;
hBrush = CreateBrushIndirect(&LogBrush);
hOldBrush = (HBRUSH)SelectObject(hMainWndDC, hBrush);
hOldPen = (HPEN)SelectObject(hMainWndDC, hPen);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -