当前位置:编程学习 > C#/ASP.NET >>

如何让文字不断显示在图片上

如何让文字不断显示在图片上?

在Form1中添加了一个picturebox,在里面添加了一张图片,在程序运行过程中,会不断显示很多文本,如何让这些文本都显示在图片上方?而且还需要图片上方的文字加一个上下方向的滚动条,以查看显示的文字。

我试过在picturebox上方加一个textbox,可是textbox不知道如何把它的背景色变为透明色,

请大家帮帮忙,在线等,马上结贴! --------------------编程问答-------------------- marque --------------------编程问答--------------------
 private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
             e.Graphics.DrawString("jinjazz", this.Font, Brushes.Red, new PointF(10, 10));
        }
--------------------编程问答-------------------- 这段代码是:有一个菜单file下子菜单为Open 和Exit;一个OpenFileDialog1;
打开一个txt文档,显示在界面上;可以达到你的要求,显示在背景图片之上,出现滚动条。

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

namespace CapsEditor
{
    public partial class Form1 : Form
    {
        #region Constant fields
        private const string standardTitle = "CapsEditor";
        private const uint margine = 10;
        #endregion

        #region Member fields
        private ArrayList documentLines = new ArrayList();
        private uint lineHeight;
        private Size documentSize;

        private uint nLines;
        private Font mainFont;
        private Font emptyDocumentFont;
        private Brush mainBrush = Brushes.Blue;
        private Brush emptyDocumentBrush = Brushes.Red;
        private Point mouseDoubleClickPosition;
        private OpenFileDialog fileOpenDialog = new OpenFileDialog();
        private bool documentHasData = false;
        #endregion


        public Form1()
        {
            InitializeComponent();

            CreateFonts();

            fileOpenDialog.FileOk += new CancelEventHandler(this.OpenFileDialog_FileOk);
            fileOpenDialog.Filter = "Text(*.txt)|*.txt|C#(*.cs)|*.cs";
        }

        class TextLineInformation
        {
            public string Text;
            public uint Width;
        }
        
        private void CreateFonts()
        {
            mainFont = new Font("Arial",10);
            lineHeight = (uint)mainFont.Height;
            emptyDocumentFont = new Font("Verdana",13, FontStyle.Bold);
        }

        protected void OpenFileDialog_FileOk(object Sender, CancelEventArgs e)
        {
            this.LoadFile(fileOpenDialog.FileName);
        }

        private void menuFileOpen_Click(object sender, EventArgs e)
        {
            fileOpenDialog.ShowDialog();
        }

        private void menuExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void LoadFile(string FileName)
        {
            StreamReader sr = new StreamReader(FileName);
            string nextLine;
            documentLines.Clear();
            nLines = 0;

            TextLineInformation nextLineInfo;

            while ((nextLine = sr.ReadLine()) != null)
            {
                nextLineInfo = new TextLineInformation();
                nextLineInfo.Text = nextLine;
                documentLines.Add(nextLineInfo);
                ++nLines;
            }
            sr.Close();
            documentHasData = (nLines > 0) ? true : false;

            CalculateLineWidth();
            CalculateDocumentSize();

            this.Text = Form1.standardTitle + " " + FileName;
            this.Invalidate();

        }

        private void CalculateLineWidth()
        {
            Graphics dc = this.CreateGraphics();
            foreach (TextLineInformation nextLine in documentLines)
            {
                nextLine.Width = (uint)dc.MeasureString(nextLine.Text, mainFont).Width;

            }
        }

        #region CalculateDocumentSize函数
        private void CalculateDocumentSize()
        {
            if (!documentHasData)
            {
                documentSize = new Size(100, 200);
            }
            else
            {
                documentSize.Height = (int)(nLines * lineHeight) + 2 * (int)margine;
                uint maxLineLength = 0;
                foreach (TextLineInformation nextWord in documentLines)
                {
                    uint tempLineLength = nextWord.Width + 2 * margine;
                    if (tempLineLength > maxLineLength)
                    {
                        maxLineLength = tempLineLength;
                    }
                    documentSize.Width = (int)maxLineLength;
                }
                this.AutoScrollMinSize = documentSize;
            }
        }
        #endregion

