📄 picdlg2.cpp
字号:
// PicDlg2.cpp : implementation file
//
#include "stdafx.h"
#include "LandSoft.h"
#include "PicDlg2.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPicDlg2 dialog
CPicDlg2::CPicDlg2(CWnd* pParent,long * resultarray, BYTE typenum ,UINT boxnum ,UINT xzoom,BOOL stand)
: CDialog(CPicDlg2::IDD, pParent)
{
//{{AFX_DATA_INIT(CPicDlg2)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_bStand=stand;
m_pArray=resultarray;
m_nTypeNum=typenum;
m_nWhichSelect=0;
m_nBoxNum=boxnum;
m_nXZoom=xzoom;
}
void CPicDlg2::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPicDlg2)
DDX_Control(pDX, IDC_WHICHSHOW2, m_WhichShow);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPicDlg2, CDialog)
//{{AFX_MSG_MAP(CPicDlg2)
ON_CBN_SELCHANGE(IDC_WHICHSHOW2, OnSelchangeWhichshow2)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_COPYTO, OnCopyto)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPicDlg2 message handlers
void CPicDlg2::OnSelchangeWhichshow2()
{
m_nWhichSelect=m_WhichShow.GetCurSel();
CRect rect(10,30,450,350);
RedrawWindow(&rect);
}
void CPicDlg2::OnPaint()
{
CPen *pen;
CPaintDC dc(this); // device context for painting
CBrush * cbBack=new CBrush(RGB(240,197,170));
CRect rect(42,42,442,342);
dc.FillRect(rect,cbBack);
delete cbBack;
UINT x0=42;
UINT y0=342;
pen=new CPen(PS_SOLID,1,RGB(3,88,33));
dc.SelectObject(pen);
dc.SetBkMode(TRANSPARENT);
UINT number=0;
for(UINT xx=0;xx<=400;xx+=m_nXZoom)
{
CString tmpstr;
tmpstr.Format("%d",number);
dc.TextOut(x0+xx-4,y0+8,tmpstr);
dc.MoveTo(x0+xx,y0);
dc.LineTo(x0+xx,y0+8);
UINT x2=xx+m_nXZoom/2+0.5;
if(x2>400)
break;
tmpstr.Format("%.1f",number+0.5);
dc.TextOut(x0+x2-4,y0+8,tmpstr);
dc.MoveTo(x0+x2,y0);
dc.LineTo(x0+x2,y0+8);
number++;
}
UINT MaxY;
if(m_bStand)
{
MaxY=1;
}
else
{
MaxY=m_pArray[m_nWhichSelect+m_nBoxNum]/10000+1;
}
UINT YZoom=300/MaxY;
number=0;
for(UINT yy=0;yy<=300;yy+=YZoom)
{
CString tmpstr;
tmpstr.Format("%d",number);
dc.TextOut(x0-20,y0-yy-8,tmpstr);
dc.MoveTo(x0,y0-yy);
dc.LineTo(x0-6,y0-yy);
UINT y2=yy+YZoom/2+0.5;
if(y2>300)
break;
tmpstr.Format("%.1f",number+0.5);
dc.TextOut(x0-26,y0-y2-8,tmpstr);
dc.MoveTo(x0,y0-y2);
dc.LineTo(x0-6,y0-y2);
number++;
}
delete pen;
pen=new CPen(PS_SOLID,1,RGB(255,0,0));
dc.SelectObject(pen);
dc.MoveTo(x0+m_pArray[0],y0-m_pArray[m_nBoxNum+m_nWhichSelect]*YZoom/10000);
for(UINT box=1;box<m_nBoxNum;box++)
{
dc.LineTo(x0+m_pArray[box],
y0-m_pArray[m_nBoxNum+m_nWhichSelect+box*m_nTypeNum]*YZoom/10000-0.5);
}
delete pen;
cbBack=new CBrush(RGB(0,255,0));
for(UINT box2=0;box2<m_nBoxNum;box2++)
{
UINT yyy=m_pArray[m_nBoxNum+m_nWhichSelect+box2*m_nTypeNum]*YZoom/10000+0.5;
CRect rect(x0+m_pArray[box2]-2,y0-yyy-2,x0+m_pArray[box2]+2,y0-yyy+2);
dc.FillRect(rect,cbBack);
}
delete cbBack;
}
BOOL CPicDlg2::OnInitDialog()
{
CDialog::OnInitDialog();
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 CPicDlg2::OnCopyto()
{
if(OpenClipboard())
{
HDIB hDib;
HBITMAP hBitmap;
HPALETTE hPal;
EmptyClipboard();
RECT rect;
GetWindowRect(&rect);
rect.left+=9;
rect.top+=20;
rect.right=rect.left+460;
rect.bottom=rect.top+407;
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 CPicDlg2::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 CPicDlg2::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 CPicDlg2::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 CPicDlg2::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 + -