一、图片上绘制线条
//在图片上绘制线条
void CGDIControlView::OnMenuitemDrawlineonimage()
{
Graphics graphics(m_hWnd);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
Rect destRect(width+20, 10, width, height);
graphics.DrawImage(&image, 10, 10, width, height);
graphics.DrawImage(&image, destRect, 0, 0, width, height, UnitPixel);
Pen pen(Color(255, 255, 0, 0), 3);
int nLineCount = 18;
int h = height/(nLineCount-1);
for (int i = 0;i <nLineCount; i++)
{
graphics.DrawLine(&pen, width+20, h*i+10, width*2 + 20, h*i+10);
}
}
二、图片上绘制网格
//在图片上绘制网格
void CGDIControlView::OnMenuitemDrawnetlineonimage()
{
Graphics graphics(m_hWnd);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
Rect destRect(width+20, 10, width, height);
graphics.DrawImage(&image, 10, 10, width, height);
graphics.DrawImage(&image, destRect, 0, 0, width, height, UnitPixel);
Pen pen(Color(255, 255, 0, 0), 3);
int nXCount = 18;
int nYCount = 18;
int h = height/(nXCount-1);
for (int i = 0;i <nXCount; i++)
{
graphics.DrawLine(&pen, width+20, h*i+10, width*2 + 20, h*i+10);
}
int w = width/(nYCount-1);
for ( int i = 0;i <nYCount; i++)
{
graphics.DrawLine(&pen, w*i+20+width, 10, w*i+20+width, 10+height);
}
}
三、打开高颜色质量图像
//打开高颜色质量图像
void CGDIControlView::OnMenuitemOpenhighimage()
{
Graphics graphics(m_hWnd);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.SetInterpolationMode(InterpolationModeHighQualityBicubic);
graphics.DrawImage(&image, 10, 10, width, height);
graphics.SetInterpolationMode(InterpolationModeNearestNeighbor);
graphics.DrawImage(&image, width + 20, 10, width, height);
}
四、创建最顶层窗体
typedef BOOL (WINAPI *SETLAYERFUNC)(HWND,COLORREF,BYTE,DWORD);
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
{
TRACE0("Failed to create status bar\n");
return -1; // fail to create
}
// TODO: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE, GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
HINSTANCE hInst = LoadLibrary("User32.DLL");
if(hInst)
{
SETLAYERFUNC func = NULL;
func=(SETLAYERFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
if(func) func(this->GetSafeHwnd() ,0, 200, 2);
else MessageBox("装载函数失败");
FreeLibrary(hInst);
}
::SetWindowPos(this->GetSafeHwnd(),HWND_TOPMOST, 0, 0, 300, 180, SWP_SHOWWINDOW);
return 0;
}
五、视图中拖动图片
1.处理按钮命令事件
//在视图中拖动图片
void CGDIControlView::OnMenuitemDrag()
{
OperType = 3;
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
Bitmap image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
rect.left = 0;
rect.top = 0;
rect.right = width;
rect.bottom = height;
}
2.处理鼠标按下事件
void CGDIControlView::OnLButtonDown(UINT nFlags, CPoint point)
{
beginPoint = point;
if(OperType == 3)
{
Graphics graphics(m_hWnd);
Region region(Rect(rect.left, rect.top, rect.right, rect.bottom));
PointF backPoint((float)point.x, (float)point.y);
if(region.IsVisible(backPoint, &graphics))
{
bSelectedImage = TRUE;
forePoint.x = point.x;
forePoint.y = point.y;
}
}
CView::OnLButtonDown(nFlags, point);
}
3.处理鼠标抬起事件
//鼠标抬起事件
void CGDIControlView::OnLButtonUp(UINT nFlags, CPoint point)
{
endPoint = point;
if (OperType == 1)
{//保留椭圆中的内容
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
GraphicsPath path;
path.AddEllipse(beginPoint.x, beginPoint.y,
(endPoint.x-beginPoint.x), (endPoint.y-beginPoint.y));
Region region(&path);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
graphics.SetClip(®ion);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 0;
}
else if (OperType == 2)
{//去除椭圆中的内容
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
CRect rect;
GetClientRect(&rect);
Region region1(Rect(rect.left, rect.top, rect.right, rect.bottom));
GraphicsPath path;
path.AddEllipse(beginPoint.x, beginPoint.y,
(endPoint.x-beginPoint.x), (endPoint.y-beginPoint.y));
Region region2(&path);
region1.Exclude(®ion2);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
graphics.SetClip(®ion1);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 0;
}
else if (OperType == 3)
{//拖动图片
if (bSelectedImage)
{
bSelectedImage = FALSE;
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
int offX = point.x - forePoint.x;
int offY = point.y - forePoint.y;
rect.left += offX;
rect.top += offY;
graphics.DrawImage(&image, rect.left, rect.top, width, height);
}
}
CView::OnLButtonUp(nFlags, point);
}
六、屏幕截图
//屏幕截图
void CGDIControlView::OnMenuitemScreencut()
{
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
HDC hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
Graphics graphics1(hScrDC);
Bitmap bitmap(cx, cy, &graphics1);
Graphics graphics2(&bitmap);
HDC dc1 = graphics1.GetHDC();
HDC dc2 = graphics2.GetHDC();
BitBlt(dc2, 0, 0, cx, cy, dc1, 0, 0, 13369376);
graphics1.ReleaseHDC(dc1);
graphics2.ReleaseHDC(dc2);
Graphics graphics(m_hWnd);
UINT width = bitmap.GetWidth();
UINT height = bitmap.GetHeight();
graphics.DrawImage(&bitmap, 0, 0, width, height);
}
七、保存屏幕图像到剪贴板
//保存屏幕图像到剪贴板
void CGDIControlView::OnMenuitemScreencopy()
{
CDC* pDC = GetDC();
int cx = GetSystemMetrics(SM_CXSCREEN);
int cy = GetSystemMetrics(SM_CYSCREEN);
HDC hScrDC = CreateDC("DISPLAY", NULL, NULL, NULL);
HBITMAP hBitmap = CreateCompatibleBitmap(hScrDC, cx, cy);
HGLOBAL hGlobal =(HGLOBAL)(UINT)hBitmap;
OpenClipboard();
SetClipboardData(MF_BITMAP, (HANDLE)hGlobal);
CloseClipboard();
}
八、获取图像RGB值
//获取图像RGB值
void CGDIControlView::OnMenuitemGetrgb()
{
Graphics graphics(m_hWnd);
Bitmap image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 30, width, height);
Color color;
image.GetPixel(width/2, height/2, &color);
CString log;
log.Format("图像中心点的颜色值为:Alpha=%d;Red=%d;Green=%d;Blue=%d",
color.GetAlpha(), color.GetRed(), color.GetGreen(), color.GetBlue());
CDC* pDC = GetDC();
pDC->TextOut(0, 0, log);
}
九、渐隐渐现的图像
//渐隐渐显的图像
DWORD WINAPI ThreadShimageFunc( LPVOID lpParam )
{
Graphics graphics((HWND)lpParam);
graphics.SetSmoothingMode(SmoothingModeAntiAlias);
Bitmap image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
TextureBrush brush(&image);
graphics.FillRectangle(&brush, Rect(0,0,width,height));
for (int i = 0; i < 255; i++)
{//渐隐
SolidBrush solidBrush(Color(i,0xA9,0xA9, 0xA9));
graphics.FillRectangle(&solidBrush, Rect(0,0,width,height));
Sleep(100);
}
graphics.Clear(Color::White);
for (int i = 0; i <255; i++)
{//渐现
Color color, colorTemp;
for(INT iRow = 0; iRow < (INT)height; iRow++)
{
for(INT iColumn = 0; iColumn < (INT)width; iColumn++)
{
image.GetPixel(iColumn, iRow, &color);
colorTemp.SetValue(color.MakeARGB(i, color.GetRed(),color.GetGreen(), color.GetBlue()));
image.SetPixel(iColumn, iRow, colorTemp);
}
}
graphics.DrawImage(&image, 0, 0, width, height);
Sleep(100);
}
return 0;
}
void CGDIControlView::OnMenuitemShimage()
{
DWORD dwThreadId;
hThread = CreateThread(NULL,0, (LPTHREAD_START_ROUTINE)ThreadShimageFunc, (LPVOID)m_hWnd, 0, &dwThreadId);
}
十、保留椭圆中图片内容
1.处理按钮事件
//保留椭圆下图片内容
void CGDIControlView::OnMenuitemSaveellipcontent()
{
Graphics graphics(m_hWnd);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 1;
}
2.处理鼠标抬起事件
//鼠标抬起事件
void CGDIControlView::OnLButtonUp(UINT nFlags, CPoint point)
{
endPoint = point;
if (OperType == 1)
{//保留椭圆中的内容
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
GraphicsPath path;
path.AddEllipse(beginPoint.x, beginPoint.y,
(endPoint.x-beginPoint.x), (endPoint.y-beginPoint.y));
Region region(&path);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
graphics.SetClip(®ion);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 0;
}
}
十一、去除椭圆下的图片内容
1.处理按钮事件
//去除椭圆下的图片内容
void CGDIControlView::OnMenuitemCutellipcontent()
{
Graphics graphics(m_hWnd);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 2;
}
2.处理鼠标抬起事件
//鼠标抬起事件
void CGDIControlView::OnLButtonUp(UINT nFlags, CPoint point)
{
endPoint = point;
if (OperType == 1)
{//保留椭圆中的内容
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
GraphicsPath path;
path.AddEllipse(beginPoint.x, beginPoint.y,
(endPoint.x-beginPoint.x), (endPoint.y-beginPoint.y));
Region region(&path);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
graphics.SetClip(®ion);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 0;
}
else if (OperType == 2)
{//去除椭圆中的内容
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
CRect rect;
GetClientRect(&rect);
Region region1(Rect(rect.left, rect.top, rect.right, rect.bottom));
GraphicsPath path;
path.AddEllipse(beginPoint.x, beginPoint.y,
(endPoint.x-beginPoint.x), (endPoint.y-beginPoint.y));
Region region2(&path);
region1.Exclude(®ion2);
Pen pen(Color(255, 0, 0, 255));
graphics.DrawPath(&pen, &path);
graphics.SetClip(®ion1);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
graphics.DrawImage(&image, 0, 0, width, height);
OperType = 0;
}
else if (OperType == 3)
{//拖动图片
if (bSelectedImage)
{
bSelectedImage = FALSE;
Graphics graphics(m_hWnd);
graphics.Clear(Color::White);
Image image(L"baby.JPG");
UINT width = image.GetWidth();
UINT height = image.GetHeight();
int offX = point.x - forePoint.x;
int offY = point.y - forePoint.y;
rect.left += offX;
rect.top += offY;
graphics.DrawImage(&image, rect.left, rect.top, width, height);
}
}
CView::OnLButtonUp(nFlags, point);
}
十二、显示word艺术字
//显示Word艺术字
void CGDIControlView::OnMenuitemShowword()
{
Graphics graphics(m_hWnd);
SolidBrush backBrush(Color(255, 255, 255, 0));
SolidBrush foreBrush(Color(255, 255, 0, 0));
FontFamily fontFamily(L"幼圆");
Font font(&fontFamily, 40, FontStyleRegular, UnitPixel);
PointF point1(42, 2);
PointF point2(40, 0);
graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);
graphics.RotateTransform(30.0f);
graphics.DrawString(L"艺术字", -1, &font, point1, &foreBrush);
graphics.DrawString(L"艺术字", -1, &font, point2, &backBrush);
}