|
You should really read the .NET Framework SDK documentation. Interop'ing correctly between managed and unmanaged code also requires some knowledge of both, like knowing that a LONG in unmanaged C/C++ is a 32-bit integer, so it is an int in managed code.
You can read more about interoperability by reading Interoperability with Unmanaged Code[^] in the .NET Framework SDK online in the MSDN Library.
Also, all the primitive types are marshalled automatically by the CLR. You can also marshal structs (though nested structs are not supported by the CLR, nor are marshalling structs at all supported by the .NET Compact Framework (CF)) and reference types.
It also helps to know about the differences between reference and values type. For example, if you were P/Invoking a pointer to a struct (a value type), you'd use the ref or out keyword in both the method declaration as well as the parameter when calling the method. If you needed to pass a pointer to an interface, you wouldn't use either keyword because an interface is a reference type already.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I recently wrote a couple of lines of code which lets me move controls on my form just by dragging them with the mouse. Now that I can move controls around, I noticed something. A control with a transparent background will allow you to see the background of your form, but it still hides other controls on my Form. Furthermore, if I have different colors on my Form's background, I'll get a distortion momentarily on my control as it moves around. I'd like to be able to have movable controls without either of these issues. So my question is this:
Can I do something to my controls so that the transparent background actually works how I want it to?
Can I somehow change the region my control takes up so that I don't have this extra "space" around it? For instance, if I had a ball, why do I need the entire square?
Thanks for any help
|
|
|
|
|
DougW48 wrote:
Can I somehow change the region my control takes up so that I don't have this extra "space" around it? For instance, if I had a ball, why do I need the entire square?
Because every control is a window and uses a rectangular region. If you want to add a clipping region to your control, override OnPaint and set the Graphics.Clip property to a Region that contains only what you want (like a circle for your aforementioned control).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi all,
This may be a rather unusual question but as I'm a newbie in C#, I would be grateful if someone can please elaborate. Well, what I want to know is that why is the components variable added everytime in the class even when it is not used anywhere else. Every class generated using the Designer in VS.NET adds this variable in the class. Why? What is the purpose?
Thx,
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
you should add all your disposable items to the components IContainer.when calling IContainer.Dispose() it will call Dispose() on all the components it contains. Its merely there for ease of use, like the Events protected property in COntrols.
|
|
|
|
|
hi,
I've been coding this PieChart control all day now, just couldn't get it to work, if you know how to write a custom control, plese help me.
The main problem I have here is at the OnPaint() the PieChart doesn't draw all the pies except the first pie. What am I missing on writing a custom control here?
Complete codes are below or you can download the project here
PieChart.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using System.Data;
using System.Windows.Forms;
namespace ColorControl.Controls
{
public class PieChart : System.Windows.Forms.Control
{
private PieChartItemCollection _Items = new PieChartItemCollection();
public PieChart()
{
base.Size = new Size(200, 200);
SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.DoubleBuffer |
ControlStyles.UserPaint,
true
);
}
public PieChartItemCollection Items
{
get
{
return _Items;
}
set
{
_Items = value;
this.Invalidate();
}
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
int valueSum = 0;
int startDegree = 0;
int sweepDegree;
foreach (PieChartItem item in _Items)
{
valueSum += (int)item.Value;
}
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rect = new Rectangle(ClientRectangle.X, ClientRectangle.Y,
ClientRectangle.Width - 1, ClientRectangle.Height - 1);
if (valueSum != 0)
{
foreach (PieChartItem item in _Items)
{
sweepDegree = (int)(item.Value / valueSum * 360);
SolidBrush brush = new SolidBrush(item.Color);
e.Graphics.FillPie(brush, rect, startDegree, sweepDegree);
if (_Items.Count > 1)
{
e.Graphics.DrawPie(new Pen(Color.Black), rect, startDegree, sweepDegree);
}
startDegree += sweepDegree;
}
}
e.Graphics.DrawEllipse(new Pen(Color.Black, 2), rect);
}
}
}
PieChartItem.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ColorControl.Controls
{
[DefaultProperty("Value")]
public class PieChartItem
{
private Color _Color;
private string _Text;
private int _Value;
public PieChartItem()
{
_Color = Color.Red;
_Text = "Item";
_Value = 0;
}
public Color Color
{
get
{
return _Color;
}
set
{
_Color = value;
}
}
public string Text
{
get
{
return _Text;
}
set
{
_Text = value;
}
}
public int Value
{
get
{
return _Value;
}
set
{
_Value = value;
}
}
}
}
PieChartItemCollection.cs
using System;
using System.Collections;
namespace ColorControl.Controls
{
public class PieChartItemCollection : CollectionBase
{
public PieChartItemCollection() : base()
{}
public PieChartItem this[int index]
{
get
{
if (index >= 0 && index < base.Count)
return (base.List[index] as PieChartItem);
else
return null;
}
set
{
base.List[index] = value;
}
}
public void Add(PieChartItem item)
{
base.List.Add(item as object);
}
public void AddRange(PieChartItem[] items)
{
foreach (PieChartItem item in items)
{
Add(item);
}
}
public void Insert(int index, PieChartItem item)
{
if (index >= 0)
{
base.List.Insert(index, item as object);
}
}
public void Remove(PieChartItem item)
{
base.List.Remove(item as object);
}
public void RemoveAt(int index)
{
if (index >= 0 && index < base.Count)
{
base.RemoveAt(index);
}
}
public void RemoveAll()
{
for (int index = 0; index < base.Count; index++)
{
base.List.RemoveAt(index);
}
}
public bool Contains(PieChartItem item)
{
return base.List.Contains(item as object);
}
public int IndexOf(PieChartItem item)
{
return base.List.IndexOf(item as object);
}
}
}
There is also a small problem is that all "Pies" of PieChart have the same color, I will solve this as soon as I solve the previous problem.
Thank!!!
|
|
|
|
|
You should use Matrix transforms to rotate the drawing "canvas".
|
|
|
|
|
Can you explain please, I really want this control to work.
Thank!
|
|
|
|
|
I've developed an application and would like as part of the setup to place an Icon on the Desktop and the Quick Launch toolbar.
Ideally, I'd like to ask the client if they would like this to be done.
Thanks,
cb
|
|
|
|
|
You just need to create a shortcut to your application's EXE in the directories designated for quick launch and desktop shortcuts
\Documents and Settings\username \Application Data\Microsoft\Internet Explorer\Quick Launch
and,
\Documents and Settings\username \Application Data\Desktop
where username is the name of the current user
Gurmeet BTW, can Google help me search my lost pajamas?
My Articles: HTML Reader C++ Class Library, Numeric Edit Control
|
|
|
|
|
Thanks alot.
I did a search for th quicklaunch folder but it I could'nt find it. You set me on the correct path.
I had the Desktop nailed and will probably use "all users".
This all being said, how do you create these two short cuts within the setup routine????
Thanks,
cb
|
|
|
|
|
If you use the Windows Installer projects to deploy your application, open the file system editor view, right-click on the root folder ("File System on Target Machine") and add the "User's Desktop" (if not there already) ad "User's Application Data". To the latter folder, add sub-directories like Microsoft\Internet Explorer\Quick Launch".
Now, here's the trick. You'll need to compile the package and use something like Orca - an MSI package editor from the Windows Installer SDK - to go the Property table and create a new property called "ALLUSERS" and set the value to 2. You can't really do this in VS.NET's installer project because it frankly stinks. This is a cinche in professional packages like those from Wise Solutions[^] and Install Shield[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thank you.
You know for something as common as asking a user "Do you want a shortcut on your quick launch toolbar" and/or "Do you want a shortcut placed on your desktop" and then have them check the appropriate checkboxes should not be so difficult.
Since I own the community that this applications is going to be distributed to, I don't have to ask the questions.
I've downloaded Orca and will give it a whirl.
I've googled this to death and there isn't much out there.
Thanks again.
cb
|
|
|
|
|
It's not difficult if you use the right tools. VS.NET is meant to be a simple deployment solution. If one is knowledgable about Windows Installer (I've been using it since 1.0 beta days and even beta test it for Microsoft) you can use tools like Orca. There's also commercial tools out there that make these sort of things easy. VS.NET is not a viable solution for anything other than installing files and running custom actions written in .NET using the Installer class (perhaps a few other things, though not much).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
What are the 'commercial tools' you mention?
cb
|
|
|
|
|
I already provided links to two of the most well-known commerical tools for Windows Installer packages in this thread, sorted in order of my personal preference and cost (ISFWI is WAY too expensive and slow to support features unlike Wise).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Missed that, danm Evyln Woods!
Thanks,
cb
|
|
|
|
|
Gurmeet S. Kochar wrote:
\Documents and Settings\username\Application Data\Desktop
Actually, get rid of "Application Data" here. It's just \Documents and Settings\username\Desktop.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hello everyone.
I'm new to C#, and not all that skilled in C++(only 1 semester). I am however decent in VB.
Anyway, I'm wondering if you could tell me how to, on startup, change the screen resolution and maximize my app fullscreen. My form is 1024x768 and I want it to fill the screen regardless of desktop reso.
This is just a simple game I am creating while learning the language a bit. This info would be greatly appreciated! Have a good day.
Acidon
|
|
|
|
|
Set the FormBorderStyle to None , and maximize your form. When you do this, all appbars, including the taskbar, get a notification that a full-screen app wants the whole screen, so they run and hide.
Don't forget, that's Persian Gulf not Arabian gulf!
Murphy: Click Here![^] I'm thirsty like sun, more landless than wind...
|
|
|
|
|
Hey, thanks for the reply.
I'm currently doing that. What I am wanting to do is actually change the resolution to 1024x768 while the game(app) is running, and then release it back to the desktop defined resolution upon exit.
This is just a simple project while i learn the language, but I would like to keep things simple in that respect and have it change to that reso when I load it. Any ideas, comments, suggestions and/or flames appreciated.
Thanks,
Acidon
|
|
|
|
|
|
Perfect. Thank you very much.
I did search for this type of info but never found anything. I obviously didn't use the right keywords. At any rate, I appreciate the reply. Take care,
Acidon
|
|
|
|
|
Hey
I am trying to make a simple 2d racing game, the wiev is like GTA.
I have som problem with the car handling. I am using this math right now,
double radians;
double tan;
radians = this.Rotation * (Math.PI/180);
tan = Math.Tan(radians);
Position.X = this.CarBox.Location.X + this.Speed;
Position.Y = this.CarBox.Location.Y + (int)(this.Speed * tan);
this.CarBox.Location = Position;
The speed is between 0-20 and the rotation is 0-360 degree.
The car is now driving forward, when im turning the car rotates like it shild and it moves in other direction, but if i turn to much toward 0 degree(UP) och 180(down) then it will flip and disipare from the screan, the y value is then somthing around -32000.
I have also notis that if i drive in around 45 degreee angle it will drive a bit faster hen if i drive forward(right).
Im not that good at math so i hope somone could help me with this.
Best regards
Jimmy
|
|
|
|
|
Snowjim wrote:
radians = this.Rotation * (Math.PI/180);
lol no, what are you doing here? Math.Pi is 180 deg in radians!
[edit] my bad, the code was just lookign a bit confusing, why are you using degrees at all?[edit]
|
|
|
|