www.pudn.com > 渐变填充.rar > 渐变填充.cpp, change:2005-04-19,size:8244b
// caiView.cpp : implementation of the CCaiView class
//
#include "stdafx.h"
#include "cai.h"
#include "math.h"
#include "caiDoc.h"
#include "caiView.h"
//#include <vector>
//#include <stack>
#include <string>
using namespace std ;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//#include <iostream>
//int inp[2][2] = {{1, 1}, {2, 0}};
// 要放入模板中的2x2的正则数组
int count=0,i;
POINT point1,point2;
void seed(CDC* pDC);
//int k;
/////////////////////////////////////////////////////////////////////////////
// CCaiView
IMPLEMENT_DYNCREATE(CCaiView, CView)
BEGIN_MESSAGE_MAP(CCaiView, CView)
//{{AFX_MSG_MAP(CCaiView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_COMMAND(ID_SEED, OnSeed)
ON_COMMAND(ID_POINTMOVE, OnPointmove)
ON_COMMAND(ID_RECTANGLE, OnRectangle)
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCaiView construction/destruction
CCaiView::CCaiView()
{
// TODO: add construction code here
}
CCaiView::~CCaiView()
{
}
BOOL CCaiView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CCaiView drawing
void CCaiView::OnDraw(CDC* pDC)
{
MSG msg;
//CString CurStr_x,CurStr_y;
VERIFY(::GetMessage(&msg, NULL, 0, 0));
switch(msg.message)
{
case WM_LBUTTONUP:
case WM_MOUSEMOVE:
case WM_PAINT:
// hdc = BeginPaint(hwnd, &ps);
// GetClientRect(hwnd, &rc);
// SetMapMode(hdc, MM_ANISOTROPIC);
// SetWindowExtEx(hdc, 100, 100, NULL);
// SetViewportExtEx(hdc, rc.right, rc.bottom, NULL);
// Polyline(hdc, aptStar, 6);
// EndPaint(hwnd, &ps);
return;
}
// {
// CPoint point;
//
// point.x = LOWORD(msg.lParam); // horizontal position of cursor
// point.y = HIWORD(msg.lParam);
// pDC->SetBkMode(TRANSPARENT);//文字透明
// pDC->SetTextColor(RGB(0 ,150, point.x*point.y));
// CurStr_x.Format("%f",(float)point.x);
// CurStr_y.Format("%f",(float)point.y);
// pDC->TextOut(point.x,point.y,"("+CurStr_x.Left(5)+","+CurStr_y.Left(5)+")");
//GetCursorPos(&point2);
//pDC->LineTo(point2);
// }
//}
// CCaiDoc* pDoc = GetDocument();
// ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CCaiView printing
BOOL CCaiView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CCaiView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CCaiView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CCaiView diagnostics
#ifdef _DEBUG
void CCaiView::AssertValid() const
{
CView::AssertValid();
}
void CCaiView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CCaiDoc* CCaiView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCaiDoc)));
return (CCaiDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CCaiView message handlers
void CCaiView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CDC* pDC=GetDC();
CString CurStr_x,CurStr_y;
count++;
if(count%2==1){
point1=point;
}
else {
point2=point;
pDC->MoveTo(point1);
pDC->LineTo(point);//画线
//pDC->Rectangle(point1.x,point1.y,point2.x,point2.y);
}
pDC->Rectangle(point.x-2,point.y-2,point.x+2,point.y+2);
pDC->SetBkMode(TRANSPARENT);//文字透明
pDC->SetTextColor(RGB(0 ,150, point.x*point.y));
CurStr_x.Format("%f",(float)point.x);
CurStr_y.Format("%f",(float)point.y);
pDC->TextOut(point.x,point.y,"("+CurStr_x.Left(5)+","+CurStr_y.Left(5)+")");
CView::OnLButtonDown(nFlags, point);
}
void CCaiView::OnPointmove()
{
// TODO: Add your command handler code here
if(i!=1)i=1;
else i=0;
}
void CCaiView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//point12=point1;
//CDC* pDC=GetDC();
//line(pDC,RGB(0,0,255));
//改变原来点坐标,删去原来的点并画新点
CDC* pDC=GetDC();
pDC->Rectangle(point.x-2,point.y-2,point.x+2,point.y+2);
CView::OnLButtonUp(nFlags, point);
}
void CCaiView::OnRectangle()
{
CDC* pDC=GetDC();
CPen myPen;
if( myPen.CreatePen( PS_SOLID, 1, RGB(0,0,0) ) )
{
// Then initialize it
// Select it into the device context
// Save the old pen at the same time
CPen* pOldPen = pDC->SelectObject( &myPen );
// Use the pen // TODO: Add your command handler code here
pDC->Rectangle(point1.x,point1.y,point2.x,point2.y);
int lx=point2.x-point1.x;
int ly=point2.y-point1.y;
if(lx>0)
{
for(int i=0;i<lx+1;i++)
if(ly>0)
for(int j=0;j<ly+1;j++)
pDC->SetPixel(point1.x+i,point1.y+j,RGB(0,i*255/fabs(lx),j*255/fabs(lx)));
else
for(int j=ly-1;j<0;j++)
pDC->SetPixel(point1.x+i,point1.y+j,RGB(0,0,i*255/fabs(lx)));
}
else
{
for(int i=lx-1;i<0;i++)
if(ly>0)
for(int j=0;j<ly+1;j++)
pDC->SetPixel(point1.x+i,point1.y+j,RGB(0,0,i*255/fabs(lx)));
else
for(int j=ly-1;j<0;j++)
pDC->SetPixel(point1.x+i,point1.y+j,RGB(0,0,i*255/fabs(lx)));
}
// Restore the old pen to the device context
pDC->SelectObject( pOldPen );
}
else
{
// Alert the user that resources are low
}
//CCaiView::OnDraw(&pDC);
}
void CCaiView::OnSeed()
{
// TODO: Add your command handler code here
CDC* pDC=GetDC();
// int a[20000],b[20000];
int k=0;//,t;
//POINT point_curpos;//,point_m;
CPen myPen;
//int a[20000],b[20000];
COLORREF nv=RGB(255,0,0);
COLORREF bv=RGB(0,0,255);
//GetGraphicsMode(hdc);
//CPoint point_start,point_end;
//point_start.x=point1.x;
//point_start.y=(point1.y+point2.y)/2;
//point_end.x=point2.x;
//point_end.y=(point1.y+point2.y)/2;
//c=pDC->GetPixel(point1);
//CString CurStr_x,CurStr_y;
//RECT rect;
//rect.top=point1.y;
//rect.bottom=point2.y;
//rect.left=point1.x;
//rect.right=point2.x;
// CurStr_x.Format("%f",(float)point1.x);
// CurStr_y.Format("%f",(float)point1.y);
// CurStr_x.Insert(0,CurStr_y);
if( myPen.CreatePen(PS_SOLID, 1, bv ) )
{
// Use the pen
CPen* pOldPen = pDC->SelectObject( &myPen );
// pDC->SelectObject( pOldPen );
pDC->Rectangle(200,200,400,400);
pDC->SetPixel(pDC->GetCurrentPosition(),nv);
//pDC->Arc(&rect,point_start,point_end);
//pDC->DrawText(
// CurStr_x, // pointer to string to draw
// &rect, // pointer to struct with formatting dimensions
// DT_BOTTOM // text-drawing flags
// );
//pDC->Rectangle(point1.x,point1.y,point2.x,point2.y);
}//end if(mypen..)
else
{
// Alert the user that resources are low
}
}
void CCaiView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
UpdateWindow();
CString CurStr_x,CurStr_y;
CDC* pDC=GetDC();
//pDC->ResetDC();
pDC->SetBkMode(TRANSPARENT);//文字透明
pDC->SetTextColor(RGB(0 ,150, point.x*point.y));
CurStr_x.Format("%f",(float)point.x);
CurStr_y.Format("%f",(float)point.y);
// pDC->TextOut(point.x,point.y,"("+CurStr_x.Left(5)+","+CurStr_y.Left(5)+")");
CView::OnMouseMove(nFlags, point);
}