|
if i convert my page width to milimetres then the above method should work fine.
Thanks for your help, I hope this sorted your problem as well.
Cheers
Kev Pearman MCP
|
|
|
|
|
The correct way is to P/Invoke GetDeviceCaps and query for the logical pixels / physical pixels using LOGPIXELSX and LOGPIXELSY . Hard-coding numbers like the other guy did will not work on every system, especially these days when more and more displays are moving to 120 DPI as opposed to the currently common 96 DPI.
I've posted code to correctly get the number of pixels per some physical unit (twips, actually, which is 1/1440 of an inch) before. See an example at http://www.codeproject.com/script/comments/forums.asp?msg=829941&forumid=1649#xx829941xx[^].
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I think it might be worth me re-stating my problem as i'm sure there will be a simpler way of solving it.
I have a string that i am trying to print on a printer, the problem is that the string needs to be aligned to the right hand margin of the page.
I can get the margin bounds, e.MarginBounds.Right , this value is in 100ths/inch i think.
I can also get the length of the string, stored in a SizeF variable
SizeF stringWidth = e.Graphics.MeasureString(myString, myFont); but the values are in pixels as opposed to physical units.
I was hoping to use the following code to print the string in the right place.
e.Graphics.DrawString(myString, MyFont, MyBrush, e.MarginBounds.right - stringWidth.x, Y);
However, this gives the wrong results as one unit is in pixels and the other is in 100ths/inch.
Is there a Method that i can use to convert the stringWidth into 100ths/Inch as this would solve the problem straight away. Or can i get the width of the string in 100ths/Inch straight away, using a different method to the one above.
Cheers
Kev Pearman MCP
|
|
|
|
|
I just told you how and my sample code can convert from pixels to twips and twips to pixels. As I said, a twip is 1/1440 of an inch, so simply divide the number of twips by 1440 to get the number of inches. Multiply the number of inches by 1440 to get the number of twips and convert to pixels.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
My apologies Heath, i should have read the example you posted properly.
However the example you posted was really buggy, I have managed to get rid of all of the bugs except for one.
The error i am getting is
The name 'logsPerPixelX' does not exist in the class or namespace 'GMB.Display'
I assume that the error is because you have not included the definition for logsPerPixelX (and also logsPerPixelY).
Kev Pearman MCP
|
|
|
|
|
The error suggest that you need to define the field, but in this case the field name should be twipsPerPixelX , which is already defined but never used. The proper code is:
public sealed class Display
{
static Display()
{
SystemEvents.DisplaySettingsChanged += new EventHandler(Update);
}
private Display() {}
[DllImport("gdi32.dll")]
private static extern IntPtr GetDeviceCaps(IntPtr hdc, IntPtr index);
[DllImport("user32.dll")]
private static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("user32.dll")]
private static extern IntPtr ReleaseDC(IntPtr hwnd, IntPtr hdc);
private static void Update(object sender, EventArgs e)
{
Setup(true);
}
private static void Setup(bool force)
{
if (initialized && !force) return;
twipsPerPixelX = twipsPerPixelY = 0;
IntPtr hdc = GetDC(IntPtr.Zero);
try
{
if (!hdc.Equals(IntPtr.Zero))
{
IntPtr value;
value = GetDeviceCaps(hdc, new IntPtr(88));
twipsPerPixelX = 1440 / value.ToInt32();
value = GetDeviceCaps(hdc, new IntPtr(90));
twipsPerPixelY = 1440 / value.ToInt32();
ReleaseDC(IntPtr.Zero, hdc);
}
}
catch {}
if (twipsPerPixelX == 0) twipsPerPixelX = 15;
if (twipsPerPixelY == 0) twipsPerPixelY = 15;
initialized = true;
}
private static bool initialized;
private static int twipsPerPixelX;
private static int twipsPerPixelY;
public static int TwipsPerPixelX
{
get
{
Setup(false);
return twipsPerPixelX;
}
}
public static int TwipsPerPixelY
{
get
{
Setup(false);
return twipsPerPixelY;
}
}
public static int FromPixelsX(int x, ScaleMode mode)
{
switch (mode)
{
case ScaleMode.Points:
return PixelsToTwipsX(x) / 20;
case ScaleMode.Inches:
return PixelsToTwipsX(x) / 1440;
default:
throw new ArgumentException("Invalid scale mode.", "mode");
}
}
public static int FromPixelsY(int y, ScaleMode mode)
{
switch (mode)
{
case ScaleMode.Points:
return PixelsToTwipsY(y) / 20;
case ScaleMode.Inches:
return PixelsToTwipsY(y) / 1440;
default:
throw new ArgumentException("Invalid scale mode.", "mode");
}
}
public static int ToPixelsX(int x, ScaleMode mode)
{
switch (mode)
{
case ScaleMode.Points:
return TwipsToPixelsX(x * 20);
case ScaleMode.Inches:
return TwipsToPixelsX(x * 1440);
default:
throw new ArgumentException("Invalid scale mode.", "mode");
}
}
public static int ToPixelsY(int y, ScaleMode mode)
{
switch (mode)
{
case ScaleMode.Points:
return TwipsToPixelsY(y * 20);
case ScaleMode.Inches:
return TwipsToPixelsY(y * 1440);
default:
throw new ArgumentException("Invalid scale mode.", "mode");
}
}
public static int PixelsToTwipsX(int x)
{
Setup(false);
return x * twipsPerPixelX;
}
public static int PixelsToTwipsY(int y)
{
Setup(false);
return y * twipsPerPixelY;
}
public static int TwipsToPixelsX(int x)
{
Setup(false);
return x / twipsPerPixelX;
}
public static int TwipsToPixelsY(int y)
{
Setup(false);
return y / twipsPerPixelY;
}
}
public enum ScaleMode
{
Points,
Inches
}
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I need tutorials or articles on "Ray Picking" in C#. Anyone of you have any ideas where i can learn from internet? I have download the example form GotDotNet but it is hard for me to understand. I try to search in google but most of the examples in C/C++.
thanks in advance.
|
|
|
|
|
How can I access to an enumeration defined in a class from an instance of another class which inherit from the first?
Example:
public class Parameter {
public enum Something {
...
}
}
public class MyParameter : Parameter {
...
}
MyParameter.Something doesn't work! I think I missed something
regards
|
|
|
|
|
Try base.Something
Hope it helps
|
|
|
|
|
Thank you but I would like to expose the enumeration to the client of the class. Maybe it is more a problem of scope.
For instance, to expose a private member of the base class, I would create a public property but I don't know how to do it for an enumeration.
|
|
|
|
|
public class A
{
protected SomeEnum e;
}
public class B : A
{
public SomeEnum E
{
get { return base.e; }
set { base.e = value; }
}
} You can't access private fields - not even in derivative classes. That's why they're declared as private.
Exposing an enumeration is like exposing any other type, be that a reference type or value type (which an enum is, along with primatives and structs).
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
|
Well, all is ok...
I am sorry but I missed that in fact my class was not inherited from the class which defined the enumeration but from an inner class of it . It was more a problem of scope than of code...
Thank you for your replies,
Best regards
|
|
|
|
|
Hello,
how can I get the URL from an open Internet Explorer? I tried it on this way:
InternetExplorerClass IE = new InternetExplorerClass();
txtGetURL.Text = IE.LocationURL;
But this returns allways an empty string
|
|
|
|
|
Your going to need to use SPY++ to get the handle of the IE's addressbar (which is a combobox). Then you will use the User32.dll SendMessage and WM_GETTEXT, which is documented here PINVOKE.net[^]
/\ |_ E X E GG
|
|
|
|
|
Hi Friends,
I need to cast a string to long. I'm reading information from an XML file. The file contains information such as record's ID, Age. These are declared as C# type long. I'm using System.XML.XMLDocument class for reading XML File. All the values returned by XMLDocument object are in string format. so i get ID ang Age as strings not Long. Now i need to cast them as long so i can utilized them safely in my program... I've searched MSDN and it says type casting across differnt domain is not allowed.
Is there any workaround to cope up with this situation ???
Thanks in advance,
Regards,
Hatim Ali.
|
|
|
|
|
Use an overload of Int64.Parse to convert your string.
|
|
|
|
|
|
Hi Guys,
How to validate a TIME?
- After selecting the data from the database SQL SERVER I need to validate the time. IF it's > 12:00 am then I will not accept that particular record no specific dates.
ex0. 2004/06/20 12:01:43 AM - this is not allowed
ex1. 2004/06/20 12:00:01 AM - this is not allowed
if I used this sql statement using the ex0 or ex1 value it gives me a "0" value
select datepart(hh,requestdate) from dtrequest where requestno='DRAFT104' group by requestdate
Anybody who can direct me to a page where I can read and give me idea or solution, or if you can help me.
Thanks
/Dabuskol
|
|
|
|
|
dabuskol wrote:
IF it's > 12:00 am then I will not accept that particular record no specific dates
Then what records are you accepting? 12AM == Midnight, 12PM == Noon
So, the result you are getting from the datepart is correct. Zero-hundred-hours == Midnight.
When dealing with dates and times I always use the 24-hour clock - it is much easier to work with.
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
|
dabuskol wrote:
12 PM..
"You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar
The Second EuroCPian Event will be in Brussels on the 4th of September
Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way!
My Blog
|
|
|
|
|
hello there,
i want to take lively images from a webcam which connects to my PC from USB.
if you help me i will be very very happy.
have nice work.....
turgaytr@hotmail.com
T.A
|
|
|
|
|
you'd better have the driver and SDK of the webcam.
then you can do it better.
|
|
|
|
|