你是要做一个按键随机改变颜色的Hello World? 创建个Windows应用程序,在窗体上放一个Label,再响应窗体的KeyPress事件,加入如下代码: private Random r = new Random();private void Form1_KeyPress(object sender, KeyPressEventArgs e) { label1.ForeColor = Color.FromArgb(r.Next(0x1000000)); }
using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Windows.Forms; using System.Runtime.InteropServices; namespace _90998 { public partial class Form1 : Form { private Color ctemp; public Color cTemp{get{return ctemp;} set{ctemp = value;}} public Form1() { InitializeComponent(); CenterToScreen(); Paint+= new PaintEventHandler(OnPaint); AutoSizeChanged += new EventHandler(OnAutoSizeChanged); KeyDown += new KeyEventHandler(OnKeyDown); } public void OnPaint(object sender, PaintEventArgs e) { Color c = new Color(); c = RandomColor();
ChangeTextColor(c,e); } public void OnKeyDown(object sender, KeyEventArgs e) { Redraw(); } public void ChangeTextColor(Color c, PaintEventArgs e) { GraphicsPath p = new GraphicsPath();
RECT rt = new RECT(); //rt.top = ClientSize.Height; //rt.left = ClientSize.Width;
p.AddString("Hello, You Are Welcome!", FontFamily.GenericSansSerif, (int)FontStyle.Italic, (float)72, new Point(0,0), StringFormat.GenericDefault); if(!GetClientRect(base.Handle, ref rt)) throw new Exception("调用失败"); bool bDt = DrawText(GetWindowDC(base.Handle), "Hello, You Are Welcome!", -1, ref rt, 0x00000020 | 0x00000001 | 0x00000004);//DT_SINGLELINE | DT_CENTER | DT_VCENTER); if (!bDt) throw new Exception("调用失败"); SolidBrush brush = new SolidBrush(c); cTemp = c; e.Graphics.FillPath(brush, p); brush.Dispose(); } public void OnAutoSizeChanged(object sender, EventArgs e) { Redraw(cTemp); } public void Redraw() { Color c = new Color(); Graphics g = Graphics.FromHwnd(base.Handle); Rectangle rc = new Rectangle(); PaintEventArgs ep = new PaintEventArgs(g, rc); c = RandomColor();
Color c1 = Color.FromArgb(255,255,255); g.Clear(c1); ChangeTextColor(c, ep); BackColor = RandomColor(50);
} public void Redraw(Color c) { Graphics g = Graphics.FromHwnd(base.Handle); Rectangle rc = new Rectangle(); PaintEventArgs ep = new PaintEventArgs(g, rc); ChangeTextColor(c, ep); } public Color RandomColor() { Random rd =new Random(); int r = rd.Next(255); int g = rd.Next(255); int b = rd.Next(255);
Color c = new Color(); c=Color.FromArgb(r,g,b); //资料来源http://study.qqcf.com/web/715/214956.htm return c; } public Color RandomColor(int ir) { Random rd = new Random(); int r = rd.Next(ir); int g = rd.Next(ir); int b = rd.Next(ir); Color c = new Color(); c = Color.FromArgb(r, g, b); //资料来源http://study.qqcf.com/web/715/214956.htm return c; } [DllImport("User32.dll", EntryPoint = "DrawText")] private static extern bool DrawText(IntPtr hDC,string lpString,int nCount,ref RECT rt,uint uFormat); [DllImport("User32.dll", EntryPoint = "GetWindowDC")] private static extern IntPtr GetWindowDC(IntPtr hWnd); [DllImport("User32.dll", EntryPoint = "GetClientRect")] private static extern bool GetClientRect(IntPtr hWnd, ref RECT rt); } [StructLayout(LayoutKind.Sequential)] public struct RECT { public long left; public long top; public long righ; public long bottom; }} //不是很完善,不足之处请各位老师多多批评指导。谢谢。
楼猪编这个有什么用呀 form1 new 一个算了
顺便请教一下p.AddString("Hello, You Are Welcome!", FontFamily.GenericSansSerif, (int)FontStyle.Italic, (float)72, new Point(0,0), StringFormat.GenericDefault); 如何使该字符串居中显示,还没找到方法还有 public void OnAutoSizeChanged(object sender, EventArgs e) { Redraw(cTemp); } 在这个触发事件中快速改变窗体尺寸时,字体会花,如何保证字体不花呢?
用C# 用winform很简单就实现了,拖个窗体,画写字符,改变颜色就行了
创建个Windows应用程序,在窗体上放一个Label,再响应窗体的KeyPress事件,加入如下代码:
private Random r = new Random();private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
label1.ForeColor = Color.FromArgb(r.Next(0x1000000));
}
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace _90998
{
public partial class Form1 : Form
{ private Color ctemp;
public Color cTemp{get{return ctemp;} set{ctemp = value;}} public Form1()
{
InitializeComponent();
CenterToScreen(); Paint+= new PaintEventHandler(OnPaint);
AutoSizeChanged += new EventHandler(OnAutoSizeChanged);
KeyDown += new KeyEventHandler(OnKeyDown);
}
public void OnPaint(object sender, PaintEventArgs e)
{
Color c = new Color(); c = RandomColor();
ChangeTextColor(c,e); }
public void OnKeyDown(object sender, KeyEventArgs e)
{
Redraw();
} public void ChangeTextColor(Color c, PaintEventArgs e)
{
GraphicsPath p = new GraphicsPath();
RECT rt = new RECT();
//rt.top = ClientSize.Height;
//rt.left = ClientSize.Width;
p.AddString("Hello, You Are Welcome!", FontFamily.GenericSansSerif, (int)FontStyle.Italic, (float)72, new Point(0,0), StringFormat.GenericDefault);
if(!GetClientRect(base.Handle, ref rt))
throw new Exception("调用失败"); bool bDt = DrawText(GetWindowDC(base.Handle), "Hello, You Are Welcome!", -1, ref rt, 0x00000020 | 0x00000001 | 0x00000004);//DT_SINGLELINE | DT_CENTER | DT_VCENTER);
if (!bDt)
throw new Exception("调用失败");
SolidBrush brush = new SolidBrush(c);
cTemp = c;
e.Graphics.FillPath(brush, p);
brush.Dispose(); }
public void OnAutoSizeChanged(object sender, EventArgs e)
{
Redraw(cTemp);
} public void Redraw()
{
Color c = new Color();
Graphics g = Graphics.FromHwnd(base.Handle);
Rectangle rc = new Rectangle();
PaintEventArgs ep = new PaintEventArgs(g, rc);
c = RandomColor();
Color c1 = Color.FromArgb(255,255,255);
g.Clear(c1);
ChangeTextColor(c, ep); BackColor = RandomColor(50);
} public void Redraw(Color c)
{ Graphics g = Graphics.FromHwnd(base.Handle);
Rectangle rc = new Rectangle();
PaintEventArgs ep = new PaintEventArgs(g, rc);
ChangeTextColor(c, ep); }
public Color RandomColor()
{
Random rd =new Random();
int r = rd.Next(255);
int g = rd.Next(255);
int b = rd.Next(255);
Color c = new Color();
c=Color.FromArgb(r,g,b);
//资料来源http://study.qqcf.com/web/715/214956.htm
return c;
} public Color RandomColor(int ir)
{
Random rd = new Random();
int r = rd.Next(ir);
int g = rd.Next(ir);
int b = rd.Next(ir); Color c = new Color();
c = Color.FromArgb(r, g, b);
//资料来源http://study.qqcf.com/web/715/214956.htm
return c;
}
[DllImport("User32.dll", EntryPoint = "DrawText")]
private static extern bool DrawText(IntPtr hDC,string lpString,int nCount,ref RECT rt,uint uFormat);
[DllImport("User32.dll", EntryPoint = "GetWindowDC")]
private static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("User32.dll", EntryPoint = "GetClientRect")]
private static extern bool GetClientRect(IntPtr hWnd, ref RECT rt);
} [StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public long left;
public long top;
public long righ;
public long bottom;
}}
//不是很完善,不足之处请各位老师多多批评指导。谢谢。
form1 new 一个算了
{
Redraw(cTemp);
} 在这个触发事件中快速改变窗体尺寸时,字体会花,如何保证字体不花呢?