|
Well, to copy a struct to a byte[], you need some marshaling code..It uses the Marshal class (as if that wasn't obvious) to allocate some memory..Like this:
(i usually declare methods like this as an instance method and a static method with a parameter of whatever type it's defined in)
<pre><br />
public byte[] ToBytes()<br />
{<br />
byte[] buff = new byte[Marshal.SizeOf(typeof(MyType))];<br />
GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);<br />
Marshal.StructureToPtr(this, handle.AddrOfPinnedObject(), false);<br />
handle.Free();<br />
return buff;<br />
}<br />
</pre><br />
<br />
That code will marshal the struct to a byte[], and from that code, the code for marshaling back to a struct comes easy:<br />
<br />
<code lang=cs><pre><br />
public static MyType FromBytes(byte[] buff)<br />
{<br />
GCHandle handle = GCHandle.Alloc(buff, GCHandleType.Pinned);<br />
MyType mt = (MyType)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(MyType));
handle.Free();<br />
return mt;<br />
}<br />
</pre><br />
<br />
<br />
Now as for chaining structures together, you can do that yourself using Array.Copy() and copying into a byte[]<br />
<br />
Passing back the data as an IntPtr requires only slight modification to the original functions:<br />
<br />
<code lang=cs><pre><br />
public IntPtr ToIntPtr()<br />
{<br />
byte[] buff = new byte[Marshal.SizeOf(typeof(MyType))];<br />
IntPtr handle = Marshal.AllocHGlobal(buff.Length);<br />
Marshal.StructureToPtr(this, handle, true);
return handle;<br />
}<br />
<br />
public static MyType FromIntPtr(IntPtr ptr)<br />
{<br />
MyType mt = (MyType)Marshal.PtrToStructure(ptr, typeof(MyType));
return mt;<br />
}<br />
Note: That code is not 100% guaranteed to compile or work, I didn't test it very much, but it should work, and the basic concept is more or less correct.
If you have any more questions, feel free to ask.
|
|
|
|
|
|
I need to allow a user to select a portion of an image for cropping. Once this selection frame has been created they also need to be able to resize and rotate it. The behavior I am looking for is similar to drawing rectangles in Word or selecting objects in Visio.
Any insight on how to do this?
Is there already a control available that I missed?
I tried taking nashcontrol's RectTracker http://www.codeproject.com/cs/miscctrl/CSharpRectTracker.asp[^] and converting it to take a Rectangle instead of a Control, which worked but not sure what to do about rotation. Matrix offers nice transforms for rotating the region but once rotated how do I find out if the mouse is at one of the "resize grips" to change the cursor? There must be an easy way lots of apps do this..
Thanks
|
|
|
|
|
Hi all,
I have a datagrid with a checkbox item in the header and i'm implementing a "select all" function which when select would select all other checkboxes on the form...
i am implementing the selectall function in javascript as such
function selectAll(obj)
{
if (obj.checked==true)
{
for(i=0; i<(document.forms[0].elements.length); i++)
{
...
all is working fine but i would like to change the text of the obj checkbox but i cant...
i tried obj.text = 'removeAll' but that doesnt work
any ideas?
|
|
|
|
|
Please don't double post.
---
b { font-weight: normal; }
|
|
|
|
|
Hello,
How can I prevent my control to be serialized in design-mode?
Thank you in advance.
Goran
|
|
|
|
|
|
So, when I move the control in design-mode I would like the position settings not to be updated.
|
|
|
|
|
You're serializing a control? I didnt even think that you could serialize a control.
|
|
|
|
|
Actually, I would like to check in the constructor of my control where I am - in designer or in runtime. How can i do that?
|
|
|
|
|
Do you mean that you want to check to see if youre running in debug or release mode?
|
|
|
|
|
No. The control can be placed to the form in design mode in MSVS.NET IDE - design-mode or the application can be run - run-mode.
|
|
|
|
|
Try this:
if (DesignMode)
{
}
|
|
|
|
|
Ummm....What???
If you move the control, you're changing the position! When you move the control in the designer, the only property of the control being changed is Location. So how do you want to both move and not move the control? What are you really trying to do with this?
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Actually, I would like to check in the constructor of my control where I am - in design-mode in MSVS.NET IDE or in runtime, which means my application is started. How can I check that?
|
|
|
|
|
Just check the DesignMode property. It'll be true if you're in DesignMode and false if in Runtime or other.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hello,
I have a simple questions: How can I check inside the control where I am - in design mode or run-time mode?
Thank you in advance.
Goran
|
|
|
|
|
Well if you want to know what the parent of the control is just use the Parent property of the control. If you want to know where the mouse is, use the MousePosition property of the control.
|
|
|
|
|
Actually, I would like to check in the constructor of my control where I am - in designer or in runtime. How can i do that?
|
|
|
|
|
Say what? None of your code is executed during design time.
|
|
|
|
|
|
Actually, that's not true. When you drop an instance of your control on a form, the code IS executed.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
If your talking about your controls position on the form, AFAIK, you can't do that in the constructor. I don't think your control has it's Location property set until after the constructor returns.
I could be wrong though...
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Every conrol has a property called DesignMode
you can check
if (DesignMode)
{
}
else
{
}
-
Drew
|
|
|
|
|
im a total newbie to c#, visual studio .net 2003 and even Access database 2000.. im doing my forms using visual studio and my data are all stored in access db..
i've some vague concepts abt how to design my app.. im not sure it'll work as i do not have enough knowledge yet, and i doubt i'll have enough time to research and come out with a solution on my own..
and so im here now, need some guidance, hopefully it's explained very simply to me as i really have some difficulty understanding those acronyms/terms for an app...
im doing this> a user keys in an ID no. in a textbox and then hits the enter key/Go button in a main form in visual studio, and the app will retrieve selected data (that ID no. user's data) out of Access tables and display that data in another form within Visual studio..
i hope to use wizards as far as possible due to simplicity and time constrain.. i understand that i cant get the wizards to meet all my criterias/expectations/aims so if need be, im ok to not use a wizard and build from scratch.. but that's where i get seriously stuck, i donno how to use visual studio TOGETHER with Access db.. i already read tutorials and help books but they are meant to teach a user to create forms and queries all WITHIN Access itself.. i wont be doing any forms within Access; im only doing forms in Visual studio and keeping data in Access.. i read microsoft access help but it's not basic enough for me to understand.. i need a simpler or more idiot teaching method but i cant find it online.. any recommendations? to be emphasized again> im really short of time.. so i cant read up too long either.. i cant understand as quickly without truly doing and exploring the work process... so in a dilemma now, whether to read and learn or doing by trial and error..
some things i have to do:
1) how can i retrieve selected data (that is, retrieving just the data of user with that particular ID no.) from access db to display in my form?
2) i've a form for employer to fill in.. this is to create files for customers who do not have a file stored in the db yet.. how can i save/store all the data that has been filled in into access db and at the same time ensure that the data is stored under a particular ID no.? (among the info filled in, one of the fields entered is ID no. so i want to keep all these data under THIS ID no. that's entered in the form)
3) does anyone know anything about nevron chart, .net charting or microsoft chart control 6.0? i would like to use the data (date and time of transaction) from db and display these data in a graphical manner, that is, to display data from db in a bar chart which is within visual studio.. which chart software can be supported/can be run along with access db and visual studio .net... which chart software is more appropriate for my needs?
please help me! i've tried on my own already but i cant do... i really appreciate your understanding!
thank you very much!
|
|
|
|