         --------------------编程问答-------------------- 接上.....

protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics dc = e.Graphics;
            int scrollPosX = this.AutoScrollPosition.X;
            int scrollPosY = this.AutoScrollPosition.Y;
            dc.TranslateTransform(scrollPosX, scrollPosY);

            if (!documentHasData)
            {
                dc.DrawString("<Empty document>",emptyDocumentFont,emptyDocumentBrush,new Point(20,20));
                base.OnPaint(e);
                return;
            }

            //work out which lines are in clipping rectangle
            int minLineInClipRegion = WorldYCoordinateToLineIndex(e.ClipRectangle.Top-scrollPosY);
            if (minLineInClipRegion == -1)
            {
                minLineInClipRegion = 0;
            }

            int maxLineInClipRegion = WorldYCoordinateToLineIndex(e.ClipRectangle.Bottom-scrollPosY);
            if(maxLineInClipRegion>=this.documentLines.Count||maxLineInClipRegion==-1)
            {
                maxLineInClipRegion = this.documentLines.Count - 1;
            }

            TextLineInformation nextLine;
            for (int i = minLineInClipRegion; i <= maxLineInClipRegion; i++)
            {
                nextLine = (TextLineInformation)documentLines[i];
                dc.DrawString(nextLine.Text, mainFont, mainBrush, this.LineIndexToWorldCoordinate(i));
            }
        }       

        private int WorldYCoordinateToLineIndex(int y)
        {
            if (y < margine) return -1;
            return (int)((y-margine)/lineHeight);
        }

        private Point LineIndexToWorldCoordinate(int index)
        {
            Point TopLeftCorner = new Point((int)margine, (int)(lineHeight * index + margine));
            return TopLeftCorner;            

        }

        private int WorldCoordinateToLineIndex(Point pos)
        {
            if (!documentHasData)
            {
                return -1;
            }
            if (pos.Y < margine || pos.X < margine)
            {
                return -1;
            }

            int index = (int)(pos.Y-margine)/(int)this.lineHeight;
            
            //check position isn't below document
            if (index >= documentLines.Count)
            {
                return -1;
            }

            //now check that horizontal position is within this line
            TextLineInformation theLine = (TextLineInformation)documentLines[index];

            if (pos.X > margine + theLine.Width) return -1;
             
            //all is Ok.We can return answer
            return index;

        }

        private Point LineIndexToPageCoordinate(int index)
        {
            return LineIndexToWorldCoordinate(index) + new Size(AutoScrollPosition);
        }

        private int PageCoordinateToLineIndex(Point pos)
        {
            //God bless you!
            return WorldCoordinateToLineIndex(pos - new Size(AutoScrollPosition));
        }

        protected override void OnDoubleClick(EventArgs e)
        {
            //base.OnDoubleClick(e);
            //Point MouseLocation = Control.MousePosition;
            int i = PageCoordinateToLineIndex(this.mouseDoubleClickPosition);
            if (i >= 0)
            {
                TextLineInformation lineToBeChanged = (TextLineInformation)documentLines[i];
                lineToBeChanged.Text = lineToBeChanged.Text.ToUpper();
                Graphics dc = this.CreateGraphics();
                
                uint newWidth = (uint)dc.MeasureString(lineToBeChanged.Text, mainFont).Width;
                if (newWidth > lineToBeChanged.Width)
                {
                    lineToBeChanged.Width = newWidth;
                }

                if (newWidth + 2 * margine > this.documentSize.Width)
                {
                    this.documentSize.Width = (int)newWidth;
                    this.AutoScrollMinSize = this.documentSize;
                }

                Rectangle chageRectangle = new Rectangle(LineIndexToPageCoordinate(i), new Size((int)newWidth, (int)this.lineHeight));

                this.Invalidate(chageRectangle);

            }
            base.OnDoubleClick(e);
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            this.mouseDoubleClickPosition = new Point(e.X, e.Y);
        }

    }
} --------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 不是吧几位,我的意思不是要绘图,绘图没有办法实现滚动条效果吧!

添加个textbox不行吗?

