moyan_min 2014-05-22 01:30 采纳率: 0%
浏览 987

C# 窗口移动,文字显示

c#代码:
this.Opacity = 1;
Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - 968, Screen.PrimaryScreen.WorkingArea.Height - 250);
this.PointToScreen(p);
this.Location = p;
this.Show();
for (int i = 0; i <= this.Height; i += 2)
{
this.Location = new Point(p.X, p.Y - i);
Thread.Sleep(10); // 将线程沉睡时间调的越小升起的越快
}

            this.timer2.Enabled = true;
            this.timer2.Start();

窗口一直在移动,可是在移动过程中文字为何不显示呢??

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-02 03:18
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在你的 C# 代码中,你正在尝试使用 System.Windows.Forms.Timer 来控制一个窗口的移动。然而,如果你没有正确地将文本设置为透明,那么这个操作可能不会生效。

    以下是一个基本的例子,展示了如何创建一个包含文本的窗口,并且通过 System.Windows.Forms.Timer 控制其位置和大小:

    using System;
    using System.Windows.Forms;
    
    public class WindowControl : Form
    {
        private string _text;
    
        public WindowControl(string text)
        {
            _text = text;
            Text = _text;
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawString(_text, Font, Brushes.Black, new Rectangle(0, 0, Width, Height));
        }
    }
    
    class Program
    {
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
    
            var window = new WindowControl("Hello World");
            window.Show();
    
            var timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += Timer_Tick;
            timer.Enabled = true;
            timer.Start();
    
            void Timer_Tick(object sender, EventArgs e)
            {
                window.Location = new Point(window.Location.X + 1, window.Location.Y + 1);
            }
        }
    }
    

    在这个例子中,我们首先创建了一个包含文本的窗体,然后设置了它的背景颜色为黑色。接着,我们在 OnPaint 方法中绘制了文本。最后,在主循环中,我们创建了一个定时器并为其添加了一个事件处理器,当定时器触发时,我们将窗口的位置增加一单位。

    请注意,这只是一个非常基础的例子,实际的应用程序可能会更复杂,包括处理用户输入、响应用户的点击等。

    评论

报告相同问题?