|
I think i already post answer for the same question.
Anyhow don't upset.;)
<br />
try<br />
{<br />
PrintDia=new PrintDialog();<br />
PrintDia.Document=printDocument1;<br />
string content=this.RitchEditor.Text;<br />
StrReader=new StringReader(content);<br />
if(PrintDia.ShowDialog()==DialogResult.OK)<br />
{<br />
this.printDocument1.Print();<br />
}<br />
}<br />
catch(Exception Ex)<br />
{<br />
MessageBox.Show(Ex.Message.ToString());<br />
}<br />
This much is very easy one who know how to print the content. Next is importent.
<br />
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)<br />
{<br />
try<br />
{<br />
float Lines_Per_Page = 0;<br />
float Y_Position = 0; <br />
int Count = 0;<br />
float Left_Margin = e.MarginBounds.Left;<br />
float Top_Margin = e.MarginBounds.Top;<br />
<br />
string Line = null;<br />
Font fnt = Editor.Font;<br />
SolidBrush Solid = new SolidBrush(Color.Black);<br />
<br />
Lines_Per_Page=e.MarginBounds.Height/Font.GetHeight(e.Graphics);<br />
<br />
while(Count < Lines_Per_Page && ((Line=StrReader.ReadLine())!=null ))<br />
<br />
{<br />
<br />
Y_Position =Top_Margin + (Count * fnt.GetHeight(e.Graphics));<br />
<br />
<br />
e.Graphics.DrawString(Line ,fnt ,Solid ,Left_Margin ,Y_Position ,new StringFormat());<br />
<br />
Count++;<br />
}<br />
<br />
if(Line !=null)<br />
e.HasMorePages=true;<br />
else<br />
e.HasMorePages=false;<br />
}<br />
catch(Exception Ex)<br />
{<br />
MessageBox.Show(Ex.Message.ToString());<br />
}<br />
<br />
<br />
}
Sreejith Nair
[ My Articles ]
|
|
|
|
|
|
Dear Mr. Gold !
I'm trying to find out about Face Recognition system using neural network (back propagation algorithm). I intent to write a program using C#.The most importance in face recognition is preprocessing step, i know that we can feed network with a pixel matrix of image but i don't know how to do this?? And would you mind give me some algorithm for image processing to improve its quanlity as reduce intensity, light, noise????
|
|
|
|
|
Hi
I am writing a windows forms app in C# and sometimes when i build and run the application in debug mode I get the two error screens with the following messages
An unhandled exception of type 'System.NullReferenceException' occured in Unknown Module
There is no source code available for the current location.
Does anyone have any ideas about this? There is no call stack to see where the issue is.
It does not occur every single time. Only about fifty percent of the time but it is anoying.
Thanks for any help.
|
|
|
|
|
try starting the program in debug mode directly. take every code line that is in main and surely you will find the code that makes you headaches. i don't think that the exception is from the .net implementation, maybe you letted an variable unalocated (when one defines a variable assignes it a value, a default value, just to be sure )
I hope you understand...
By the way... visit http://nehe.gamedev.net[^]
|
|
|
|
|
Excuse me, but what are you talking about?????????????????????????????????????
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
//VC++:
memset( SPTWB, 0, Marshal.SizeOf( Win32.SCSI_PASS_THROUGH_WITH_BUFFERS ) );
If C# has an identical funtion to do the same work?
|
|
|
|
|
|
can anybody help me to suspend a process and then resume it also
|
|
|
|
|
What do you mean by "suspend process"?
If your talking about what I'm thinking, you can't. A process can be tagged low priorty, but must remain running to keep it's message pump alive and processing messages. This would include having the app repaint it's client window when necessary, even though the user is not interacting with the app. If you suspend the process and move a smaller window over the suspended process, it won't repaint itself. This would have the effect of having one window "erase" the window of the suspended process.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Is Thread.Sleep(milliseconds) what you need?
Regards,
Alvaro
Our enemies are innovative and resourceful, and so are we. They never stop thinking about new ways to harm our country and our people, and neither do we. - George W. Bush
|
|
|
|
|
Hi All,
Is it possible to get the address of an object reference
variable in C#?
Cheers
|
|
|
|
|
In my form application, I repeatedly obtain graphics from a sequence of images by using Graphics.FromImage(...). At first few calls, it is fine. But after a while, there will an exception: System.InvalidOperationException: The object is currently in use elsewhere.
I checked on the web, it seems this is related to a microsoft bug and can be solved by a hotfix. I updated my machine with several patches, but the problem is still there.
Any suggestion? Thanx.
|
|
|
|
|
Have you ever called Disposed on Graphics object?
Maqsood Ahmed
Kolachi Advanced Technologies
http://www.kolachi.net
|
|
|
|
|
Yes, I did. I tried both with dispose and no dispose. There is no difference.
|
|
|
|
|
how would i make a control that can save and edit bmps and have a grid like the following...
___________
| | | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
| | | | | |
-----------
|
|
|
|
|
Hi~
In C# compact framework, I would like to send a message to a window written using "SendMessage" method by p/Invoke.
How can I pass the string to p/invoke method so that the C++ window can get the message? If a pass string, How to convert the lparam into string in C++
windows message loop function? If pass the string as other type, then what is the type?
In compact framework, the key word "MarshalAs" is not supported. Then is there any other way to solve it?
[DllImport("coredll.dll",EntryPoint="SendMessage", CharSet=CharSet.Auto)]
private static extern void SendMessage(IntPtr hwnd, uint msg, int wparam, [MarshalAs(UnmanagedType.LPSTR)] string lparam);
Thanks
|
|
|
|
|
MarshalAs isn't a keyword - it's an attribute, as in MarshalAsAttribute . What you have isn't even correct on the full .NET Framework, though. Why do you set CharSet=CharSet.Auto , then try to marshal the string as an UnmanagedType.LPSTR ? An LPSTR is a char* . You should marshal as an LPTSTR (TCHAR* ), but setting the CharSet field to CharSet.Auto does that automatically.
The only reason overloading the SendMessage function like this would work is because by passing a TCHAR* you pass the address of the first character (rather, the first byte which is 1 character in ANSI and 2 Unicode (typically). Since both an WPARAM and LPARAM are just addresses (32 bits on a 32-bit system, and 64 bits on a 64-bit system, unless you're running under WOW64 to emulate a 32-bit processor for a 32-bit application, which is currently all that's available with .NET 1.x), the following signature should work for you:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
extern static IntPtr SendMessage(IntPtr hWnd, uint msg, IntPtr wParam, string lParam); As you may have noticed, I've changed the signature a little more. The return value should be an IntPtr . By not defining the return value correctly you could corrupt the stack (a return value needs to be pushed back onto the stack whether or not you use it). And, as I said, both the WPARAM and LPARAM parameters are processor-dependent so you need to use an IntPtr (which is also a processor-dependent bit width). If you recompiled for 64-bit processors ever, your code would not work (you'd be trying to call the 32-bit SendMessage on the 64-bit implementation and should surely have an execution stack problem since your parameters wouldn't be correct and you'd end up passing something else (uninitialized data, etc. - definitely not your address and most likely ending up with an Access Violation (AV) exception).
PS: To pass 0 as the wParam that's now correctly defined as an IntPtr , use IntPtr.Zero .
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Then, I pass a string to the method "SendMessage". How can I turn the lparam to LPSTR or any other string pointer? It is simple that just cast the lparam to string pointer? Thanks
|
|
|
|
|
You really need to understand the difference between LPSTR , LPWSTR , and LPTSTR . Since CharSet.Auto is specified in the DllImportAttribute , the string (always Unicode in .NET) will be marshaled as ANSI strings on Win9x/Me and Unicode strings on Windows NT (includes 2K, XP, 2K3, and future versions of Windows).
If you need to marshal from an IntPtr to a string (assuming that memory address points to a string) then use the relevent Marshal class methods, like Marshal.PtrToStringAuto . Read more about the class in the .NET Framework SDK for more information.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Oh sorry,
How to let a IntPtr point to a string in C#?
Thanks
|
|
|
|
|
The only reason to get the start address of a string as an IntPtr would be to marshal it, and the Marshal class does just that, so I told you exactly how to do it in C# (remember, C# is just a language that targets the CLR; it's the .NET Framework that provides the language constructs).
You could also use GCHandle to pin the string in memory so that the garbage collector (GC) doesn't move it around after you've sent the address to unmanaged code (if the GC moves it, the unmanaged code won't know and will access the wrong data):
string s = "Hello, world!";
GCHandle handle = GCHandle.Alloc(s, GCHandleType.Pinned);
IntPtr ptr = handle.AddrOfPinnedObject();
SomeUnmanagedPInvokedFunction(ptr);
handle.Free(); In most cases you don't need to worry about this, though. Passing a string is actually passing the address of the string, so declaring your P/Invoke signatures correctly aleviates this problem.
Again, read the documentation for interop'ing with unmanaged DLL functions I posted before, and play close attention to the marshaling topics toward the end.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi~
I use these code to past string to a C++ window but still doesn't work.
[C#]<br />
string s = "Hello, world!";<br />
GCHandle handle = GCHandle.Alloc(s, GCHandleType.Pinned);<br />
IntPtr ptr = handle.AddrOfPinnedObject();<br />
PostMessage(hWnd, msg, IntPtr.Zero, ptr);<br />
handle.Free();<br />
<br />
[DllImport="Test.dll", CharSet=CharSet.Auto]<br />
private static extern IntPtr PostMessage(IntPtr hwnd, uint msg, IntPtr wparam, IntPtr lparam);<br />
<br />
<br />
[C++]<br />
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)<br />
{<br />
if (message == WM_TESTING)<br />
{<br />
TCHAR* StringPtr = (TCHAR*)LPARAM;<br />
MessageBox(0,StringPtr,_T("Caption"),MB_OK | MB_INFORMATION);<br />
}<br />
else<br />
{<br />
return DefWindowProc(hWnd, message, wParam, lParam);<br />
}<br />
return 0;<br />
}
The message box in C++ show null string. How can I solve it?
Thanks
|
|
|
|
|
As I said in my first reply, in such a case you can define the last parameter as string instead of an IntPtr . Either passes the address of the variable and in this case you don't need to pin the string if you use SendMessage instead of PostMessage . Besides, you don't need to set DllImportAttribute.CharSet unless you're passing a string.
If you're C++ code was compiled without UNICODE (or _UNICODE , depending on which macros and APIs you're using - always be sure to define both) and get the IntPtr of your managed string - which is always Unicode - the ANSI version of your call to MessageBox won't display the string right. You should've at least seen a single character.
So, just define SendMessage how I showed you in the first place. You really only need PostMessage if you need to return immediately and let the message be processed on the target window's thread. If you use SendMessage , the thread context is switched - blocking your execution - and the message is processed until finished.
If you use PostMessage , then you'll need to pin the string but don't free it right away. Remember, your message may still not be processed. To marshal the string as well as get the IntPtr , use Marshal.StringToHGlobalAuto and when you know your message has bee processed, call Marshal.FreeHGlobal with the IntPtr returned from the previous call. This will make sure your string is marshaled as the correct type (ANSI or Unicode) and copied into global memory.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
In compact framework, there is no Marshal.StringToHGloablAuto method.
Also if I use sendMessage,
[DllImport("User32.dll"), CharSet=CharSet.Auto]<br />
private static extern IntPtr SendMessage (IntPtr Hwnd, uint msg, IntPtr wparam, string lparam)
is that correct?
In C++ side
Is that just Cast the lparam to TCHAR * like this?
TCHAR* stringPtr = (TCHAR*)lparam;<br />
MessageBox(0, stringPtr, _T("Caption"), MB_OK);
Thanks
|
|
|
|
|