zoukankan      html  css  js  c++  java
  • textbox在c#中怎么添加背景图片

         最近考虑做个动感日记,其中一个需求就是像163邮箱里面,可以设置背景信纸。因此考虑写一个设定背景图片的控件,

    网上找到一个方法。可以借鉴一下,所以贴出来了。

    http://topic.csdn.net/u/20080102/11/577b2d8d-4b75-473e-b533-55d4e8881d9c.html

    ===========================从textbox类继承处理绘制背景消息====================
       
    class MyTextBox : TextBox
        {
           
    const int WM_ERASEBKGND = 0x0014;

           
    private Image backImage;

            [DisplayName(
    "背景图片。")]
           
    public Image BackImage
            {
               
    get { return backImage; }
               
    set { backImage = value; }
            }

           
    protected void OnEraseBkgnd(Graphics gs)
            {
                gs.FillRectangle(Brushes.White,
    0, 0, this.Width, this.Height); //填充为白色,防止图片太小出现重影
                if (backImage != null) gs.DrawImage(backImage, 0, 0); //绘制背景。
                gs.Dispose();
            }

           
    protected override void WndProc(ref Message m)
            {
               
    if (m.Msg == WM_ERASEBKGND) //绘制背景
                {
                    OnEraseBkgnd(Graphics.FromHdc(m.WParam));
                    m.Result
    = (IntPtr)1;
                }
               
    base.WndProc(ref m);
            }
        }

    ==================窗口类里设置控件的字体颜色等=============================

           
    const int WM_CTLCOLOREDIT = 0x0133;
           
    const int TRANSPARENT = 0x1;
           
    const int NULL_BRUSH = 0x5;

            [DllImport(
    "gdi32")]
           
    static extern int SetBkMode(IntPtr hdc, int bkMode);
            [DllImport(
    "gdi32")]
           
    static extern int SetTextColor(IntPtr hdc, int color);
            [DllImport(
    "gdi32")]
           
    static extern IntPtr GetStockObject(int fnobject);

           
    protected override void WndProc(ref Message m)
            {
               
    if (m.Msg == WM_CTLCOLOREDIT && m.LParam == myTextBox1.Handle)//类型为EDIT(TextBox)
                {
                    SetBkMode(m.WParam, TRANSPARENT);
    //设置背景透明
                    SetTextColor(m.WParam,0xFF);     //字体颜色为红色
                    m.Result = GetStockObject(NULL_BRUSH);
                   
    return;
                }
               
    else base.WndProc(ref m);
            }

  • 相关阅读:
    【转】[fix] Wireshark error: There are no interfaces on which a capture can be done. on Mac OS X
    【转载】Linux 文件系统的目录结构
    postgreSQL使用
    [转载] Linux启动过程详解-《别怕Linux编程》之八
    冒泡排序
    java值类型和引用类型
    冒泡排序法与二分查找法
    关系型数据库
    SQList的建表并添加数据练习
    数据存储——SQLite数据库存储
  • 原文地址:https://www.cnblogs.com/jackhuclan/p/1285254.html
Copyright © 2011-2022 走看看