请高手帮帮忙! --------------------编程问答-------------------- 建立一个表格,将该图片设置为背景 --------------------编程问答-------------------- 大炮大苍蝇,居然把GDI搬出来了 --------------------编程问答-------------------- panel设置图片背景,顶部panel背景颜色transparent --------------------编程问答-------------------- 具体是什么意思 详细解释一下 --------------------编程问答-------------------- 具体怎么做?麻烦详细解释一下!谢谢! --------------------编程问答-------------------- 用层啊。 --------------------编程问答-------------------- <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<html>
<head>
<link rel="StyleSheet" type="text/css" href="css/bt.css">
<script type="text/javascript" src="js/msg_ajax.js"></script>
<title>电影详细信息</title>
</head>
<body>
<span id="btc" style="position:absolute"></span>
<form method="post" name="viewForm">
<table border="1">
<tr>
<td align="middle">
<img border="0" src="image/9.jpg" id="img1" onmouseover="getDetail('1')">
</td>
<td align="middle">
<img border="0" src="image/9.jpg"  id="img2" onmouseover="getDetail('2')">
</td>
<td align="middle">
<img border="0" src="image/9.jpg" id="img3" onmouseover="getDetail('3')">
</td>
</tr>
</table>
</form>
</body>
</html>

js
//
var title;
var body;
function CreateXmlHttpObject() {
var xmlHttp = null;
try {
    // Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
}
catch (e) {
    // Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function getDetail(id) {
var xmlHttp = CreateXmlHttpObject();
if (xmlHttp == null) {
alert("AJAX对象创建失败!");
return;
}
var url = "";
url = "http://localhost:9000/testAjax/doGetMsg.do?method=get&id=" + id + "&random=" + Math.random();
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
var temp = xmlHttp.responseText;
var display = temp.split(",");
title = display[0];
body = display[1];
enableTooltips(id);
}
};
xmlHttp.open("POST", url, true);
xmlHttp.send(null);
}
function enableTooltips(id) {
var e1;
if (!document.getElementById || !document.getElementsByTagName) {
return;
}
//AddCss();
//h = document.createElement("span");
//h.id = "btc";
//h.setAttribute("id", "btc");
//h.style.position = "absolute";
//document.getElementsByTagName("body")[0].appendChild(h);
e1 = document.getElementById("img"+id+"");
Prepare(e1);
}
function Prepare(el) {
var tooltip, t, b, s, l;
tooltip = CreateEl("span", "tooltip");
s = CreateEl("span", "top");
s.appendChild(document.createTextNode(title));
tooltip.appendChild(s);
b = CreateEl("b", "bottom");
b.appendChild(document.createTextNode(body));
tooltip.appendChild(b);
setOpacity(tooltip);
el.tooltip = tooltip;
el.onmouseover = showTooltip;
el.onmouseout = hideTooltip;
el.onmousemove = Locate;
}
function showTooltip(e) {
document.getElementById("btc").appendChild(this.tooltip);
Locate(e);
}
function hideTooltip(e) {
var d = document.getElementById("btc");
if (d.childNodes.length > 0) {
d.removeChild(d.firstChild);
}
}
function setOpacity(el) {
el.style.filter = "alpha(opacity:95)";
el.style.KHTMLOpacity = "0.95";
el.style.MozOpacity = "0.95";
el.style.opacity = "0.95";
}
function CreateEl(t, c) {
var x = document.createElement(t);
x.className = c;
x.style.display = "block";
return (x);
}
function AddCss() {
var l = CreateEl("link");
l.setAttribute("type", "text/css");
l.setAttribute("rel", "stylesheet");
l.setAttribute("href", "css/bt.css");
l.setAttribute("media", "screen");
document.getElementsByTagName("head")[0].appendChild(l);
}
function Locate(e) {
var posx = 0, posy = 0;
if (e == null) {
e = window.event;
}
if (e.pageX || e.pageY) {
posx = e.pageX;
posy = e.pageY;
} else {
if (e.clientX || e.clientY) {
if (document.documentElement.scrollTop) {
posx = e.clientX + document.documentElement.scrollLeft;
posy = e.clientY + document.documentElement.scrollTop;
} else {
posx = e.clientX + document.body.scrollLeft;
posy = e.clientY + document.body.scrollTop;
}
}
}
document.getElementById("btc").style.top = (posy + 10) + "px";
document.getElementById("btc").style.left = (posx - 20) + "px";
}

修改一下就可以了。 --------------------编程问答-------------------- 不好意思,没注意,我的这个是java的。不过方法类似,希望有帮助 --------------------编程问答--------------------
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,