gpt4 book ai didi

c# - RichTextBox 的 AnimateWindow API 透明度问题

转载 作者:太空宇宙 更新时间:2023-11-03 16:52:20 25 4
gpt4 key购买 nike

我正在使用 AnimateWindow API 来显示或隐藏带有幻灯片动画的 Form。问题是,如果表单包含一个 RichTextBox 控件,它就不会正确显示该控件。它是透明的,不显示任何文本。

动画完成后,双击控件中的某处将显示文本行。

我已经创建了一个完整的示例,任何人都可以使用它来编译和测试自己。没有更好的调试方法,除非您已经知道答案。

主窗体中有 2 个按钮,一个用于显示另一个窗体,另一个用于隐藏同一个窗体。我将 RichTextBox 添加为简单的 TextBox。正如您将看到的,问题只发生在 RichTextBox 上。

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1 {
static class Program {
public const int AW_ACTIVATE = 0x00020000;
public const int AW_HIDE = 0x00010000;
public const int AW_HOR_NEGATIVE = 0x00000002;
public const int AW_HOR_POSITIVE = 0x00000001;
public const int AW_SLIDE = 0x00040000;

[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AnimateWindow(IntPtr hWnd, int time, int awFlags);

[STAThread]
public static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

public class Form1 : Form {
public Form form;

public Form1() {
InitializeComponent();
form = new Form2();
form.Show();
form.Hide();
}

private void button1_Click(object sender, EventArgs e) {
form.Location = new Point(Location.X, Location.Y + form.Height + 100);
Program.AnimateWindow(form.Handle, 1000, Program.AW_SLIDE | Program.AW_HOR_NEGATIVE | Program.AW_ACTIVATE);
form.Show();
}

private void button2_Click(object sender, EventArgs e) {
Program.AnimateWindow(form.Handle, 1000, Program.AW_HIDE | Program.AW_HOR_POSITIVE);
form.Hide();
}

#region Windows Form Designer generated code

private void InitializeComponent() {
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(11, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(133, 114);
this.button1.TabIndex = 0;
this.button1.Text = "SHOW";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button2.Location = new System.Drawing.Point(150, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(133, 114);
this.button2.TabIndex = 1;
this.button2.Text = "HIDE";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(294, 138);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);
}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}

public class Form2 : Form {
public Form2() {
InitializeComponent();
}

#region Windows Form Designer generated code

private void InitializeComponent() {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2));
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 50);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
this.textBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.textBox1.Location = new System.Drawing.Point(0, 57);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(240, 50);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(240, 107);
this.ControlBox = false;
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}

#endregion

private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox1;
}
}

注意:我正在为此悬赏,所以如果你要回答,请回答一个有效的解决方案,而不是让我尝试的东西,看看它是否有效。为什么?因为如果人们赞成您的答案并且没有解决任何问题,它仍将被标记为已接受但没有帮助。感谢您的理解。

最佳答案

请看这段代码...这是我见过的最奇怪的错误...唯一的区别是我必须捕获 RichTextBox 的 VisibleChanged 事件,并设置 Capture 属性,更新它,然后关闭 Capture,我认为是向控件发送消息以强制双击事件,但发现这不是必需的......我也有覆盖 form2 本身的 OnActivated 事件,以便在动画完成后 捕获事件并强制刷新...奇怪的奇怪错误....

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Text;

namespace WindowsFormsApplication1
{
static class Program
{
public const int AW_ACTIVATE = 0x00020000;
public const int AW_HIDE = 0x00010000;
public const int AW_HOR_NEGATIVE = 0x00000002;
public const int AW_HOR_POSITIVE = 0x00000001;
public const int AW_SLIDE = 0x00040000;

[DllImport("user32")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool AnimateWindow(IntPtr hWnd, int time, int awFlags);

[STAThread]
public static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}

public class Form1 : Form
{
public Form form;

public Form1()
{
InitializeComponent();
form = new Form2();
form.Show();
form.Hide();
}

private void button1_Click(object sender, EventArgs e)
{
form.Location = new Point(Location.X, Location.Y + form.Height + 100);
Program.AnimateWindow(form.Handle, 1000, Program.AW_SLIDE | Program.AW_HOR_NEGATIVE | Program.AW_ACTIVATE);
form.Show();
}

private void button2_Click(object sender, EventArgs e)
{
Program.AnimateWindow(form.Handle, 1000, Program.AW_HIDE | Program.AW_HOR_POSITIVE);
form.Hide();
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button1.Location = new System.Drawing.Point(11, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(133, 114);
this.button1.TabIndex = 0;
this.button1.Text = "SHOW";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.button2.Location = new System.Drawing.Point(150, 12);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(133, 114);
this.button2.TabIndex = 1;
this.button2.Text = "HIDE";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(294, 138);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.ResumeLayout(false);
}

#endregion

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}

public class Form2 : Form
{
public Form2()
{
InitializeComponent();
this.richTextBox1.VisibleChanged += new EventHandler(richTextBox1_VisibleChanged);
}

void richTextBox1_VisibleChanged(object sender, EventArgs e)
{
if (this.richTextBox1.Visible)
{
System.Diagnostics.Debug.WriteLine("Visible!");
this.richTextBox1.Capture = true;
this.richTextBox1.Update();
this.richTextBox1.Capture = false;
this.richTextBox1.Refresh();
}
else System.Diagnostics.Debug.WriteLine("InVisible!");
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form2));
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(240, 50);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
this.textBox1.Dock = System.Windows.Forms.DockStyle.Bottom;
this.textBox1.Location = new System.Drawing.Point(0, 57);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textBox1.Size = new System.Drawing.Size(240, 50);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.";
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(240, 107);
this.ControlBox = false;
this.Controls.Add(this.textBox1);
this.Controls.Add(this.richTextBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "Form2";
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}

#endregion

protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
System.Diagnostics.Debug.WriteLine("OnActivated");
this.Refresh();
}

private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TextBox textBox1;
}
}

代码仍然有效 - 一件非常晦涩的事情 - 有趣的是在 VisibleChanged 处理程序中,我期望在单击“隐藏”按钮时调试输出显示“InVisible”,但它没有。我确实尝试使用 WM_PRINTWM_PRINTCLIENT 消息,但富文本框失败了,只是不要问我为什么要在富文本框本身上设置捕获并强制刷新解决了它......这是来自MSDN的引述对此:

The window procedures for the window and its child windows should handle any WM_PRINT or WM_PRINTCLIENT messages. Dialog boxes, controls, and common controls already handle WM_PRINTCLIENT. The default window procedure already handles WM_PRINT. If a child window is displayed partially clipped, when it is animated it will have holes where it is clipped.

我不知道是否值得麻烦向 Microsoft 指出这一点并说“AnimateWindow 或 RichTextBox Control with .NET 中存在错误”,无论哪种方式,都很难确定,因为在动画完成,文本框显示正常,但富文本框不正常,但在捕获打开时显式使用 CaptureUpdate,强制执行刷新...它在错误本身上双向摆动 - 可能在两个位置...无论如何非常不寻常和奇怪..希望代码对您有用。

关于c# - RichTextBox 的 AnimateWindow API 透明度问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3507355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com