|
You have at least 2 solutions:
1. Create a class that extends PictureBox (or Control if you get adventurous) and override OnPaint().
2. Keep a copy of the image in memory.
I think the 1st option is the better choice because it opens the possibility for code-reuse and uses less memory.
Example:
public class PictureSelectionBox : PictureBox
{
private Rectangle selectionRect;
private Pen selectionPen;
public Rectangle CurrentSelection
{
get { return this.selectionRect; }
set
{
this.selectionRect = value;
this.Refresh();
}
}
public Pen SelectionPen
{
get { return this.selectionPen; }
set
{
this.selectionPen = value;
this.Refresh();
}
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (this.selectionPen != null &&
this.selectionRect.Width != 0 &&
this.selectionRect.Height != 0)
{
e.Graphics.DrawRectangle(this.selectionPen, this.selectionRect);
}
}
public void CommitSelection()
{
Graphics g = Graphics.FromImage(base.Image);
g.DrawRectangle(this.selectionPen, this.selectionRect);
g.Dispose();
this.CurrentSelection = new Rectangle(0, 0, 0, 0);
this.Refresh();
}
}
You could even override the OnMouseDown, OnMouseMove, and OnMouseUp events in the derived class to allow the user to make his/her selection. You better watch out with the use of Refresh() doing this though because you'll get some ugly flickering.
I'd either [DllImport] InvalidateRect() or something like:
Graphics g = this.CreateGraphics();
OnPaint(new PaintEventArgs(g, this.ClientRectangle));
g.Dispose();
Enjoy,
David
|
|
|
|
|
Thank You David,
Let me check it out!
|
|
|
|
|
I have 2 DateTime Vars.
DateTime dtFrom = '1/1/2005';
DateTime dtTo = '1/24/2005';
How can I select a period of: Week, 2 Weeks, Month...
And Divide the date range into it to something like this:
Example: 1 week:
1 2 3 4 5
======== ======== ========= ========= =========
1/1/2005 1/2/2005 1/9/2005 1/16/2005 1/23/2005
1/1/2005 1/8/2005 1/15/2005 1/22/2005 1/24/2005
======== ======== ========= ========= =========
|
|
|
|
|
First Row Differences:
1-1-05, 1-2-05 = 1 Day
1-2-05, 1-9-05 = 7 Days
1-9-05, 1-16-05 = 7 Days
1-16-05, 1-23-05 = 7 Days
Second Row Differences:
1-1-05, 1-8-05 = 7 Days
1-8-05, 1-15-05 = 7 Days
1-15-05, 1-22-05 = 7 Days
1-22-05, 1-24-05 = 2 Days
I don't see any patterns in the differences here, so what exactly do you want again?
What do you mean by "divide the date range into it" ?
What do you want to do?
Are looking for every Sunday/Saturday in the month?
The work-week days?
Display a calender to the screen?
David
|
|
|
|
|
you were reading this wrong.
1/1/2005 - 1/1/2005 - 1 (only one day cos' it is last day of a week)
1/2/2005 - 1/8/2005 - 2 (full second week)
...and so on...
got it?
|
|
|
|
|
Ok, well you could try something like:
DateTime dtStart = new DateTime(2005, 1, 1);
DateTime dtEnd = new DateTime(2005, 1, 30);
DateTime dtCurrent = dtStart;
while (dtCurrent < dtEnd)
{
double dDays;
if (dtCurrent.DayOfWeek != DayOfWeek.Sunday)
dDays = 6.0 - (double)dtCurrent.DayOfWeek;
else if ((dtCurrent.Day + 6) <= dtEnd.Day)
dDays = 6.0;
else
dDays = dtEnd.Day - dtCurrent.Day;
Console.Write(dtCurrent + " - ");
DateTime dtEndOfWeek = dtCurrent.AddDays(dDays);
Console.WriteLine(dtEndOfWeek);
dtCurrent = dtEndOfWeek.AddDays(1.0);
}
David
|
|
|
|
|
i need to understand what kind of object is just one..
i have myOwn it could be of Car or Bike.
How can i understand it?
i thought about:
if( typedef(myOwn) == Car)
but typedef doesn't exist in c# 1.1
I don't want to use try catch,
tnx
|
|
|
|
|
your best bet is to use typeof()
if(typeof(myOwn) == Car)
DoSomethingWithCar(myOwn as Car)
else
DoSomethingWithBike(myOwn as Bike)
The reason why a poor man will always be poor and the rich man will be rich, is because the poor always maximizes his expenditures and the rich man always maximizes his potential. --T.Parker
|
|
|
|
|
if(myOwn is Car)<br />
{<br />
}
or
if(myOwn.GetType() == typeof(Car))<br />
{<br />
}
They are the same in essence.
|
|
|
|
|
Just to add to J4amieC 's suggestion, if you need to actually use the methods of your specific class, you could use the as keyword to reduce the number of casts that you'll use. For example:
Car c = someObject as Car;
if( c != null )
{
}
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
|
|
|
|
|
There's a small difference between using is and as .
If you use as and then check for null you don't know if someObject actually is a Bike or if it's a Car with value null .
With is you can really test for the correct type.
Usually this won't matter, but it's a different meaning nevertheless...
Regards,
mav
|
|
|
|
|
Well the idea is:
Car car = someObj as Car;
Boat boat = someObj as Boat;
Plane plane = someObj as Plane;
if (car != null) {
}
else if (boat != null) {
}
else if (plane != null) {
}
The assumption is that your testing classes on the same level in the class hierarchy.
If your not testing classes on the same level, then the difference between is and as matters. But you could also write your if-else statement so that it tests the more specific class types to the more broad class types.... lol
Anyways,
David
|
|
|
|
|
I agree with you about most of that. If I use typeof on a null value, then I still not get what I need in terms of runtime type information. Here, a use of as prevents an extra cast later on. For example, in the following code, we pass null values and with both is and as , we cannot retrieve runtime type information:
public class Foo {}
public class Goo {}
public class Test
{
[STAThread]
static void Main( string[] args )
{
Foo f = null;
Goo g = null;
Foo ff = new Foo();
Goo gg = new Goo();
Console.WriteLine( Form1.TestMeAsFoo( f ) );
Console.WriteLine( Form1.TestMeAsFoo( g ) );
Console.WriteLine( Form1.TestMeAsFoo( ff ) );
Console.WriteLine( Form1.TestMeAsFoo( gg ) );
Console.WriteLine( Form1.TestMeIsFoo( f ) );
Console.WriteLine( Form1.TestMeIsFoo( g ) );
Console.WriteLine( Form1.TestMeIsFoo( ff ) );
Console.WriteLine( Form1.TestMeIsFoo( gg ) );
}
public static bool TestMeIsFoo( object o )
{
return ( o is Foo );
}
public static bool TestMeAsFoo( object o )
{
return ( o as Foo ) != null;
}
}
"we must lose precision to make significant statements about complex systems."
-deKorvin on uncertainty
-- modified at 15:24 Monday 5th December, 2005
|
|
|
|
|
Hello all, I am currently taking a C# programming class and have no prior knowledge of any programming. I am to write a program that takes a user's input and encrypt it, only using the ASCII code table (32-126). Now I know that there is a way of doing without re-inventing the wheel to say. I just can not find it. I think I need to use a character array, but just can not figure it out. Also I know that there is something in the System.Security that does this, but can not find out any information. Please help. Thanks
Rich Poulin
|
|
|
|
|
|
Hi,
I am making an application in VSC# 2005 which has a "WebBrowser" control in it, I want to be able to manipulate a webpage in the webrowser control from within the application.
I have worked out how to get (and set) the text in a form on the webpage with something like;
webBrowser1.Document.GetElementById("Textbox1").InnerText.ToString();
which is fine, but I just can't work out how to invoke a button on the webpage. Basically I want to get the text from the form, change it, paste it back into the form (all of which I can do) and then call the "Save" button to save my changes (which I can't do). Any help would be greatly appreciated!
Thanks in advance, Martin.
|
|
|
|
|
OK I worked out one way, by putting the user focus on the correct button, then simulating pressing the enter key;
webBrowser1.Document.GetElementById("Save").Focus();
SendKeys.Send("{ENTER}");
but it's pretty ugly, and possibly not very robust. Anyone got any better ideas?
thanks
|
|
|
|
|
how can I do to get the disk space of a computer of a domain (The domain hasn´t got Active Directory)?
thanks
|
|
|
|
|
This may not be possible unless you have an application sitting on the target machine that will publish how much disk space is free.
On the other hand, I'm no expert when it comes to Windows networking, so take this with a grain of salt.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Bought a House!
Judah Himango
|
|
|
|
|
how can I do to get a list of all the computer of a domain (The domain hasn´t got Active Directory)?
And how can with a list the printers?
|
|
|
|
|
i am working with [b]Windows control library[/b] in c#.i am using ShDocVw.dll in my project. i add a refrence of this dll into my project. but the problem is that when i complie this project it gives me an error dll doesn't have strong name. i already have one another dll with strong name. how can i add another dll using strong name. or i s there is any anther alternative to put this dll into Globlal Assembly.
Amit Grover
CDAC R&D
Gist Group
pune
|
|
|
|
|
Hi Amit,
If you want to add shdocvw.dll to the gac go to your project and then
In the solution explorer right click on the project name in the window.In the contextual menu click properties under the common properties\general category
you will see wrapper assembly for activex\com objects in the 'wrapper assembly key file' give the name of .snk file with wchih you want to sign this assembly.you can use the same key.compile it and then register it using gacutil.Hope it Helps.
Rakesh
|
|
|
|
|
i go through the same way as u have written.i named ShDocVw to "wrapper assembly key file" . where do i use this .snk Key.i had already used "AssemblyKeyFile" attrubute in Assemblyinfo.cs file. becoz i m using one another dll also.
after giving name ShDocVw to "wrapper assembly key file" when i compile.it is giving the same error " ShDocVw.dll does have strong name. if i used second AssembleKeyFile attribute in assemblyinfo.cs it is giving an error "duplicate assembly key file attribute" . how can i solve this problem
Amit Grover
CDAC R&D
Gist Group
pune
|
|
|
|
|
Hi,
I display a lot of images in a context menu, using OwnerDraw=true and overring OnMeasureItem/OnDrawItem. The automatically displayed arrows are great, but the menu takes all the screen height! My boss want the menu to be shorter...
I tried to display only a few images, and added my own arrow items at the beginning and the end of them, but I can't make the menu scroll when I click/select the arrows!
- when clicking the arrows, the menu automatically closes
- when selecting them, I dynamically modify the images items, but I can't make the menu update!
I tried everything: Refresh/Update/Invalidate of the parent control, PerformSelect on all the images items, calling SendMessage with WM_PAINT, WM_PRINT, WM_PRINT_CLIENT with the menu handle, and the parent control handle, I've even tried to set to false then back to true all the images items OwnerDraw, they update more or less, but the false ownerdraw menu (brief) displaying is awful!
I didn't manage to find an answer to my problems on the web, so if someone have a clue it would be great to tell me! Either about how making my long menu not taking all the screen height, or forcing the menu to update when I dynamically modify it!
Thanks a lot
Thibaud
|
|
|
|
|
Allow the menu to close as normal when the arrows are selected, update the items, and make it appear in the same location again.
If you added the context menu to the control using the "ContextMenu" Property create a delegate for the MouseDown event and note the location of the mouse when the right mouse button is clicked.
Update the menu items, then call:
myCtrl.ContextMenu.Show(myCtrl, savedX, savedY);
David
|
|
|
|