日期:2014-05-19  浏览次数:20841 次

许多控件enable设置为false后就变成灰色,怎么能让他只读且不为灰色?
有些控件没有Readonly属性
(比如ComboText,CheckBox,DatetimePicker,TreeView)
想让它只读只好把enable设置为false
但是这样子就显示成灰色了   很难看
请问怎么样来正常显示?
或者有什么别的方法来设置只读?

------解决方案--------------------
画个控件上去,把原控件隐藏掉。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace TestDLL
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport( "gdi32.dll ")]
public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
ArrayList arry=new ArrayList();
public Form1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(Form1_Paint);
}

void Form1_Paint(object sender, PaintEventArgs e)
{
for (int i = 0; i < arry.Count; i++)
{
e.Graphics.DrawImage(((Bitmap)((object[])arry[i])[0]), ((Point)((object[])arry[i])[1]));

}
}
private void DrawImage(Control c)
{
Size s = c.Size;
Graphics mygraphics=c.CreateGraphics();
IntPtr dc1;
IntPtr dc2;
Bitmap memoryImage = new Bitmap(s.Width, s.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
dc1 = mygraphics.GetHdc();
dc2 = memoryGraphics.GetHdc();
BitBlt(dc2, 0, 0, c.ClientRectangle.Width, c.ClientRectangle.Height, dc1, 0, 0, 13369376);
mygraphics.ReleaseHdc(dc1);
memoryGraphics.ReleaseHdc(dc2);
Point p = new Point(c.Location.X, c.Location.Y);
Panel pa = new Panel();
object[] obj ={ memoryImage, p};
arry.Add(obj);
c.Visible = false;
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
DrawImage(this.treeView1);
DrawImage(this.comboBox1);
}
}
}