|
There are many, many ways to write code incorrectly. In this sort of situation, we won't be able to do much without seeing the code that doesn't work.
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.
|
|
|
|
|
Here is the code. Thanks alot
public IntPtr CreateFont(IntPtr hDC, string fontName, int size, FontStyle style)
{
LOGFONT lf = new PrinterApi.LOGFONT();
FONT_WEIGHT_FLAGS weight = FONT_WEIGHT_FLAGS.FW_DONTCARE;
if (Convert.ToBoolean(style & FontStyle.Bold)) {
weight |= FONT_WEIGHT_FLAGS.FW_BOLD;
}
if (Convert.ToBoolean(style & FontStyle.Regular)) {
weight |= FONT_WEIGHT_FLAGS.FW_REGULAR;
}
lf.lfHeight = (-1) * GetFontSize(hDC, size);
lf.lfWidth = 0;
lf.lfEscapement = 0;
lf.lfOrientation = 0;
lf.lfWeight = weight;
lf.lfItalic = Convert.ToByte(style & FontStyle.Italic);
lf.lfUnderline = Convert.ToByte(style & FontStyle.Underline);
lf.lfStrikeout = Convert.ToByte(style & FontStyle.Strikeout);
lf.lfOutPrecision = CHARACTER_PRECISION.OUT_TT_ONLY_PRECIS;
lf.lfClipPrecision = CLIP_PRECISION.CLIP_DEFAULT_PRECIS;
lf.lfPitchAndFamily = (byte)FONT_PITCH.DEFAULT_PITCH | (byte)FONT_FAMILY.FF_DONTCARE;
lf.lfFaceName = new byte[LF_FACESIZE];
byte[] temp = Encoding.ASCII.GetBytes(fontName);
for (int i = 0; i < lf.lfFaceName.Length; i++) {
if (i >= temp.Length) {
lf.lfFaceName[i] = 0;
} else {
lf.lfFaceName[i] = temp[i];
}
}
return CreateFontIndirect(ref lf);
}
int GetFontSize(IntPtr hDC, int size)
{
return size / 72 * GetDeviceCaps(hDC, LOGPIXELSY);
}
public void Method()
{
string Text = "Some Text";
IntPtr hDC = CreateDC(null, @"Network Imagistics im3510 PCL5e", null, 0);
DOCINFO doc = new PrinterApi.DOCINFO();
doc.cbSize = Marshal.SizeOf(typeof(DOCINFO));
doc.lpszDocName = "Test Win32Printer";
doc.lpszOutput = null;
if (StartDoc(hDC, doc)) {
StartPage(hDC);
try {
RECT rectStruct = new PrinterApi.RECT();
rectStruct.Left = 0;
rectStruct.Top = 0;
rectStruct.Right = 600;
rectStruct.Bottom = 600;
IntPtr font = CreateFont(hDC, "Times New Roman", 20, 0);
IntPtr oldFont = SelectObject(hDC, font);
DrawText(hDC, Text, Text.Length, ref rectStruct, DRAWTEXT_FORMAT_FLAGS.DT_NOCLIP);
SelectObject(hDC, oldFont);
DeleteObject(font);
EndPage(hDC);
EndDoc(hDC);
} catch (Exception e) {
AbortDoc(hDC);
throw e;
} finally {
}
}
DeleteDC(hDC);
}
|
|
|
|
|
Well, I haven't done Win32 printing for a while, but here are a few pointers/questions.
* Why use Win32 printing from C#, when the .NET Framework provides an easier method?
* When using Win32 routines, you need to check return codes, and call GetLastError() -- especially when things aren't working as you expect. It is quite likely that one of the two will give you a strong hint.
* Check basic things like - Is the text color white? - Am I really putting the text where I thought I was? - Can my printer even print in the area that I'm placing the text, or is it cutting that off? - Am I getting back a valid font?
* You are simultaneously dealing with Interop issues and Win32 issues. Unless you understand them both well, the confusion is going to be much greater than if you were dealing with only on at a time.
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.
|
|
|
|
|
The reason that I am trying to use Win32 to print instead of the .Net framework is because the project I am working on is a mass printing system and the .Net framework is too slow to keep up. How do I change the text color? Thanks
|
|
|
|
|
Do you know of a good resource on printing with Win32 Api? Everything that I have found has been very vague and not as detailed as I would like. Thanks alot. I really appreciate your help.
|
|
|
|
|
I haven't personally found (or really needed) a good printing resource for Win32. However, my impression is that it is primarily generic Win32 GDI calls, with a different dpi and size. So, a reasonable way to test your code would be to separate the code that does actual drawing into a separate function, then send that function any DC handle. When trying it out, you can draw straight to the screen. Once that works, you can draw to the printer DC.
SetTextColor() is what my memory tells me about changing the text color.
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.
|
|
|
|
|
Is there any way to add the checkbox control for each subitem in the ListView? thanks in advance for any help.
|
|
|
|
|
You'd have to owner draw the list. Search CodeProject for examples. This will require overriding WndProc and handling notification messages, so experience with Win32 programming will be helpful. There are also third-party controls that offer this behavior out of the box.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I've searched the web, and was not able to find any example code. The only one example code in the CodeProject website was way too complicated for my need.
Are you suggesting inherit the ListView & add the checkbox control. I did that, but not sure how to display the control when a new item is added for a subitem (for example, I need a checkbox control display for subitem 2 & 6). I would think it would be the same as adding a image to the specific column, and when click event occurs, switch to another image depending on if the checkbox is checked or not.
thanks in advance.
|
|
|
|
|
You can't simply add the CheckBox control to a sub-item. A sub-item is not a valid windows control and can't be parent to another control. The solution is complicated. If you can't do it, then you should get a third-party control like those from http://www.devexpress.com[^] or http://www.infragistics.com[^].
The .NET controls in Windows Forms encapsulate the Windows Common Controls, which are native controls for Win32. If the functionality you need is not exposed from the .NET controls, you have to go back to the basics of Win32 control programming and handle notification messages and send messages, use Windows styles, and more. That's just the way it is.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
katetsai wrote:
Are you suggesting inherit the ListView & add the checkbox control.
No, he is talking more about using a Panel, or creating a UserControl. Then, do all the drawing and mouse checking for yourself by handling the painting routines, and dealing with MouseDown, MouseUp, MouseMove, etc.
There are many ways to deal with this, it would be an immense help to you if you understood the basics of creating a custom control. So, have a look here: http://www.codeproject.com/cs/miscctrl/cutebutton.asp[^]
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.
|
|
|
|
|
Actually, I'm talking about extending the ListView , overriding WndProc , P/Invoking a few native APIs like SendMessage , declaring necessary consts and structs, and handling notification messages and sending windows messages as necessary like you would in Win32 programming. I've done it to provide sorted column background colors and sorting arrows on column headers. There are several articles on that here on CodeProject as well (though if you have decent Win32 programming experience it's not hard to figure out since it's what you had to do to extend anything anyway).
For instance, you could handle the NM_CUSTOMDRAW to paint to shift the text and draw the check box (for which you can use ControlPaint.DrawCheckBox if you create a Graphics object from the necessary handles). It's complicated to explain and is content meant for an article. Fortunately, there is articles on this here on CodeProject and elsewhere on the web. To the original poster, you just have to search for the right terms, like ListView SubItem CheckBox C#[^], which came up with several results (including some here on CodeProject).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi, All!
I am using PropertyGrid to edit properties of type Font, Size (e.g. standard types).
These properties are expandable in propertygrid and I need to make names of sub-properties (Font.Size, Font.Name, Size.Width etc) locale-dependent.
There are examples how to localize your own-made classes, by overriding GetProperties method (go to http://www.codeproject.com/cs/miscctrl/globalizedpropertygrid.asp or search for "GlobalizedObject", "Globalized Property Grid" to see details).
I successfully used this for my own classes, but in the case of standard classes this does not suit me well:
1) I cannot inherit from Font to just override GetProperties, Font is sealed class.
2) When I make my own class that will wrap Font instance, I will lose advanced editing in PropertyGrid, e.g. combobox with icons for font family name.
3) Also and I am confused with emulating original Font editing: Font has read-only properties Name, Size that can nevertheless be edited in PropertyGrid.
Could someone suggest me way to resolve this problem, so I will have localized sub-properties for properties of standard types Font, Size in PropertyGrid and editors in PropertyGrid for these properties will remain with all their bells and whistles?
Thanks in advance.
|
|
|
|
|
If you're using a custom TypeConverter or UITypeEditor , you can attribute the property that is of Type Font . This would take precedence over the TypeConverterAttribute or EditorAttribute that is declared for the class itself.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I made type converter inherited from System.Drawing.FontConverter and override GetProperties method.
But GetProperties method of my type converter is never used.
See code below.
//////////////////////////////////////////////////
// class that should be localized
...
[TypeConverter(typeof(LocalizedFontTypeConverter))]
public Font FontProp
{
get { return fontProp; }
set { fontProp = value;}
}
...
// type convertor
...
public class LocalizedFontTypeConverter: System.Drawing.FontConverter
{
public override PropertyDescriptorCollection GetProperties(System.ComponentModel.ITypeDescriptorContext context, object value, System.Attribute[] attributes)
{
return GetLocalizedProperties(base.GetProperties(context, value, attributes));
}
// GetLocalizedProperties method is omitted
}
|
|
|
|
|
Did you try putting a breakpoint on your return statement? I've done this in several apps and it works fine, depending on how the containing class is used (if you assign the containing class instance to PropertyGrid.SelectedObject for example).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Yes, of course. I put breakpoint in my GetProperties method and saw that this method was not called.
Class instance attached to PropertyGrid using
PropertyGrid.SelectedObject = ...
If you say that this should work, it is fine
I will continue trying.
|
|
|
|
|
How can i get the true icons of a file using c#.
When someone enters a whole path (which exist) in a textbox.
e.g. If it is "c:\tekst1.txt", how can i get the file icon. Here it is a plain text icon.
If you enter the path of a ms-word doc. I will get the ms-word icon.
Is there some class availible or need i to read the registry entries in the Classes_root. (when the registry refer to a ico-file this is no problem, but most programs refer to an exe-file.
Thanks
Jonathan Slenders
|
|
|
|
|
|
|
Hello,
Precondition.
1. The ListView control. have 5 columns.
2. This have 70 data.
I would like to remove data after add data on visual.
I could do it. but not visible.
After remove and add data on ListView and ListViewItem, I called Update() or BeginUpdate() & EndUpdate(). But These data didn't update.
The following is the source.
If you have some ideas, please tell me.
regards,
yu-yu
--------------------------------------------------------
lvListView.BeginUpdate();
while(i < lvListView.Items.Count)
{
if (lvListView.Items[i].SubItems[3].Text.CompareTo(sTemp[3]) == 0)
{
lvListView.Items[i].Remove();
break;
}
i ++;
}
ListViewItem items = new ListViewItem("WEB");
items.SubItems.Add(sTemp[2]);
items.SubItems.Add(sTemp[4]);
items.SubItems.Add(sTemp[3]);
lvListView.Items.Insert(lv参加者.Items.Count, items);
lvListView.EndUpdate();
|
|
|
|
|
Our scenario:
Multiple objects being hosted in a Windows services on Win2003 Server.
Up until now, we were running the server as an EXE, but since moving to a service all our errors are being returned as "Server encountered an internal error.".
We last saw this error when our exceptions were not supporting serialisation correctly.
Since changing the exception classes, the error went away and we haven't changed anything else since moving the server code into a service.
The code works 100% and the exceptions are expected, however the type and associated info about the exception is now not being preserved across the remoting boundary.
Is the behaviour of the remoting code different if it's in a service?
Cheers,
Simon
sig :: "Don't try to be like Jackie. There is only one Jackie.... Study computers instead.", Jackie Chan on career choices.
article :: animation mechanics in SVG blog:: brokenkeyboards "It'll be a cold day in Hell when I do VB.NET...", Chris Maunder
|
|
|
|
|
It shouldn't be (since a service is just an executable that's run by the SCM), but to satisfy curiousity (and hopefully help you solve the problem), have you tried checking the Exception.InnerException to see if you are getting what you expected?
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
<br />
private void btnPrevious_Click(object sender, System.EventArgs e)<br />
{<br />
txtAcctID.Clear();<br />
<br />
if (Row == 0)<br />
{<br />
MessageBox.Show("You are at the first patient on the Queue","Beginning Of Queue");<br />
Row = 0;<br />
<br />
}<br />
else<br />
{ <br />
Row = Row --;<br />
txtAcctID.Text= dsPatDia.Tables["PatDia"].Rows[lastrecord]["Acct ID"].ToString(); <-- ERROR! <br />
}<br />
<br />
if( Row == 0)<br />
{<br />
try<br />
{<br />
oleDbConnection1.Open();<br />
<br />
oleDbDataAdapter1.SelectCommand.CommandText = "SELECT Results, Tests, Symptom FROM Dianosis WHERE [Acct ID]= '"+txtAcctID.Text+"'";<br />
<br />
oleDbDataAdapter1.SelectCommand.ExecuteNonQuery();<br />
<br />
oleDbDataAdapter1.Fill(dsDiag2);<br />
<br />
Display(dsDiag2);<br />
}<br />
catch (Exception ConnError)<br />
{<br />
MessageBox.Show(ConnError.ToString());<br />
}<br />
<br />
}<br />
<br />
}<br />
<br />
private void Display(System.Data.DataSet dsDiag2)<br />
{<br />
oleDbConnection1.Close();<br />
try<br />
{<br />
System.Data.DataTable PrevDiag = dsDiag2.Tables[0];<br />
<br />
if(PrevDiag.Rows.Count != 0)<br />
{ <br />
txtResults.Text = (string) PrevDiag.Rows[0][3];<br />
txtTests.Text = (string) PrevDiag.Rows[0][4];<br />
txtSymp.Text = (string) PrevDiag.Rows[0][5];<br />
}<br />
else<br />
{<br />
MessageBox.Show("The Patient Record does not exist","Information");<br />
dsDiag2.Clear();<br />
}<br />
}<br />
catch(Exception Error10)<br />
{<br />
MessageBox.Show(Error10.ToString());<br />
}<br />
}<br />
An unhandled exception of type 'System.NullReferenceException' occurred in CLINICINFORMATIONSYSTEM.exe
Additional information: Object reference not set to an instance of an object.
how do i rectify this prblem..i want the textbox to display the id of the record before the current one.?
CODER
|
|
|
|
|
There are at least two things you want to check out:
1. You probably forgot to fill the dataset before you call the methods (or even declared it as new).
2. The table does not exist, the column does not exist, or there are no records in the table.
How did you get the lastrecord variable in Rows[lastrecord] anyway?
Edbert P.
Sydney, Australia.
|
|
|
|
|