|
Hi ....
I hope this Example will help you ...
I used 2 Files
- mycaret.cs (Class)
- caretsample.cs ( Example How to use it ) Optional
File : mycaret.cs
<br />
using System;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
using System.Drawing;<br />
<br />
public class MyCaret {<br />
[DllImport("user32.dll")]<br />
public static extern int CreateCaret(IntPtr hwnd, IntPtr hbm, int cx, int cy);<br />
[DllImport("user32.dll")]<br />
public static extern int DestroyCaret();<br />
[DllImport("user32.dll")]<br />
public static extern int SetCaretPos(int x, int y);<br />
[DllImport("user32.dll")]<br />
public static extern int ShowCaret(IntPtr hwnd);<br />
[DllImport("user32.dll")]<br />
public static extern int HideCaret(IntPtr hwnd);<br />
<br />
Control ctrl;<br />
Size size;<br />
Point pos;<br />
bool bVisible;<br />
<br />
public MyCaret(Control ctrl) {<br />
this.ctrl = ctrl;<br />
Position = Point.Empty;<br />
Size = new Size(1, ctrl.Font.Height);<br />
Control.GotFocus += new EventHandler(OnGotFocus);<br />
Control.LostFocus += new EventHandler(OnLostFocus);<br />
<br />
if (ctrl.Focused)<br />
OnGotFocus(ctrl, new EventArgs());<br />
}<br />
<br />
public Control Control {<br />
get { return ctrl; }<br />
}<br />
<br />
public Size Size {<br />
get { return size; }<br />
set { size = value; }<br />
}<br />
<br />
public Point Position {<br />
get { <br />
return pos; <br />
}<br />
set { <br />
pos = value; <br />
SetCaretPos(pos.X, pos.Y);<br />
}<br />
}<br />
<br />
public bool Visible {<br />
get {<br />
return bVisible;<br />
}<br />
set {<br />
bVisible = value;<br />
if (bVisible)<br />
ShowCaret(Control.Handle);<br />
else<br />
HideCaret(Control.Handle);<br />
}<br />
}<br />
<br />
public void Dispose() {<br />
if (ctrl.Focused)<br />
OnLostFocus(ctrl, new EventArgs());<br />
Control.GotFocus -= new EventHandler(OnGotFocus);<br />
Control.LostFocus -= new EventHandler(OnLostFocus);<br />
}<br />
<br />
private void OnGotFocus(object sender, EventArgs e) {<br />
CreateCaret(Control.Handle, IntPtr.Zero, Size.Width, Size.Height);<br />
SetCaretPos(Position.X, Position.Y);<br />
Visible = true;<br />
}<br />
<br />
private void OnLostFocus(object sender, EventArgs e) {<br />
Visible = false;<br />
DestroyCaret();<br />
}<br />
}<br />
caretsample.cs
<br />
using System;<br />
using System.Drawing;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
<br />
public class MyForm : Form {<br />
private Container components = null;<br />
MyCaret caret;<br />
string[] lines;<br />
Point cur;<br />
Point org;<br />
bool bSel;<br />
<br />
public MyForm() {<br />
InitializeComponent();<br />
<br />
caret = new MyCaret(this);<br />
lines = new string[] {<br />
"public class MyApp {",<br />
" System.Console.WriteLine(\"Hello,World\");",<br />
"}"};<br />
cur = Point.Empty;<br />
org = Point.Empty;<br />
bSel = false;<br />
}<br />
<br />
protected override void Dispose( bool disposing ) {<br />
if( disposing ) {<br />
if (components != null) {<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
protected override void OnPaint(PaintEventArgs e) {<br />
base.OnPaint (e);<br />
StringFormat fmt = StringFormat.GenericTypographic;<br />
fmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;<br />
for (int i = 0; i < lines.Length; ++i)<br />
e.Graphics.DrawString(lines[i], Font, new SolidBrush(this.ForeColor), <br />
0, Font.Height * i, StringFormat.GenericTypographic);<br />
if (bSel) {<br />
DrawSelected(e.Graphics);<br />
}<br />
}<br />
<br />
protected override void OnKeyDown(KeyEventArgs e) {<br />
if (e.Shift) {<br />
if (!bSel) {<br />
org = cur;<br />
bSel = true;<br />
}<br />
}<br />
else {<br />
org = cur;<br />
bSel = false;<br />
}<br />
<br />
switch (e.KeyCode) {<br />
case Keys.Left:<br />
if (cur.X > 0) <br />
--cur.X;<br />
else if (cur.Y > 0) {<br />
--cur.Y;<br />
cur.X = lines[cur.Y].Length;<br />
}<br />
break;<br />
case Keys.Right:<br />
if (cur.X < lines[cur.Y].Length)<br />
++cur.X;<br />
else if (cur.Y < lines.Length - 1) {<br />
++cur.Y;<br />
cur.X = 0;<br />
}<br />
break;<br />
case Keys.Up:<br />
if (cur.Y > 0) {<br />
--cur.Y;<br />
if (cur.X > lines[cur.Y].Length)<br />
cur.X = lines[cur.Y].Length;<br />
}<br />
break;<br />
case Keys.Down:<br />
if (cur.Y < lines.Length - 1) {<br />
++cur.Y;<br />
if (cur.X > lines[cur.Y].Length)<br />
cur.X = lines[cur.Y].Length;<br />
}<br />
break;<br />
}<br />
MoveCaret();<br />
Invalidate();<br />
}<br />
<br />
void DrawSelected(Graphics g) {<br />
Point bgn;<br />
Point end;<br />
<br />
if (org.Y < cur.Y || org.Y == cur.Y && org.X < cur.X) {<br />
bgn = org;<br />
end = cur;<br />
}<br />
else {<br />
bgn = cur;<br />
end = org;<br />
}<br />
<br />
int p;<br />
int q;<br />
PointF lt = PointF.Empty;<br />
PointF rb = PointF.Empty;<br />
for (int i = bgn.Y; i <= end.Y; ++i) {<br />
lt.Y = i * Font.Height;<br />
rb.Y = (i + 1) * Font.Height;<br />
<br />
if (i == bgn.Y) { <br />
lt.X = CalcWidth(g, lines[i].Substring(0, bgn.X));<br />
p = bgn.X;<br />
}<br />
else { <br />
lt.X = 0; <br />
p = 0;<br />
}<br />
if (i == end.Y) {<br />
rb.X = CalcWidth(g, lines[i].Substring(0, end.X));<br />
q = end.X;<br />
}<br />
else {<br />
rb.X = CalcWidth(g, lines[i]);<br />
q = lines[i].Length;<br />
}<br />
g.FillRectangle(Brushes.DarkBlue, lt.X, lt.Y, rb.X - lt.X, rb.Y - lt.Y);<br />
g.DrawString(lines[i].Substring(p, q - p), Font, new SolidBrush(Color.White), <br />
lt.X, lt.Y, StringFormat.GenericTypographic);<br />
}<br />
}<br />
<br />
float CalcWidth(Graphics g, string s) {<br />
StringFormat fmt = StringFormat.GenericTypographic;<br />
fmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;<br />
SizeF size = g.MeasureString(s, Font, Point.Empty, fmt);<br />
return size.Width;<br />
}<br />
<br />
void MoveCaret() {<br />
using (Graphics g = Graphics.FromHwnd(Handle)) {<br />
float w = CalcWidth(g, lines[cur.Y].Substring(0, cur.X));<br />
caret.Position = new Point((int)w, Font.Height * cur.Y);<br />
}<br />
}<br />
<br />
private void InitializeComponent() {<br />
this.AutoScaleBaseSize = new System.Drawing.Size(6, 12);<br />
this.ClientSize = new System.Drawing.Size(292, 266);<br />
this.Font = new System.Drawing.Font("MS ゴシック", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));<br />
this.Name = "Form1";<br />
this.Text = "Form1";<br />
<br />
}<br />
<br />
[STAThread]<br />
static void Main() {<br />
Application.Run(new MyForm());<br />
}<br />
}<br />
<br />
<br />
have a good day ...
I know nothing , I know nothing
|
|
|
|
|
|
I see this same #$%# in lotsa managed code (see SharpDevelop too)! Why??? A managed caret is just as easy to make...
|
|
|
|
|
Is one better than the other? I have done some research and found that if I have a ClassA that overloads the '==' operator then using ClassA == null will actually go into that overload operator function, but object.Equal(ClassA,null) does not go into the overloaded '==' operator function. Furthermore, what is really weird is if I say ClassA myClassA; in my code and do not set it. myClassA equals null and then coding if (myClassA == null);. My overloaded '==' operator function will get called and I HAVE to use object.Equal(myClassA,null). I cannot use if (this == null && myClassA == null) return true; within my overloaded '==' operator function because it would go into an infinite loop.
Another thing which I think is weird is using object.Equal() instead of ==, I will sometimes get a Code Analysis warning CA1062 (Ex: Microsoft.Design : Validate parameter 'value' passed to externally visible method GettingControlEventArgs.set_Control(Control):Void. So in my code if I do not want the overloaded == operator to get called if myClassA is null then I first must ust object.Equal() and then use == to test if myClassA is null to get rid of the Code Analysis warning.
Chris
|
|
|
|
|
chris175 wrote: Is one better than the other?
The better one is the one that does exactly what you want to do...
if I have a ClassA that overloads the '==' operator then using ClassA == null will actually go into that overload operator function, but object.Equal(ClassA,null) does not go into the overloaded '==' operator function.
You are not talking about overloading, but overriding. Overloading is when you have more than one method with the same name but with different parameters.
Both the == operator and the Equals method can be overridden, and using either on the object will use the overridden one. If you override the == operator, you should also override the Equals method (and also the != operator and the GetHashCode method).
If you use Object.Equals , you have specified that it should use the static method in the Object class. Static methods can not be overridden.
Furthermore, what is really weird is if I say ClassA myClassA; in my code and do not set it. myClassA equals null
Only if you declare the variable in a class. If you declare it as a local variable in a method, it will be undefined, and the compiler will prevent you from using it before assigning it a value.
and then coding if (myClassA == null);. My overloaded '==' operator function will get called and I HAVE to use object.Equal(myClassA,null).
No, you don't HAVE to. You can simply cast your object to Object :
if ((object)myClassA == null)
This will use the == operator of the Object class instead of the overridden == operator.
---
single minded; short sighted; long gone;
|
|
|
|
|
Guffa wrote: You are not talking about overloading, but overriding
But the custom operator implementation must be a static method, which doesn't fit in with overriding. Even MSDN talks about overloading[^] operators, not overriding them.
|
|
|
|
|
Yes, you are right. Operators are overloaded.
This is a special form of overloading, though, as operators are overloaded on a global level, not within the same class. I didn't really realise that distinction until now.
---
single minded; short sighted; long gone;
|
|
|
|
|
Is is possible to bind the datareader to a datagrid in windows based application.
Can any one pass me the hint?
Thanx,
Seenivasan
|
|
|
|
|
Hi
It is not possible. Bind to datagrid using dataset and fill the dataset using DataAdapter.
Harini
|
|
|
|
|
Hi,
Thanx for your reply
|
|
|
|
|
Hi.
I want to print a pdf file from C# code.
For this I found a "not-very-elegant, but will-do-for-now" type of solution:
Call AcrobatReader with command line parameters which tell it to print the file:
System.Diagnostics.Process.Start("C:\\Program Files\\Adobe\\Acrobat 7.0\\Reader\\Acrord32.exe",<br />
"/t \"mytest.pdf\" \"MyWindowsPrinterName\"");
It works, but the drawback is that the AcrobatReader program is opened and stays open afterwards. It starts minimized thanks to the "/t" parameter in the call, but it's still open.
So I'm thinkin' of killin' the AcroRd32.exe process after it has completed it's mission.
Now the question: When can the AcroRd32 process be killed without fear of undesirable side-efects(exceptions, etc)?
If I kill it immediately after the call, the file never gets printed.
How can I check that the print job has been enqueued to the printer?
Thanks for having the patience to read.
|
|
|
|
|
System.Diagnostics.Process.Start returns a Process object which you can use to kill it later..
another drawback is acrobat readed's path.. it can be different for everybody..
life is study!!!
|
|
|
|
|
Seishin# wrote: life is study!!!
Errrr...OK
There are 10 types of people in the world, those who understand binary and those who dont.
|
|
|
|
|
what?!.. you learn throughout your whole life...
besides watch GoldenBoy, then we'll talk
life is study!!!
|
|
|
|
|
Seishin# wrote: System.Diagnostics.Process.Start returns a Process object which you can use to kill it later..
Well, yes. That's not the problem. The problem is I have to wait until the document is sent for printing(that takes some time from the moment the process is started) and kill it afterwards.
Seishin# wrote: another drawback is acrobat readed's path.. it can be different for everybody..
Yes, I'll see how I can find that after I take care of the current issue.
|
|
|
|
|
blackjack2150 wrote:
Seishin# wrote:
another drawback is acrobat readed's path.. it can be different for everybody..
Yes, I'll see how I can find that after I take care of the current issue.
There is probably a key in the registry that holds the path.
There are 10 types of people in the world, those who understand binary and those who dont.
|
|
|
|
|
Were you able to find a solution? I'm having the same problem. I don't want to just wait X number of seconds and then kill the process, that's kinda ugly.
|
|
|
|
|
Hi. Nope and the feature was ultimately abbandoned. In the meanwhile I also changed jobs.
I remembers there was a solution that used Ghostscript and gsprint (http://pages.cs.wisc.edu/~ghost/gsview/gsprint.htm[^]), but IIRC it was dual-licensed and you had to pay to use it in commercial software.
All the best!
|
|
|
|
|
Can you please tell me how to write the contents to the pdf file because it will be helpfull to me as i am creating asame type of applicaiton
Best Regards,
M. J. Jaya Chitra
|
|
|
|
|
Hello
I want to change the property of column header in datagridview.
Reply me asap.
Thanks
Devesh Mishra
|
|
|
|
|
There are lot of propeties present in the DataGridView Class for column header in MSDN.
Please refer MSDN. some of them are,
- ColumnHeadersBorderStyle
- ColumnHeadersDefaultCellStyle
- ColumnHeadersHeight
- ColumnHeadersHeightSizeMode
- ColumnHeadersVisible.
Praveen Raghuvanshi
Software Engineer,
Wins Infotek Pvt. LTd.
India.
|
|
|
|
|
Thanks man...
Devesh Mishra
|
|
|
|
|
Hai everybody,
Can u give me a hint how to make two windows service to communicate.
Best Regards,
M. J. Jaya Chitra
|
|
|
|
|
Hi Jaya,
One way is to use Remoting.
You can also use MSMQ. There might be more ways of doing this. The best way needs to be figured out based on your application requirements.
i Think, i Wait, i Fast rAm
|
|
|
|
|
As ganti pointed out, there are more ways to let the service communicate with eachother. Another option is using a memory pipe to communicate. But I personally think remoting might be a better solution.
WM.
What about weapons of mass-construction?
"What? Its an Apple MacBook Pro. They are sexy!" - Paul Watson
|
|
|
|
|