📄 picdlg.cpp
字号:
// PicDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LandSoft.h"
#include "PicDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPictureDlg dialog
CPictureDlg::CPictureDlg(CWnd* pParent,BYTE type,long * resultarray,
BYTE typenum,UINT slipnum)
: CDialog(CPictureDlg::IDD, pParent)
{
char * name;
switch(type)
{
case 1:
name="该段面积和-->";
break;
case 2:
name="该段周长和-->";
break;
}
//{{AFX_DATA_INIT(CPictureDlg)
m_Type = _T(name);
//}}AFX_DATA_INIT
m_pArray=resultarray;
m_nTypeNum=typenum;
m_nSlipNum=slipnum;
m_nWhichSelect=0;
}
void CPictureDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPictureDlg)
DDX_Control(pDX, IDC_WHICHSHOW, m_WhichShow);
DDX_Text(pDX, IDC_10, m_Type);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPictureDlg, CDialog)
//{{AFX_MSG_MAP(CPictureDlg)
ON_WM_PAINT()
ON_CBN_SELCHANGE(IDC_WHICHSHOW, OnSelchangeWhichshow)
ON_BN_CLICKED(IDC_COPYTO, OnCopyto)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPictureDlg message handlers
void CPictureDlg::OnPaint()
{
CPen * pen;
CPaintDC dc(this); // device context for painting
CBrush* cbBack=new CBrush(RGB(240,197,170));
CRect rect(42,42,442,302);
dc.FillRect(rect,cbBack);
delete cbBack;
UINT x0=42;
UINT y0=302;
double width=400/(m_nSlipNum-1);
pen=new CPen(PS_SOLID,1,RGB(3,88,33));
dc.SelectObject(pen);
dc.SetBkMode(TRANSPARENT);
for(int i5=0;i5<=10;i5=i5+2)
{
CString tmpstr;
tmpstr.Format("%.1f",float(i5)/10);
dc.TextOut(x0-26,y0-i5*26-8,tmpstr,3);
dc.MoveTo(x0,y0-i5*26);
dc.LineTo(x0-6,y0-i5*26);
}
for(UINT j5=1;j5<=m_nSlipNum;)
{
CString tmpstr;
tmpstr.Format("%d",j5);
dc.TextOut(x0+width*(j5-1)-4,y0+8,tmpstr);
dc.MoveTo(x0+width*(j5-1),y0);
dc.LineTo(x0+width*(j5-1),y0+8);
if(m_nSlipNum<=11)
j5++;
else
j5+=(m_nSlipNum/10+1);
}
delete pen;
long E=0;
long En=0;
POINT * nLine=(POINT *)malloc(sizeof(POINT)*m_nSlipNum);
POINT * Line=(POINT *)malloc(sizeof(POINT)*m_nSlipNum);
for(UINT i=0;i<m_nSlipNum;i++)
{
nLine[i].y=m_pArray[m_nWhichSelect*2*m_nSlipNum+i];
En+=nLine[i].y;
Line[i].y=m_pArray[(m_nWhichSelect*2+1)*m_nSlipNum+i];
E+=Line[i].y;
}
double nHeight=260.0/double(En);
double Height=260.0/double(E);
for(UINT j=0;j<m_nSlipNum;j++)
{
nLine[j].x=x0+j*width;
nLine[j].y=y0-nLine[j].y*nHeight;
Line[j].x=nLine[j].x;
Line[j].y=y0-Line[j].y*Height;
}
//画线
pen=new CPen(PS_SOLID, 2, RGB(255,0,0));
dc.SelectObject(pen);
dc.MoveTo(Line[0]);
for (UINT i1=1; i1 < m_nSlipNum; i1++)
{
dc.LineTo(Line[i1]);
}
delete pen;
//画线
pen=new CPen(PS_SOLID,1,RGB(0,0,255));
dc.SelectObject(pen);
dc.MoveTo(nLine[0]);
for(UINT j1=1;j1<m_nSlipNum;j1++)
{
dc.LineTo(nLine[j1]);
}
delete pen;
//画点
cbBack=new CBrush(RGB(0,0,0));
for(UINT i0=0;i0<m_nSlipNum;i0++)
{
CRect rect(Line[i0].x-4,Line[i0].y-4,Line[i0].x+4,Line[i0].y+4);
dc.FillRect(rect,cbBack);
}
delete cbBack;
//画点
cbBack=new CBrush(RGB(255,255,255));
for(UINT j0=0;j0<m_nSlipNum;j0++)
{
CRect rect(nLine[j0].x-2,nLine[j0].y-2,nLine[j0].x+2,nLine[j0].y+2);
dc.FillRect(rect,cbBack);
}
delete cbBack;
}
BOOL CPictureDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_WhichShow.AddString("总体");
for(BYTE i=0;i<m_nTypeNum;i++)
{
CString tmpstr;
tmpstr.Format("第 %d 类",i+1);
m_WhichShow.AddString(tmpstr);
}
m_WhichShow.SetCurSel(m_nWhichSelect);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPictureDlg::OnSelchangeWhichshow()
{
m_nWhichSelect=m_WhichShow.GetCurSel();
CRect rect(0,0,500,500);
RedrawWindow(&rect);
}
void CPictureDlg::OnCopyto()
{
if(OpenClipboard())
{
HDIB hDib;
HBITMAP hBitmap;
HPALETTE hPal;
EmptyClipboard();
RECT rect;
GetWindowRect(&rect);
rect.left+=9;
rect.top+=24;
rect.right=rect.left+460;
rect.bottom=rect.top+386;
hBitmap=CopyScreenToBitmap(&rect);
SetClipboardData(CF_BITMAP,hBitmap);
if (hPal =(HPALETTE)GetStockObject(DEFAULT_PALETTE))
SetClipboardData(CF_PALETTE, hPal);
if (hDib = BitmapToDIB(hBitmap, hPal))
SetClipboardData(CF_DIB, hDib);
CloseClipboard();
}
}
HBITMAP CPictureDlg::CopyScreenToBitmap(LPRECT lpRect)
{
HDC hScrDC, hMemDC; // screen DC and memory DC
HBITMAP hBitmap, hOldBitmap; // handles to deice-dependent bitmaps
int nX, nY, nX2, nY2; // coordinates of rectangle to grab
int nWidth, nHeight; // DIB width and height
int xScrn, yScrn; // screen resolution
// check for an empty rectangle
if (IsRectEmpty(lpRect))
return NULL;
// create a DC for the screen and create
// a memory DC compatible to screen DC
hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
hMemDC = CreateCompatibleDC(hScrDC);
// get points of rectangle to grab
nX = lpRect->left;
nY = lpRect->top;
nX2 = lpRect->right;
nY2 = lpRect->bottom;
// get screen resolution
xScrn = GetDeviceCaps(hScrDC, HORZRES);
yScrn = GetDeviceCaps(hScrDC, VERTRES);
//make sure bitmap rectangle is visible
if (nX < 0)
nX = 0;
if (nY < 0)
nY = 0;
if (nX2 > xScrn)
nX2 = xScrn;
if (nY2 > yScrn)
nY2 = yScrn;
nWidth = nX2 - nX;
nHeight = nY2 - nY;
// create a bitmap compatible with the screen DC
hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
// select new bitmap into memory DC
hOldBitmap = (HBITMAP) SelectObject(hMemDC, hBitmap);
// bitblt screen DC to memory DC
BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, nX, nY, SRCCOPY);
// select old bitmap back into memory DC and get handle to
// bitmap of the screen
hBitmap = (HBITMAP) SelectObject(hMemDC, hOldBitmap);
// clean up
DeleteDC(hScrDC);
DeleteDC(hMemDC);
// return handle to the bitmap
return hBitmap;
}
HDIB CPictureDlg::BitmapToDIB(HBITMAP hBitmap, HPALETTE hPal)
{
BITMAP bm; // bitmap structure
BITMAPINFOHEADER bi; // bitmap header
LPBITMAPINFOHEADER lpbi; // pointer to BITMAPINFOHEADER
DWORD dwLen; // size of memory block
HANDLE hDIB, h; // handle to DIB, temp handle
HDC hDC; // handle to DC
WORD biBits; // bits per pixel
// check if bitmap handle is valid
if (!hBitmap)
return NULL;
// fill in BITMAP structure, return NULL if it didn't work
if (!GetObject(hBitmap, sizeof(bm), (LPSTR)&bm))
return NULL;
// if no palette is specified, use default palette
if (hPal == NULL)
hPal =(HPALETTE)GetStockObject(DEFAULT_PALETTE);
// calculate bits per pixel
biBits = bm.bmPlanes * bm.bmBitsPixel;
// make sure bits per pixel is valid
if (biBits <= 1)
biBits = 1;
else if (biBits <= 4)
biBits = 4;
else if (biBits <= 8)
biBits = 8;
else // if greater than 8-bit, force to 24-bit
biBits = 24;
// initialize BITMAPINFOHEADER
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bm.bmWidth;
bi.biHeight = bm.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = biBits;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
// calculate size of memory block required to store BITMAPINFO
dwLen = bi.biSize + PaletteSize((LPSTR)&bi);
// get a DC
hDC = ::GetDC(NULL);
// select and realize our palette
hPal = SelectPalette(hDC, hPal, FALSE);
RealizePalette(hDC);
// alloc memory block to store our bitmap
hDIB = GlobalAlloc(GHND, dwLen);
// if we couldn't get memory block
if (!hDIB)
{
// clean up and return NULL
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL,hDC);
return NULL;
}
// lock memory and get pointer to it
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
/// use our bitmap info. to fill BITMAPINFOHEADER
*lpbi = bi;
// call GetDIBits with a NULL lpBits param, so it will calculate the
// biSizeImage field for us
GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, NULL, (LPBITMAPINFO)lpbi,
DIB_RGB_COLORS);
// get the info. returned by GetDIBits and unlock memory block
bi = *lpbi;
GlobalUnlock(hDIB);
// if the driver did not fill in the biSizeImage field, make one up
if (bi.biSizeImage == 0)
bi.biSizeImage = WIDTHBYTES((DWORD)bm.bmWidth * biBits) * bm.bmHeight;
// realloc the buffer big enough to hold all the bits
dwLen = bi.biSize + PaletteSize((LPSTR)&bi) + bi.biSizeImage;
if (h = GlobalReAlloc(hDIB, dwLen, 0))
hDIB = h;
else
{
// clean up and return NULL
GlobalFree(hDIB);
hDIB = NULL;
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL,hDC);
return NULL;
}
// lock memory block and get pointer to it */
lpbi = (LPBITMAPINFOHEADER)GlobalLock(hDIB);
// call GetDIBits with a NON-NULL lpBits param, and actualy get the
// bits this time
if (GetDIBits(hDC, hBitmap, 0, (UINT)bi.biHeight, (LPSTR)lpbi +
(WORD)lpbi->biSize + PaletteSize((LPSTR)lpbi), (LPBITMAPINFO)lpbi,
DIB_RGB_COLORS) == 0)
{
// clean up and return NULL
GlobalUnlock(hDIB);
hDIB = NULL;
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL,hDC);
return NULL;
}
bi = *lpbi;
// clean up
GlobalUnlock(hDIB);
SelectPalette(hDC, hPal, TRUE);
RealizePalette(hDC);
::ReleaseDC(NULL,hDC);
// return handle to the DIB
return hDIB;
}
WORD CPictureDlg::PaletteSize(LPSTR lpDIB)
{
// calculate the size required by the palette
if (IS_WIN30_DIB (lpDIB))
return (DIBNumColors(lpDIB) * sizeof(RGBQUAD));
else
return (DIBNumColors(lpDIB) * sizeof(RGBTRIPLE));
}
WORD CPictureDlg::DIBNumColors(LPSTR lpDIB)
{
WORD wBitCount; // DIB bit count
// If this is a Windows-style DIB, the number of colors in the
// color table can be less than the number of bits per pixel
// allows for (i.e. lpbi->biClrUsed can be set to some value).
// If this is the case, return the appropriate value.
if (IS_WIN30_DIB(lpDIB))
{
DWORD dwClrUsed;
dwClrUsed = ((LPBITMAPINFOHEADER)lpDIB)->biClrUsed;
if (dwClrUsed)
return (WORD)dwClrUsed;
}
// Calculate the number of colors in the color table based on
// the number of bits per pixel for the DIB.
if (IS_WIN30_DIB(lpDIB))
wBitCount = ((LPBITMAPINFOHEADER)lpDIB)->biBitCount;
else
wBitCount = ((LPBITMAPCOREHEADER)lpDIB)->bcBitCount;
// return number of colors based on bits per pixel
switch (wBitCount)
{
case 1:
return 2;
case 4:
return 16;
case 8:
return 256;
default:
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -