|
Actually what I needed is a bit more complicated. I first used MeasureString to create a rectangle for the text. Then I want to rotate that rectangle and find the largest "non-rotate" rectangle that rectangle will fit in. I don't care about the location of the largest rectangle, just the width and height. I did figure this out with a bunch of math. here is the routine if anyone is interested.
Note that the rect that is passed into this routine is the rect created using MeasureString.
private RectangleF FindBoundingRect(RectangleF rect, float angle)
{
// Do a quick short cut here if the text is not really rotated
// but is instead horizontal or rotated.
if (angle == 0 || angle == 180 || angle == 360)
{
return new RectangleF(0, 0, rect.Width, rect.Height);
}
else if (angle == 90 || angle == 270)
{
return new RectangleF(0, 0, rect.Height, rect.Width);
}
else if (angle > 360)
{
// Don't handle this case for now.
return new RectangleF(0, 0, 0, 0);
}
// First transform the angle to radians.
float radianAngle = angle * (float)(Math.PI / 180.0F);
float radian90Degrees = 90 * (float)(Math.PI / 180.0F);
// First find the rotated point 1. This is the bottom right point. The radius
// for this point is the width.
float x = (float)(rect.Width * Math.Cos(radianAngle));
float y = (float)(rect.Width * Math.Sin(radianAngle));
PointF p1 = new PointF(x, y);
// Now find point 3 which is also easy. This is the top left point. The
// radius for this point is the height.
x = (float)(rect.Height * Math.Cos(radianAngle + radian90Degrees));
y = (float)(rect.Height * Math.Sin(radianAngle + radian90Degrees));
PointF p3 = new PointF(x, y);
// Point 2, the top right point, is a bit trickier. First find the angle
// from the bottom left up to the top left when the rect is not rotated.
float radianAngleP2 = (float) Math.Atan(rect.Height / rect.Width);
// Now find the length of the diagonal line between these two points.
float radiusP2 = (float) Math.Sqrt((rect.Height * rect.Height) + (rect.Width * rect.Width));
// Now we can find the x,y of point 2 rotated by the specified angle.
x = (float)(radiusP2 * Math.Cos(radianAngle + radianAngleP2));
y = (float)(radiusP2 * Math.Sin(radianAngle + radianAngleP2));
PointF p2 = new PointF(x, y);
// Now based on the original angle we can figure out the width and height
// of our returned rectagle.
float width = 0;
float height = 0;
if (angle < 90)
{
width = p1.X + Math.Abs(p3.X);
height = p2.Y;
}
else if (angle < 180)
{
width = Math.Abs(p2.X);
height = p1.Y + Math.Abs(p3.Y);
}
else if (angle < 270)
{
width = Math.Abs(p1.X) + p3.X;
height = Math.Abs(p2.Y);
}
else
{
width = p2.X;
height = Math.Abs(p1.Y) + p3.Y;
}
return new RectangleF(0, 0, width, height);
}
|
|
|
|
|
Please could you post a full snippet of how to do rotated text as you describe, as that would be useful to know, from my point of view. In olrder apps I have been using LOGFONT stuff and API calls.
Nursey
|
|
|
|
|
To rotate the text use the power of the Graphics class in .net. It has a RotateTransform() method. Here is one link that talks about how to use it. I'm sure you can find other tutorials on the web also.
http://www.c-sharpcorner.com/Code/2004/April/Transformations06.asp
|
|
|
|
|
Hi everybody,,
Very curious to know...
In MFC there were __FILE__ and __LINE__ macro's, which were very helpfull for trace operations.
Are there any equivalents in C# .NET ??
Please let me know.
Thanx in advance.
Saleem
|
|
|
|
|
There are no equivalent directives in C# or VB.NET.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Hi All,
I feel that what I'm about to ask might be a really silly question, but I'd appreciate any help.
I want to create an application that exposes Functions and possibly Events that can be accessed through CreateObject() in VBScript (simular to the way you can control Office applications).
Where do I begin? Does the application have to be registered on the host machine in someway and if this is the case, is it simply a case of making a Function public in order to expose it?
Rich
|
|
|
|
|
VBScript will only work with late bound COM objects.
You can begin by implementing the COM-based interfaces in your C# code.
First, mark you assembly so that it is visible to COM.
<br />
[COMVisible(true)]<br />
public class myClass
Next tell the compiler to expose the necessary interface that VBScript understands.
<br />
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
Finally tell the compiler that you want a COM-based entity to see you.
In the project properties mark Register for COM Interop = true;
A word of warning: It is not that easy to use overloaded methods in VBScript so you should avoid these as much as possible.
This signature left intentionally blank
|
|
|
|
|
Thanks for the reply RC!
By what name would I then call the Application from VBScript. How does the enviroment know that Excel is an Excel.Application?
Rich
|
|
|
|
|
You would create and access methods within your script like any other COM component. So if your class was csExcelViewer you would do a
CreateInstance(csExcelViewer) and then do your method calls.
This signature left intentionally blank
|
|
|
|
|
C# is not a scripting language, thus you can't create objects of your C# code in a VBScript. You can create late bound objects in C#, is that what you are looking to do, that is what happens in VBScript when you call CreateObject.
- Nick Parker My Blog | My Articles
|
|
|
|
|
Thanks for the comment Nick. But, I was actually wanting to create an application in C# that could be called from VBScript using CreateObject.
Rich
|
|
|
|
|
|
In addition to what Nick and the others say, you could also use the wizard I've created for exactly this task. Take a look here:
A COM class wizard for VS.NET 2003[^].
Regards,
mav
|
|
|
|
|
Has anybody found a way to implement a global message trap, so that no matte what window/control has focus, and no matte where the mouse is pointing, you can listen to the mouse wheel roll messages?
I have seen mouse driver settings that say send messsages to the window being pointed at, rather than the window with focus. I want to develop several controls that respond to mouse wheel events without having focus, and I dont want to set focus to them just so that I can "hear" the mouse wheel messages either.
Any input, pardon the pun, would be great.
Nursey
|
|
|
|
|
It's exactly the same as a keyboard hook, except it looks at mouse messages. Create an implementation of IMessageFilter and call Application.AddMessageFilter[^] to start it. All you have to do is add the code to watch for the mouse wheel message, documented here[^]. Look through the Windows C++ header files (*.h) for MW_MOUSEWHEEL to find what the constant value is for the message.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Please note that what Dave said is only for the message pump for your application. You could also set Control.Capture to true to capture mouse events that you could handle either in WndProc or even the events that are exposed on the Control on which you set its Capture property to true .
You could also implement a system hook, but this is very dangerous and not something to be taken lightly - you're hooking every group of messages for the entire platform and your code has to be clean, efficient, and must be unloaded and gracefully fail should any problems occur. There are several articles here on CodeProject about that. Search for SetWindowsHook to find articles on this site.
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
Thank you both for your advice, that was just what I needed to hear.
I've done quite a bit with subclassing in the past, but as its been a while, and as I would like to know if there is a more ".NET" like way in which this should be done, then it was well worth asking.
If I hook into the application message pump, then presumably, as controls run "in process" I will be able to hook into every Windows Forms App that utilises my controls?
Many thanks once again.
Nursey
|
|
|
|
|
cnurse wrote:
If I hook into the application message pump, then presumably, as controls run "in process" I will be able to hook into every Windows Forms App that utilises my controls?
Most likely, yes. It greatly depends on the container application, though. I don't forsee you having any
The ".NET" way of things has nothing to do with Windows Forms or anything else, actually. .NET is Microsoft's brand of the CLI, or Common Language Infrastructure. Windows Forms is part of the BCL and actually encapsulates the Common Controls (like Edit, Rich Edit, Static, etc.) and many, many other Windows APIs and other Win32 APIs. Handling messages in this way is nothing different from what Microsoft is doing. That's why the Control class defines the WndProc method (among other Windows Platform-related members) - not just so you can use it but so that they can use it to encapsulates the Common Controls.
Software Design Engineer
Developer Division Sustained Engineering, Microsoft
My Articles
|
|
|
|
|
well i want to open my window in full screen mode. i mean only client area should be displayed and also i want to change the resolution of computer through code .. like when my application runs it change the computer resolution to my desired resolution is it possible in c#.
what comes in my mind as a biggner programer .. i set size to 810 x 610 and set location manually to -10,-10 but i don't think it is a good and proper solution so if there is any other way to do this please tell me.
In short words i want that my application covers the whole screen area no metter what resolution is been used
|
|
|
|
|
How about setting the form border to none and size to maximize?
Gary Thom
DYNAMIC LINKING ERROR: Your mistake is now everywhere.
|
|
|
|
|
Here is what I do in OnLoad event of the form:
FormBorderStyle = FormBorderStyle.None;
TopMost = true;
Rectangle rect = Screen.PrimaryScreen.Bounds;
Location = new Point(0, 0);
Size = new Size(rect.Width, rect.Height);
and it works perfectly for me.
|
|
|
|
|
Umair Ahmad khan wrote:
like when my application runs it change the computer resolution to my desired resolution
Don't. Your user might not have enough privileges to accomplish this operation. Besides, it is considered a bad practice to programaticaly change user's settings.
My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.
|
|
|
|
|
Hello,
I have problem with filling the data table. The error is in the code below.
[code]
private void frmStudent_Load(object sender, System.EventArgs e)
{
//Setup the connection string, data adapter, and commanbuilder.
//Fill the data table
OleDbConnection m_cnADONetConnection = new OleDbConnection();
OleDbDataAdapter m_daDataAdapter = new OleDbDataAdapter();
DataTable m_dtStudents = new DataTable();
m_cnADONetConnection.ConnectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\IBS Library System\LibrarySystem.mdb;Persist Security Info=False";
m_daDataAdapter = new OleDbDataAdapter("Select * student",m_cnADONetConnection);
OleDbCommandBuilder m_cbCommandBuilder = new OleDbCommandBuilder(m_daDataAdapter);
m_daDataAdapter.Fill(m_dtStudents);//Error - An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in system.data.dll
m_cnADONetConnection.Open();
}
[/code]
I would be happy if anyone knows how to solve this problem.
Many thanks in advance
Steve
|
|
|
|
|
Hi Steve,
My first suggestion would be that you check the syntax of your SQL statement.
"Select * student"
I don't use Access very much, but in SQL Server you can't miss out 'FROM'.
Rich
|
|
|
|
|
Thanks for your reply. I feel stupid for missing the from keyword of.
Thanks
|
|
|
|