473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Doublebuffering of more then one object

I have a couple of objects(eg. Rectangles) for dragging them I'm using
double buffering, and it works fine while using only one of them. Once I'
trying to put another object on screen the first one is disapperas and there
is flicking while draggings.
Please advice.

--
Tamir Khason
You want dot.NET? Just ask:
"Please, www.dotnet.us "
Nov 16 '05 #1
1 1044
Tamir Khason wrote:
I have a couple of objects(eg. Rectangles) for dragging them I'm using
double buffering, and it works fine while using only one of them. Once I'
trying to put another object on screen the first one is disapperas and
there is flicking while draggings.
Please advice.


I'm assuming you're talking about a Windows Forms application here?

When you say you have a couple of objects, what exactly do you mean? Do you
mean multiple controls? Or just that you're drawing multiple features?

It's very hard to work out what the problem might be with your very brief
description - I have no idea what your code is doing. But here's an example
that shows lots of draggable rectangles, all of which can be dragged around
without any flicker whatsoever:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace DragRectangles
{
public class DragRectangles : System.Windows.Forms.Form
{
private Rectangle[] rectangles = new Rectangle[10];

public DragRectangles()
{
Text = "Rectangles";
SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.AllPaintingInWmPaint, true);
Random rand = new Random();
for (int i = 0; i < rectangles.Length; ++i)
{
rectangles[i] = new Rectangle(
rand.Next(Width - 100), rand.Next(Height - 100),
rand.Next(100) + 10, rand.Next(100) + 10);
}
}

protected override void OnPaint(PaintEventArgs e)
{
using (Brush b = new SolidBrush(Color.FromArgb(30,
Color.DarkBlue)))
using (Pen p = new Pen(Color.Black, 2))
{
for (int i = 0; i < rectangles.Length; ++i)
{
e.Graphics.FillRectangle(b, rectangles[i]);
e.Graphics.DrawRectangle(p, rectangles[i]);
}
}
base.OnPaint (e);
}

private bool dragging = false;
private int rectangleBeingDragged;
private int dragPrevX, dragPrevY;

protected override void OnMouseDown(MouseEventArgs e)
{
for (int i = 0; i < rectangles.Length; ++i)
{
if (rectangles[i].Contains(e.X, e.Y))
{
dragging = true;
dragPrevX = e.X;
dragPrevY = e.Y;
rectangleBeingDragged = i;
break;
}
}
base.OnMouseDown (e);
}

protected override void OnMouseUp(MouseEventArgs e)
{
dragging = false;
base.OnMouseUp (e);
}

protected override void OnMouseMove(MouseEventArgs e)
{
if (dragging)
{
int dx = e.X - dragPrevX;
int dy = e.Y - dragPrevY;
Rectangle original = rectangles[rectangleBeingDragged];
rectangles[rectangleBeingDragged].Location += new Size(dx,
dy);

Rectangle updateArea =Rectangle.Union(original,
rectangles[rectangleBeingDragged]);
updateArea.Inflate(1,1);
Invalidate(updateArea);
dragPrevX = e.X;
dragPrevY = e.Y;
}
base.OnMouseMove (e);
}

[STAThread]
static void Main()
{
Application.Run(new DragRectangles());
}
}
}
--
Ian Griffiths - http://www.interact-sw.co.uk/iangblog/
DevelopMentor - http://www.develop.com/
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: bearophile | last post by:
Ville Vainio: >It's highly typical for the newbies to suggest improvements to the >language. They will usually learn that they are wrong, but the >discussion that ensues can be fruitfull anyway...
21
by: Rabbit63 | last post by:
Hi: I want to show a set of records in the database table on the clicnt browser. I have two ways to do this (writen in JScript): 1.The first way is: <% var sql = "select firstname from...
0
by: Joe Kraft | last post by:
I'm hoping someone can help me out. We're running a website that uses XML/XSLT transformations, VB.Net and an Oracle database. Currently the site cannot support more than 6-7 users at a time...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
2
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a...
161
by: KraftDiner | last post by:
I was under the assumption that everything in python was a refrence... so if I code this: lst = for i in lst: if i==2: i = 4 print lst I though the contents of lst would be modified.....
4
by: =?Utf-8?B?bGpsZXZlbmQy?= | last post by:
Is there any way to enable double buffering in an MdiClient control? I have a case where I need to hide all of the MdiClient forms and interactively draw a custom background in an MdiClient...
4
by: =?Utf-8?B?U2ltb24gVGFtbWFu?= | last post by:
Hi, Re-posting this as the formatting on the other post is terrible. If I inherit from a control with the following OnPaint override and draw in the DoPaint method then i get odd UI results...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.