|
Hey all,
I'm pretty new to .NET, and want to know how to use someone else's managed C++ code from my C# app. The C++ code is already compiled to DLLs, but doesn't appear to have been put in a namespace.
All I want is to create an object of the class, and call a few void methods
Any help is much appreciated, G
|
|
|
|
|
Just reference it like you would if you were calling your c# code in a dll. Every .NET language compiles down to the same basic construct which is IL.
------------------
I'm naked under my clothes...
|
|
|
|
|
Before I go and write my own converter - is there one available in .NET Framework? Is there a function to which I can give 0.003 inch and it converts it to milimeters (I don't need the code for this function - I need to know if in the .NET Framework there exists one already)?
Tomaz
|
|
|
|
|
No. The System.Windows.Forms.SystemInformation encapsulates some of the GetDeviceCaps functionality, but not all - including things you'd need like LOGPIXELSX and LOGPIXELSY . It has some other useful things, though, if you want to check it out anyway.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, I was wondering if anyone had tried printing from a webservice without the use of forms. Im trying to create a webservice that when called from a remote browser, will print to the computers default printer. I have found many resources for printing but they all use forms. Any help/guidance is much apprieciated.
|
|
|
|
|
Is what you're printing documents that are send to the web service, or are you printing data passed to the web service? In the former case, you must invoke the application associated with that object. In many cases, the major programs support a "/print" switch or "print" verb. You could do something like the following:
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "path to document";
info.UseShellExecute = true;
info.Verb = "print";
Process.Start(info); If you are printing data, you need a graphics handle in which to print. The .NET base class libraries allow you to instantiate a new PrintDocument and to attach handlers to its events like PrintPage , which pass a Graphics object in the PrintPageEventArgs . You can see the PrintDocument class documentation for an example. It is invoked from a Button on a form's Click event handler, but common sense would tell you that you don't need to invoke this from a control. Just instantiate the PrintDocument , attach your handlers, and do the painting on the PrintPageEventArgs.Graphics .
Note, it is likely that the username under which ASP.NET runs will require a Windows station, i.e. a Windows desktop session associated with it. Make sure that account under which ASP.NET runs ("ASPNET" by default) has the "Log in locally" privilege ("ASPNET" should by default).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks for your reply. I am trying to print data that is passed into the webservice. I belive I have set up the handler correctly but its still not working. The code works in a regular C# project just fine but not in my webservice. When I browse to the webservice I just get an error "This page cannot be displayed HTTP 500 - Internal server error". It seems as if PrintPageEventArgs object that is passed into the print handler needs some setting up first, but i'm not sure how to do this. Here is my code if anyone cares to point and laugh. Again thanks for any help.
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
}
PrintDocument pd = new PrintDocument();
[WebMethod]
public string printIt()
{
pd.PrinterSettings.PrinterName = @"\\printers\printer / HP";
pd.PrintPage += new PrintPageEventHandler(this.printItHandler);
PrintPageEventArgs ea;
if (pd.PrinterSettings.IsValid)
{
pd.Print();
return "printing worked";
}
return "Printing failed";
}
private void printItHandler(Object sender ,
PrintPageEventArgs e)
{
String textToPrint = "Printing is fun!!";
Font printFont = new Font("Courier New", 12);
int leftMargin = e.MarginBounds.Left;
int topMargin = e.MarginBounds.Top;
e.Graphics.DrawString(textToPrint, printFont,
Brushes.Black, leftMargin, topMargin);
}
}
|
|
|
|
|
HTTP status code 500 could be just about anything. That's like just saying, "There's an error". If this is an ASP.NET application, check the trace.axd "file" from your web application's root if logging is enabled from your web application's Web.config file. This should give more information.
I'm not even totally sure if this will work, although I'm fairly positive this will not work if the user doesn't have local login privileges since a Windows station (i.e., support for window and graphic handles) would have to be available (so far as docs read).
If you need more information about how to set up printing, then read the .NET Framework SDK documentation for the related classes, such as PrintDocument , PrinterSettings , etc. That's why it's there.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath,
Thanks for the reply. I'll definatly give the trace.axd a look. As far as login privaledges go, Im testing on the same machine as I am developing on so that should'nt be an issue. Also, everything I have so far IS from the SDK documentation and has not helped. If I wanted to use forms with button event listeners I am sure that info would be of great use. Like I said, this code works fine in a standard C# project. Thanks again.
|
|
|
|
|
You don't seem to get it. The ASP.NET worker process (aspnet_wp.exe) runs as a particular user, just as IIS runs as a particular user - and it isn't you unless you know how to properly edit the machine.config file (and judging by your comments, I doubt that). In IIS6 (on Windows Server 2003), the application pool runs as a particular user. Again, this isn't you. By default, ASP.NET 1.0 and 1.1 on IIS5 and 5.1 run as the ASPNET local account. Application pools in IIS6 run as NetworkService (NT AUTHORITY\NETWORK SERVICE) by default. These accounts must be granted privileges. It has nothing to do with you, even if you enable impersonation (which executes the thread - not process - under your credentials).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Your right. I dont get it. If I got it I probably would'nt be posting here! But thanks for you're help. I will look at that and see if I can figure it out. Thanks
|
|
|
|
|
No, what you don't seem to get is what I've explained three times in this thread: ASP.NET runs under a particular user's credentials - not yours. Credentials are a username and password. By default, the ASP.NET worker process runs under the local "ASPNET" account. This may not have all the necessary security privileges. You do, but ASP.NET isn't running as you (nor should it - it should be a dedicated, mostly unpriviledged account). The reason this works for you in Windows Forms and not for the ASP.NET application is because your account has a Windows station associated with it - hence all the windows and graphic UI you see. ASP.NET doesn't, unless you set your machine up correctly to do so. This has absolutely nothing to do with C# or .NET.
There are also examples out there. I simplied googled for "ASP.NET PrintDocument" and found several.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi,
I have a problem in GDI+, I put a code snippet (draws a rectangle with a text inside) in the onpaint event handler of my simple form but when I resize the form, the handler redraws my rectangle? Is there a way to determine if there is a rectangle or some other thing on the form's drawing area to not to redraw the rectangle? How can I solve this problem? (my code is below...)
Thanks in advance...
Cem Louis
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace GDI_Test01
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.form1_Paint);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void form1_Paint(object sender, PaintEventArgs e)
{
// Obtain the Graphics object
Graphics g = this.CreateGraphics();
int rect_width = 110; int rect_height = 20;
Rectangle rect = new Rectangle((this.Width-rect_width)/2,20,rect_width,rect_height);
FontFamily verdanaFamily = new FontFamily("Verdana");
Font verdanaFont = new Font(verdanaFamily, 12, FontStyle.Bold);
StringFormat strFormat1 = new StringFormat();
strFormat1.Alignment = StringAlignment.Center;
strFormat1.LineAlignment = StringAlignment.Center;
strFormat1.Trimming = StringTrimming.Character;
// Draw text using DrawString
g.DrawRectangle(new Pen(Color.Black), rect);
g.DrawString("GDI+", verdanaFont, new SolidBrush(Color.Red), rect, strFormat1);
verdanaFont.Dispose();
g.Dispose();
}
}
}
|
|
|
|
|
A couple of things.
First, the problem is not that the rectangle is being drawn again when the form is resized, it's that the old one is not being erased.
To solve this use the Graphics object passed into the paint event handler (e.Graphics) instead of creating a Graphics object.
Next, I think you actually do want the rectangle redrawn when your form is resized, so you'll need to add the line ResizeRedraw = true; to your form's constructor. You can see for yourself the difference this makes.
One last thing, since you're doing this painting in the form itself, you can override the OnPaint method rather than handling the paint event.
Charlie
if(!curlies){ return; }
|
|
|
|
|
Hi Charlie,
Thank you for your reply, I didn't know the ResizeRedraw property before.
Regards,
Cem Louis
|
|
|
|
|
Hi.
Can someone show me how to fill 50% of the below drawing with a solid color.
From buttom to top.
Thanks
Regards r9
The drawing:
private System.Drawing.Graphics graphicsObj = null;
private System.Drawing.Pen penBlackWidth1 = new System.Drawing.Pen(System.Drawing.Color.Black,1);
private Point[] curvePoints = null;
private SolidBrush lightGrayBrush = new SolidBrush(Color.LightGray);
private Point point1;
private Point point2;
private Point point3;
private Point point4;
private SolidBrush blackBrush = new SolidBrush(Color.Black);
...................................................
try
{
graphicsObj = e.Graphics;
graphicsObj.DrawLine(penBlackWidth1, 45, 20, 175, 20);
graphicsObj.DrawLine(penBlackWidth1, 45, 20, 45, 120);
graphicsObj.DrawLine(penBlackWidth1, 175, 20, 175, 120);
graphicsObj.DrawLine(penBlackWidth1, 45,120,90,160);
graphicsObj.DrawLine(penBlackWidth1, 175, 120, 135, 160);
graphicsObj.DrawLine(penBlackWidth1, 90, 160, 135, 160);
graphicsObj.DrawLine(penBlackWidth1, 465, 335, 490, 335);
graphicsObj.DrawLine(penBlackWidth1, 580, 335, 605, 335);
graphicsObj.DrawLine(penBlackWidth1, 465, 335, 490, 335);
graphicsObj.DrawLine(penBlackWidth1, 580, 335, 605, 335);
graphicsObj.DrawLine(penBlackWidth1, 473, 345, 483, 345);
graphicsObj.DrawLine(penBlackWidth1, 473, 345, 478, 335);
graphicsObj.DrawLine(penBlackWidth1, 483, 345, 478, 335);
graphicsObj.DrawLine(penBlackWidth1, 587, 345, 597, 345);
graphicsObj.DrawLine(penBlackWidth1, 587, 345, 592, 335);
graphicsObj.DrawLine(penBlackWidth1, 597, 345, 592, 335);
point1 = new Point(592,335);
point2 = new Point(597,345);
point3 = new Point(587,345);
point4 = new Point(592,335);
curvePoints = new Point[]
{
point1,point2, point3, point4
};
graphicsObj.FillPolygon(blackBrush, curvePoints, FillMode.Alternate);
point1 = new Point(478,335);
point2 = new Point(483,345);
point3 = new Point(473,345);
point4 = new Point(478,335);
curvePoints = new Point[]
{
point1,point2, point3, point4
};
graphicsObj.FillPolygon(blackBrush, curvePoints, FillMode.Alternate);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
|
|
|
|
|
Here's a though - look in your mathematics book. If you want exactly 50%, then find the algorithm given specified polygram. This is a forum for C#, not math.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi Heath Stewart
The algorithm is not the problem.
But how do I fill the figure?
(the C# GDI+ code is my problem)
Regards
C#
|
|
|
|
|
Looking at the Graphics class, the logical choices are FillPolygon , FillPath , or FillRegion . Full class documentation - it's a great thing to read.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
This code creates a control (based on the Panel) with a new property named "Colors". Setting the property yo an array of colors will display the colors.
(example:
multiColorPanel1.Colors = new Color[] { Color.Red, Color.White, Color.Blue};
end of example)
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace Project1
{
/// <summary>
/// Summary description for MultiColorPanel.
/// </summary>
public class MultiColorPanel : System.Windows.Forms.Panel
{
private Color[] colors = null;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public MultiColorPanel()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitializeComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
#endregion
public Color[] Colors
{
get
{
return colors;
}
set
{
colors = value;
this.Invalidate();
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
if ((colors != null) && (colors.Length > 0))
{
Graphics area = pevent.Graphics;
int height = this.Height / colors.Length;
for (int index = 0; index < colors.Length; index++)
{
Rectangle rect = new Rectangle(0, height * index, this.Width, this.Height);
Brush brush = new SolidBrush(colors[index]);
pevent.Graphics.FillRectangle(brush, rect);
}
}
else
{
base.OnPaintBackground (pevent);
}
}
}
}
|
|
|
|
|
How to referenc functions in other code file/unit?
|
|
|
|
|
Thats a bit vague. Are they .NET assemblies or C code or what?
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
|
|
|
|
|
Sorry, I am new in .Net.
I want to call a functin in c# code file from a form code in same application, I tried this:
//uMyLib.c
// created on 12/02/2004 at 02:16 ?
using System;
namespace TextLib
{
public class TextLib
{
string ReversText(string s)
{
string txt = "";
//for (int i=0; i <= s.Length-1; i++)
for (int i = s.Length-1; i>=0 ;i--)
{
txt += s[i].ToString();
}
return txt;
}
}
}
//==============
// project created on 12/02/2004 at 02:12 Õ
using System;
using System.Windows.Forms;
using uMyLib; // file name
namespace MyFormProject
{
class MainForm : System.Windows.Forms.Form
{
.......
..........
..........
void ButtonClick(object sender, System.EventArgs e)
{
uMyLib.MyLib S = new uMyLib.TextLib();
label.Text = S.ReversText(textBox.Text);
}
Thanks
|
|
|
|
|
klufy wrote:
using uMyLib; // file name
This should not be the name of the file, but the name of the namespace. So rewrite it as:
using TextLib;
Also, is this file in the same project or another project?
If it is in the same project then there is nothing more you should need to do. If it is in another project is the other project in the same solution as this one or not?
If the two projects are in the same solution right click the forms project (which is probably your exe file) and select "Add Reference..." from the context menu, in the dialog select the projects tab and select the project that includes TextLib.
If the project containing TextLib is not in the solution it may be best to add it to the solution (if they are both under your control) or just link to it if not.
To add the other project right-click on the root element in the Solution Explorer (the solution element) and select "Add"-->"Existing Project..." and browse for the project file.
Does this help?
EuroCPian Spring 2004 Get Together[^]
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
"Get in touch with your Inner Capitalist - I wish you much success!" -- Christopher Duncan, Lounge 9-Feb-2004
|
|
|
|
|
This is fundamental to object-oriented programming and you really should pick up a good book about the .NET Framework (which most entry-level books present information on basic OO designs and programming) from http://www.microsoft.com/mspress[^] or something.
First of all, the files DO NOT matter, so long as their compiled into the same assembly (except in the case of Java, in which only one public class can be in a file and the classname and filename must match, and they get compiled to .class files). Second, you don't reference assembly names in code - the current assembly references other assemblies. In your case, you're using two completely different namespaces, TextLib and MyFormProject . If these are in the same assembly, they typically should share a common namespace! Notice how you keep typing using System.Something ? That's not magic - you're merely telling the compiler which namespaces - which can span multiple assemblies - to look in for classes and other Types. The filename matters not.
So, you can do either of the following:
using TextLib;
TextLib tl = new TextLib();
label.Text = tl.ReversText(textBox.Text); or
TextLib.TextLib tl = new TextLib.TextLib();
label.Text = tl.ReversText(textbox.Text); Again, you really should read a book or two on .NET programming.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|