http://files.cnblogs.com/xe2011/officetable.rar
画表格防OFFICE的功能
画表格的功能
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
namespace System.Windows.Forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int x = 0;
public int y = 0;
private void panel1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < 8; i++)//i是横着的
{
for (int j = 0; j < 7; j++) //j是竖着的
{
Rectangle boxRect = new Rectangle(1 + 20 * i, 1 + j * 20, 18, 18);
e.Graphics.FillRectangle(new SolidBrush(Color.White), boxRect);
e.Graphics.DrawRectangle(new Pen(Color.FromArgb(131, 125, 125), 1), boxRect);
}
}
}
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Rectangle rect1 = new Rectangle(0, 0, e.Location.X, e.Location.Y);
Text = rect1.ToString();
Graphics g = panel1.CreateGraphics();
x = (rect1.Width - 2) / 20 + 1;
y = (rect1.Height - 2) / 20 + 1;
if (e.Location.X == 0 || e.Location.Y == 0 || e.Location.X < 2 || e.Location.Y < 2)
{
x = 0;
y = 0;
}
if (x > 8)
x = 8;
if (y > 7)
y = 7;
labelMsg.Text = string.Format("{0} x {1}", x, y);
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 7; j++)
{
Rectangle rect2 = new Rectangle(1 + 1 + 20 * i, 1 + 1 + j * 20, 17, 17);
Brush brush1 = new SolidBrush(Color.White);
if (rect1.IntersectsWith(rect2))
brush1 = new SolidBrush(SystemColors.Highlight);
g.FillRectangle(brush1, rect2);
}
}
g.Dispose();
}
private void panel1_Click(object sender, EventArgs e)
{
string s = string.Format("{0} x {1}", x, y);
MessageBox.Show(s);
((ToolStripDropDown)Parent).Close();
}
}
}
使用
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
namespace System.Windows.Forms
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
form1.TopLevel = false;
host = new ToolStripControlHost(form1);
host.AutoSize = false;
host.Width = form1.Width;
}
Form1 form1 = new Form1();
ToolStripDropDown dropdown = new ToolStripDropDown();
ToolStripControlHost host ;//= new ToolStripControlHost(form1);
private void button1_MouseEnter(object sender, EventArgs e)
{
dropdown.Margin = Padding.Empty;
dropdown.Padding = Padding.Empty;
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
dropdown.Items.Add(host);
// 让这个窗体出面在按扭的相对下面
Point location = PointToScreen(button1.Location);
dropdown.Show(location.X, location.Y + button1.Height);
}
}
}