在多文挡中的视图中声明一个
CRulerManager m_objRuler
对象。
在OnDraw()函数
加入m_objRuler.DrawRuler( pDC->m_hDC );
即可。头文件定义://**************************************************************************************
//FileName : RulerManager.h
//Copyright : TopLevel Studio 2002 - 2006 All Rights Reserved
//Revision : 1.0
//Date : 2002/09
//Author : TopLevel
//Histroy : 2002/09 TopLevel create
//**************************************************************************************
#if !defined(AFX_RULERMANAGER_H__26FA673D_0EA3_4A89_B7E2_2577B3E58B23__INCLUDED_)
#define AFX_RULERMANAGER_H__26FA673D_0EA3_4A89_B7E2_2577B3E58B23__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#ifndef _RULERMANAGER_H
#define _RULERMANAGER_Hclass CRulerManager
{
public:
void DrawRuler(HDC hDC);
CRulerManager();
virtual ~CRulerManager();private:
CString IntToStr(int i); CFont m_Font;
// CBrush m_Brush;
};#endif#endif // !defined(AFX_RULERMANAGER_H__26FA673D_0EA3_4A89_B7E2_2577B3E58B23__INCLUDED_)cpp文件定义:
//**************************************************************************************
//FileName : RulerManager.cpp
//Copyright : TopLevel Studio 2002 - 2006 All Rights Reserved
//Revision : 1.0
//Date : 2002/09
//Author : TopLevel
//Histroy : 2002/09 TopLevel create
//**************************************************************************************
#include "stdafx.h"
#include "RulerManager.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CRulerManager::CRulerManager()
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // Zero out the structure.
lf.lfHeight = 48; // Request a 12-pixel-height font.
lstrcpy(lf.lfFaceName, _T("Arial")); // Request a face name "Arial".
VERIFY(m_Font.CreateFontIndirect(&lf)); // Create the font.
}CRulerManager::~CRulerManager()
{
::DeleteObject(&m_Font);
}//***********************************************************************
//Name : DrawRuler
//Author : TopLevel
//Modify : 2002-09-28
//Return : N/A
//Description : 在指定的区域设置标尺。
//***********************************************************************
void CRulerManager::DrawRuler(HDC hDC)
{
int oldDC = ::SaveDC( hDC );
int oldMap = ::SetMapMode( hDC, MM_LOMETRIC ); CDC *pDC = CDC::FromHandle(hDC);
CBrush NewBrush;
CBrush *pOldBrush = NULL;
RECT rc = {0};
RECT rc1 = {0}; pDC->SetBkMode(TRANSPARENT);
if(!NewBrush.CreateSolidBrush(RGB(220, 220, 220)))
{
pDC->DeleteTempMap();
return;
}
pOldBrush = pDC->SelectObject(&NewBrush);
//画横向面板
rc.left = 100;
rc.top = 0;
rc.right = 10000;
rc.bottom = -100;
pDC->Rectangle( &rc );
rc1.left = 0;
rc1.top = 0;
rc1.right = 999;
rc1.bottom = -98;
pDC->FillRect(&rc1, &NewBrush);
//画纵向面板
rc.left = 0;
rc.top = -100;
rc.bottom = -10000;
rc.right = 100;
pDC->Rectangle( &rc );
rc1.left = 0;
rc1.top = 0;
rc1.right = 97;
rc1.bottom = -9999;
pDC->FillRect(&rc1, &NewBrush);
pDC->SelectObject(pOldBrush);
::DeleteObject(&NewBrush);
CFont* pOldFont = pDC->SelectObject( &m_Font );
//画横尺(X轴)
for(int i = 0; i < 10000; i++)
{
if((i%100) == 0 )
{
pDC->MoveTo(i+100, 0);
pDC->LineTo(i+100, -48);
pDC->TextOut( i+100-10, -50, IntToStr(i/100));
} if((i%50) == 0)
{
pDC->MoveTo(i+100, 0);
pDC->LineTo(i+100, -32);
} if ( (i%10) == 0 )
{
pDC->MoveTo(i+100, 0);
pDC->LineTo(i+100, -16);
}
}
//画纵尺(Y轴)
for( i = 0; i < 10000; i++)
{
if( (i%100) == 0 )
{
pDC->MoveTo( 0, -i-100 );
pDC->LineTo( 48, -i-100 );
pDC->TextOut( 50, -i-100+24, IntToStr(i/100));
}
if( (i%50) == 0 )
{
pDC->MoveTo( 0, -i-100 );
pDC->LineTo( 32, -i-100 );
} if( (i%10) == 0 )
{
pDC->MoveTo( 0, -i-100 );
pDC->LineTo( 16, -i-100 );
}
} pDC->SelectObject( pOldFont ); pDC->DeleteTempMap(); ::SetMapMode( hDC, oldMap );
::RestoreDC( hDC, oldDC );
}//***********************************************************************
//Name : IntToStr
//Author : TopLevel Create
//Modify : 2002-09-28
//Return : N/A
//Description : 一个整数转换成字符串
//***********************************************************************
CString CRulerManager::IntToStr(int i)
{
char chText[sizeof(int) + 1] = {0};
sprintf(chText, "%d", i);
CString str;
str.Empty();
str = chText; return str;
}
CRulerManager m_objRuler
对象。
在OnDraw()函数
加入m_objRuler.DrawRuler( pDC->m_hDC );
即可。头文件定义://**************************************************************************************
//FileName : RulerManager.h
//Copyright : TopLevel Studio 2002 - 2006 All Rights Reserved
//Revision : 1.0
//Date : 2002/09
//Author : TopLevel
//Histroy : 2002/09 TopLevel create
//**************************************************************************************
#if !defined(AFX_RULERMANAGER_H__26FA673D_0EA3_4A89_B7E2_2577B3E58B23__INCLUDED_)
#define AFX_RULERMANAGER_H__26FA673D_0EA3_4A89_B7E2_2577B3E58B23__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#ifndef _RULERMANAGER_H
#define _RULERMANAGER_Hclass CRulerManager
{
public:
void DrawRuler(HDC hDC);
CRulerManager();
virtual ~CRulerManager();private:
CString IntToStr(int i); CFont m_Font;
// CBrush m_Brush;
};#endif#endif // !defined(AFX_RULERMANAGER_H__26FA673D_0EA3_4A89_B7E2_2577B3E58B23__INCLUDED_)cpp文件定义:
//**************************************************************************************
//FileName : RulerManager.cpp
//Copyright : TopLevel Studio 2002 - 2006 All Rights Reserved
//Revision : 1.0
//Date : 2002/09
//Author : TopLevel
//Histroy : 2002/09 TopLevel create
//**************************************************************************************
#include "stdafx.h"
#include "RulerManager.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////CRulerManager::CRulerManager()
{
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT)); // Zero out the structure.
lf.lfHeight = 48; // Request a 12-pixel-height font.
lstrcpy(lf.lfFaceName, _T("Arial")); // Request a face name "Arial".
VERIFY(m_Font.CreateFontIndirect(&lf)); // Create the font.
}CRulerManager::~CRulerManager()
{
::DeleteObject(&m_Font);
}//***********************************************************************
//Name : DrawRuler
//Author : TopLevel
//Modify : 2002-09-28
//Return : N/A
//Description : 在指定的区域设置标尺。
//***********************************************************************
void CRulerManager::DrawRuler(HDC hDC)
{
int oldDC = ::SaveDC( hDC );
int oldMap = ::SetMapMode( hDC, MM_LOMETRIC ); CDC *pDC = CDC::FromHandle(hDC);
CBrush NewBrush;
CBrush *pOldBrush = NULL;
RECT rc = {0};
RECT rc1 = {0}; pDC->SetBkMode(TRANSPARENT);
if(!NewBrush.CreateSolidBrush(RGB(220, 220, 220)))
{
pDC->DeleteTempMap();
return;
}
pOldBrush = pDC->SelectObject(&NewBrush);
//画横向面板
rc.left = 100;
rc.top = 0;
rc.right = 10000;
rc.bottom = -100;
pDC->Rectangle( &rc );
rc1.left = 0;
rc1.top = 0;
rc1.right = 999;
rc1.bottom = -98;
pDC->FillRect(&rc1, &NewBrush);
//画纵向面板
rc.left = 0;
rc.top = -100;
rc.bottom = -10000;
rc.right = 100;
pDC->Rectangle( &rc );
rc1.left = 0;
rc1.top = 0;
rc1.right = 97;
rc1.bottom = -9999;
pDC->FillRect(&rc1, &NewBrush);
pDC->SelectObject(pOldBrush);
::DeleteObject(&NewBrush);
CFont* pOldFont = pDC->SelectObject( &m_Font );
//画横尺(X轴)
for(int i = 0; i < 10000; i++)
{
if((i%100) == 0 )
{
pDC->MoveTo(i+100, 0);
pDC->LineTo(i+100, -48);
pDC->TextOut( i+100-10, -50, IntToStr(i/100));
} if((i%50) == 0)
{
pDC->MoveTo(i+100, 0);
pDC->LineTo(i+100, -32);
} if ( (i%10) == 0 )
{
pDC->MoveTo(i+100, 0);
pDC->LineTo(i+100, -16);
}
}
//画纵尺(Y轴)
for( i = 0; i < 10000; i++)
{
if( (i%100) == 0 )
{
pDC->MoveTo( 0, -i-100 );
pDC->LineTo( 48, -i-100 );
pDC->TextOut( 50, -i-100+24, IntToStr(i/100));
}
if( (i%50) == 0 )
{
pDC->MoveTo( 0, -i-100 );
pDC->LineTo( 32, -i-100 );
} if( (i%10) == 0 )
{
pDC->MoveTo( 0, -i-100 );
pDC->LineTo( 16, -i-100 );
}
} pDC->SelectObject( pOldFont ); pDC->DeleteTempMap(); ::SetMapMode( hDC, oldMap );
::RestoreDC( hDC, oldDC );
}//***********************************************************************
//Name : IntToStr
//Author : TopLevel Create
//Modify : 2002-09-28
//Return : N/A
//Description : 一个整数转换成字符串
//***********************************************************************
CString CRulerManager::IntToStr(int i)
{
char chText[sizeof(int) + 1] = {0};
sprintf(chText, "%d", i);
CString str;
str.Empty();
str = chText; return str;
}
在FORM1里加个PANEL:
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Graphics g=System.Drawing.Graphics.FromHwnd(this.panel1.Handle);
DrawRuler(g);
}//下面这个就是我改的函数
public void DrawRuler(System.Drawing.Graphics G)
{ System.Drawing.Rectangle rc=new Rectangle(100,0,10000,100);
System.Drawing.Rectangle rc1=new Rectangle(0,0,999,98);
System.Drawing.Rectangle rc20=new Rectangle(0,0,100,10000);
System.Drawing.Rectangle rc21=new Rectangle(0,0,97,9999); //画横向面板
G.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Blue),rc);
G.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Transparent),rc1);
//画纵向面板 G.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Blue),rc20);
G.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Transparent),rc21); //画横尺(X轴)
for(int i = 0; i < 10000; i++)
{
if((i%100) == 0 )
{
G.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Blue),i+100, 0,i+100, 48);
G.DrawString((i/100).ToString(),new System.Drawing.Font(new System.Drawing.FontFamily(System.Drawing.Text.GenericFontFamilies.Serif),12),new System.Drawing.SolidBrush(System.Drawing.Color.Green),i+100-10, 50); } if((i%50) == 0)
{
G.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Blue),i+100, 0,i+100, 32);
} if ( (i%10) == 0 )
{ G.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Blue),i+100, 0,i+100, 16);
}
}
//画纵尺(Y轴)
for(int i = 0; i < 10000; i++)
{
if( (i%100) == 0 )
{
// pDC->MoveTo( 0, -i-100 );
// pDC->LineTo( 48, -i-100 );
// pDC->TextOut( 50, -i-100+24, IntToStr(i/100));
G.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Blue), 0, i,48, i);
G.DrawString((i/100).ToString(),new System.Drawing.Font(new System.Drawing.FontFamily(System.Drawing.Text.GenericFontFamilies.Serif),12),new System.Drawing.SolidBrush(System.Drawing.Color.Green), 50, i+100-24);
}
if( (i%50) == 0 )
{
// pDC->MoveTo( 0, -i-100 );
// pDC->LineTo( 32, -i-100 );
G.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Blue),0, i,32, i);
} if( (i%10) == 0 )
{
// pDC->MoveTo( 0, -i-100 );
// pDC->LineTo( 16, -i-100 );
G.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Blue),0, i ,16, i);
}
} //pDC->SelectObject( pOldFont ); //pDC->DeleteTempMap(); //::SetMapMode( hDC, oldMap );
//::RestoreDC( hDC, oldDC );
}
调用dll文件可以通过引用方式,或者通过
[DllImport("KERNEL32.DLL", EntryPoint="MoveFileW", SetLastError=true,
CharSet=CharSet.Unicode, ExactSpelling=true,
CallingConvention=CallingConvention.StdCall)]
public static extern bool MoveFile(String src, String dst);