用InvalidateRect部分重绘桌面
c#写的Rectangler=newRectangle((x-1)%400,0,1,500);InvalidateRect((IntPtr)null,refr,false...
c#写的
Rectangle r = new Rectangle((x-1) % 400, 0, 1, 500);
InvalidateRect( (IntPtr)null,ref r,false);
期望的运行结果是,桌面只有对应区域被重绘掉了
但实际结果是整个桌面(包括所有窗体)都在重绘
因为是在定时器里面执行这个,所以桌面在高频的闪烁
请教原因 展开
Rectangle r = new Rectangle((x-1) % 400, 0, 1, 500);
InvalidateRect( (IntPtr)null,ref r,false);
期望的运行结果是,桌面只有对应区域被重绘掉了
但实际结果是整个桌面(包括所有窗体)都在重绘
因为是在定时器里面执行这个,所以桌面在高频的闪烁
请教原因 展开
1个回答
展开全部
代码中RECT的引用有问题,以下是我写的示例代码~ 重点在于RECT的定义与C#中的Rectangle不同。
至于传空指针为什么导致全屏刷新,在API中零一般表示更新桌面窗口。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Drawing;
namespace test
{
class Program
{
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool RedrawWindow(IntPtr hwnd,ref RECT rcUpdate, IntPtr hrgnUpdate, int flags);
static void Main(string[] args)
{
while (true)
{
RECT trect;
trect.Left = 150;
trect.Right = 450;
trect.Top = 150;
trect.Bottom = 450;
RedrawWindow(GetDesktopWindow(), ref trect, IntPtr.Zero, 0x85);
IntPtr desk = GetDesktopWindow();
IntPtr deskDC = GetDCEx(desk, IntPtr.Zero, 0x403);
Graphics g = Graphics.FromHdc(deskDC);
g.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Red)), new Rectangle(100, 100, 400, 400));
Thread.Sleep(1000);
}
}
}
}
至于传空指针为什么导致全屏刷新,在API中零一般表示更新桌面窗口。
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;
using System.Drawing;
namespace test
{
class Program
{
public struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll", EntryPoint = "GetDCEx", CharSet = CharSet.Auto, ExactSpelling = true)]
private static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, int flags);
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool RedrawWindow(IntPtr hwnd,ref RECT rcUpdate, IntPtr hrgnUpdate, int flags);
static void Main(string[] args)
{
while (true)
{
RECT trect;
trect.Left = 150;
trect.Right = 450;
trect.Top = 150;
trect.Bottom = 450;
RedrawWindow(GetDesktopWindow(), ref trect, IntPtr.Zero, 0x85);
IntPtr desk = GetDesktopWindow();
IntPtr deskDC = GetDCEx(desk, IntPtr.Zero, 0x403);
Graphics g = Graphics.FromHdc(deskDC);
g.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Red)), new Rectangle(100, 100, 400, 400));
Thread.Sleep(1000);
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询