|
thats fine as it seems to be my only option but, can point in the direction to find out how to that because i'm guessing it would invole some win32 calls?
|
|
|
|
|
The Win32 calls would only come in if you need to have the main form, its sub-form, and then the 3rd level sub-form all showing a title bar that indicates it is active. I'm not really if there is anything else to the base MDI document behavior.
To add a form as a child of another form, make a constructor to the (sub)form class which takes the parent as a parameter. Then, in that constructor, set TopLevel to false, and Parent to the parent form. It would look like this:
public MyMdiForm(System.Windows.Forms.Form parent)
{
InitializeComponents();
this.TopLevel = false;
this.Parent = parent;
}
To handle the look that needs multiple forms to display as active, check this[^]out.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Hi, I'm creating a C# windows application and I've got a problem. I need to temporarily lock a file directory, do something to a xml file in that directory, then unlock the directory. Is this possible? Can anyone help me with this? I know I need to use the Directory object somehow.
Thanks
|
|
|
|
|
You can't really lock a directory, but why not just lock the XML document? It's simple using the FileStream class like so:
using (FileStream stream = new FileStream(
path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
XmlDocument doc = new XmlDocument();
doc.Load(stream);
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Is their a way to programmatically take away permissions to a folder, then give it back? I think that is what I really need to do.
Thanks!
|
|
|
|
|
Yes there is, but no pure .NET way. Read NT Security Class for .NET[^], which of course only works on NT. Pay attention to the note toward the top of the page, too. There's a library written by Microsoft developers for NT security that you should use instead. That library will be included in .NET 2.0 in Q1 of 2005. Getting familiar with it now will help in the future if you upgrade your code base.
Still, though - what's the point? All you're doing is updating an XML document, so that's all that needs to be locked. Taking away permission and giving them back is dangerous if you don't account for every single exception. What if your code bombs and permissions are restored? It's not an effective solution.
If you need to update the XML document with a list of files in that directory and don't want that list to be modified while updating the XML document, the solution is simple: get a collection of filenames - which is a read-only collection of the files in that directory when you got the files. This can easily be achieved with the Directory.GetFiles static method, or the DirectoryInfo.GetFiles instance method. Update your XML document with those collections. That's an effective and proper solution.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi there. I'm working on a custom control where each side can have a different border thickness. I found this code while googling which allows me to modify the controls non-client area so I can draw my borders:
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
enum WM_MESSAGE
{
WM_NCCALCSIZE = 131,
WM_NCPAINT = 133,
WM_NCHITTEST = 132,
WM_NCLBUTTONDOWN = 161,
}
[StructLayout(LayoutKind.Sequential)]
struct NCCALCSIZE_PARAMS
{
public RECT rgrc0, rgrc1, rgrc2;
public IntPtr lppos;
}
[DllImport("User32.dll")]
private extern static IntPtr GetWindowDC( IntPtr hWnd );
[DllImport("User32.dll")]
private extern static int ReleaseDC( IntPtr hWnd, IntPtr hDC );
private Rectangle _rcButton = Rectangle.Empty;
protected override void WndProc(ref Message m)
{
switch (m.Msg)
{
case (int)WM_MESSAGE.WM_NCCALCSIZE :
if (m.WParam == IntPtr.Zero)
{
NCCALCSIZE_PARAMS csp;
csp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS));
csp.rgrc0.Left += 1;
csp.rgrc0.Top += 5;
csp.rgrc0.Right -= 1;
csp.rgrc0.Bottom -= 1;
Marshal.StructureToPtr( csp, m.LParam, false );
}
break;
case (int)WM_MESSAGE.WM_NCPAINT :
{
IntPtr hDC = GetWindowDC(m.HWnd);
Graphics g = Graphics.FromHdc(hDC);
g.FillRectangle(SystemBrushes.Highlight, 0, 0, this.Width, 5);
g.FillRectangle(SystemBrushes.Highlight, 0, 0, 1, this.Height);
g.FillRectangle(SystemBrushes.Highlight, this.Width-1, 0, 1, this.Height);
g.FillRectangle(SystemBrushes.Highlight, 0, this.Height-1, this.Width, 1);
ReleaseDC( m.HWnd, hDC );
break;
}
}
base.WndProc(ref m);
} This works fine until the control is resized - it's client area gets reset. Any ideas on how to fix this?
"I think I speak on behalf of everyone here when I say huh?" - Buffy
|
|
|
|
|
My guess is that the reason it works until the control is resized is that the client area was never successfully change the the size you are wanting.
I took a look at the WM_NCCALCSIZE documentation here[^]. It says that if WParam is FALSE (zero), that a RECT is passed in LParam, not an NCCALCSIZE_PARAMS structure. On the other hand, if WParam is TRUE (non-zero), then you are supposed to get an NCCALCSIZE_PARAMS in LParam.
So, it looks like the WM_NCCALCSIZE handling code is never doing anything. I suspect that if you put a breakpoint on the first line of the WM_NCCALCSIZE handling code, then stepped through it, that it would throw an exception at
csp.rgrc0.Left += 1;
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Thanks for the tip. I managed to get it to "remember" the resized non-client area by changing the offending case statememt to:
case (int)WM_MESSAGE.WM_NCCALCSIZE :
if ( m.WParam == IntPtr.Zero )
{
RECT rect = (RECT) Marshal.PtrToStructure(m.LParam, typeof(RECT));
rect.Left += 1;
rect.Top += 5;
rect.Right -= 1;
rect.Bottom -= 1;
Marshal.StructureToPtr(rect, m.LParam, false);
}
else
{
NCCALCSIZE_PARAMS csp;
csp = (NCCALCSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALCSIZE_PARAMS));
csp.rgrc0.Left += 1;
csp.rgrc0.Top += 5;
csp.rgrc0.Right -= 1;
csp.rgrc0.Bottom -= 1;
Marshal.StructureToPtr(csp, m.LParam, false);
}
Only problem was that sometimes the right and bottom border widths were out by 1. Managed to fix this by overriding OnSizeChanged:
[DllImport("User32.dll")]
public static extern int RedrawWindow(IntPtr hwnd, IntPtr lprcUpdate, IntPtr hrgnUpdate, int fuRedraw);
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
RedrawWindow(this.Handle, IntPtr.Zero, IntPtr.Zero, RDW_FRAME | RDW_UPDATENOW | RDW_INVALIDATE);
}
Works like a charm
"I think I speak on behalf of everyone here when I say huh?" - Buffy
|
|
|
|
|
Great!
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
How could I be able to catch a message that was sent to an application which I have its handle(Window handle)?
thanks for help.
|
|
|
|
|
Override WndProc .
Stability. What an interesting concept. -- Chris Maunder
|
|
|
|
|
Or if you want a more dynamic solution look up the IMessageFilter interface. Message filters can be added and removed from windows forms application at runtime.
|
|
|
|
|
Note that this is better for handling application messages, not specific messages for a control. For the latter, you should override WndProc . Using an application filter (IMessageFilter ) can slow down your code drastically if you don't optimize it by keeping in mind that all messages posted to your application will filter through it. Also, IMessageFilter only catches messages posted to your application pump (a la PostMessage ) while overriding WndProc catches messages posted or sent (a la PostMessage or SendMessage ).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I would like to do an event that catch the change the parallelportinput. I have maneged the event and i know how to read from the port. But i don´t know how to fire the event when the port varible changes. I done it whit a loop that read the port all the time wich works fine, exept that the program is looked. So i don´t have much use of it that way. I wan´t it to work like an extern button.
The class that i want to listen to return a integer. I guess i need a class that read that integer and fire the event when it changes.
Could anyone help me with this , please
Niklaz
|
|
|
|
|
There are 2 ways to handle this.
Option 1:
Start a separate thread to monitor the port. Then, when the thread discovers the change, it can fire any type of event that you would like.
To handle the thread, look up System.Threading.Thread.
To handle the event firing, read about the "delegate" and "event" keywords.
Option 2:
Use the BeginInvoke method of event firing. This causes the .NET framework to spawn a worker thread which is solely responsible for firing your event. The thread that is watching the serial port needs to do nothing else differently.
If your code already said,
this.MyEvent(param1, param2)
This change would turn it into:
foreach (MyEventHandlerType delegateType in this.MyEvent.GetInvocationList())
{
delegateType.BeginInvoke(param1, param2, null, null);
}
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Hello,
I really need a transparent RichTextBox control. Does anyone know how I can achieve this (by implementing code, or using a pre-existing control)????
Your help is greatly appreciated!
Agent 86
|
|
|
|
|
How to know if there is a child control at a certain point of a user control ? by desmond5 I have a user control, that can contain other controls. The control makes use of the MouseDown event. In the procedure (event) it should determine if user clicked on a child control or on a "free space" of the user control". So I have to determine if there is a child control at a certain point (the point is the mouse coordinates, x and y) of the user control. I cannot think how could I do that, however. Could anyone help me ?
public bool ContainsChildControlAtPoint(Point point)
{
...
}
|
|
|
|
|
Use the forms GetChildAtPoint method.
<br />
<br />
public void DoMouse(object s, MouseEventArgs e)<br />
{<br />
ContainsChildControlAtPoint(new Point(e.X, e.Y)); <br />
}<br />
<br />
public bool ContainsChildControlAtPoint(Point point) <br />
{<br />
if (this.GetChildAtPoint(point)== null)<br />
return false;<br />
else<br />
return true;<br />
}<br />
The smaller the mind the greater the conceit.
Aesop
|
|
|
|
|
You would use the System.Windows.Forms.Control's GetChildAtPoint method.
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|
Hi,
How can i load a 3D object, saved in an *.obj file format in a C# application, using OpenGL? Thank you for reading and help !
pertiu
|
|
|
|
|
From what I remember, a .obj is a Maya 3d object file. .NET obviously doesn't have any built methods to load this type of file, neither does OGL from what I remember. What you need is the documentation to the .obj format from the Maya people. Once you've got that, you can use .NET to read in the bytes of the file as vertices, faces, textures, etc.
---------------------------
He who knows that enough is enough will always have enough.
-Lao Tsu
|
|
|
|
|
So, you gave Judah a score of 1 for his response, eh? Too bad, but he's right. A Framework - like the .NET Framework, for which C# is one of many languages that target - cannot/should not provide class libraries for everything anyone could ever think of. Sorry, welcome to the world of programming.
You're either going to have to read the documentation for the .obj format - yes, read - or find a library like a COM library (which can interop well with .NET) and use that to write the binary file format.
If you don't like it, tough. That's the way development goes sometimes.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
It was about time I started to use real OOP techniques to make my life easier, unfortunatly it seems that C# does not support multiple inheritance as I understand it and one has to use Interfaces instead.
Now I dont mind what I use, as long as I get the functionality I am after, but I cannot get my head around interfaces. Does anybody know of a plain language tutorial that covers this aspect of OOP in C#
As an example I have the follow classes
public class widgetA
{
public object wa;
}
public class widgetB
{
public object wb;
}
public class widgetC
{
public object wb;
}
public class AllWidgets: widgetA,widgetB,widgetC
{
public AllWidgets(object a, object b, object c)
{
}
}
public void CodeFunction()
{
AllWidgets all = new AllWidgets("blah", "foo", "bar")
Console.Writeline("Widget A : " + all.wa);
Console.Writeline("Widget B : " + all.wb);
Console.Writeline("Widget C : " + all.wc);
}
Now this obviously doesnt work, but at least it gives an idea of what I am trying to acheive. Hopefully it makes sense.
post.mode = postmodes.signature;
SELECT everything FROM everywhere WHERE something = something_else;
> 1 Row Returned
> 42
|
|
|
|
|
Hi, I'll just post a quick answer. Browsing through the articles on CP, as well as looking for tutorials through google will get you much farther.
So, C# doesn't allow multiple inheritance of classes. But, it does allow multiple inheritance of interfaces. This combines to allow inheritance of 0 or 1 class and any number of interfaces.
When implementing multiple interfaces, you have two basic options. 1) Implement every interface member directly within your new class; or 2) Use implementation classes within your new class, and call these implementation members from the shell interface implementations within your new class.
Option 1 would look something like this:
public interface IWidgetA
{
object wa
{
get;
set;
}
}
public interface IWidgetB
{
object wb
{
get;
set;
}
}
public interface IWidgetC
{
object wc
{
get;
set;
}
}
public class AllWidgets : IWidgetA, IWidgetB, IWidgetC
{
protected object _wa;
public object wa
{
get { return _wa; }
set { _wa = value; }
}
protected object _wb;
public object wb
{
get { return _wb; }
set { _wb = value; }
}
protected object _wc;
public object wc
{
get { return _wc; }
set { _wc = value; }
}
}
Option 2 would look something like this:
public class WidgetAImpl : IWidgetA
{
protected object _wa;
public object wa
{
get { return _wa; }
set { _wa = value; }
}
}
public class WidgetBImpl : IWidgetB
{
protected object _wb;
public object wb
{
get { return _wb; }
set { _wb = value; }
}
}
public class WidgetCImpl : IWidgetC
{
protected object _wc;
public object wc
{
get { return _wc; }
set { _wc = value; }
}
}
public class AllWidgets : IWidgetA, IWidgetB, IWidgetC
{
protected WidgetAImpl;
protected WidgetBImpl;
protected WidgetCImpl;
public object wa
{
get { return this.WidgetAImpl.wa; }
set { this.WidgetAImpl.wa = value; }
}
public object wb
{
get { return this.WidgetBImpl.wb; }
set { this.WidgetBImpl.wb = value; }
}
public object wc
{
get { return this.WidgetCImpl.wc; }
set { this.WidgetCImpl.wc = value; }
}
}
Note: I didn't intend for this to be directly compilation ready.
With this tiny little sample, the second method obviously is more work than the first. However, when you've got complicated or large routines, or large sets of data that every interface implementation will treat the same way, then the second option becomes more useful than the first. As with most programming tasks, when there is more than one way to do things, you need to weigh the options and see which is best.
I hope this helps you out!
John
"You said a whole sentence with no words in it, and I understood you!" -- my wife as she cries about slowly becoming a geek.
|
|
|
|
|