C# Scroll text using DrawString - c#

I'm quite new to C# so excuse me if this is a stupid question. I would like to scroll some text from the bottom of the screen to the top of the screen line-by-line. What is the easiest way to achieve this?
Cheers,
Pete

Since you're planning on writing directly to the Desktop, I would strongly suggest not doing this.
A much simpler way is to draw the text onto a transparent form (use the form's TransparencyKey property to achieve this), and then move the Location of the form to achieve the scrolling effect.

On the screen on the form ?
Because if on the screen then you will need to import the DllImport("User32.dll")] and use method.
public static extern IntPtr GetDC(IntPtr hwnd);
public static extern void ReleaseDC(IntPtr dc);
If on the form easy way is to create a method that will change the string positon and put in in a look that sleep for a 100ms.

Related

Clipping the EO.WebControl, WPF & C#

I am trying to positioning EO.WebBrowser.Wpf.WebControl using .NET 4.5. It is one of WPF webbrowsers from NuGet. For some reasons, I need the instance of that webbrowser to be bigger than its parent object. My expectation was, that it is possible to clip it with the parent by something like this:
WebControl Browser = new WebControl()
{
ClipToBounds = true
};
Unfortunately, it is not working. For illustration following pictures (sorry for the links, but I don't have rights to send images directly):
http://unsite.cz/StackOverflow/7XJrP.png
http://unsite.cz/StackOverflow/DHwud.png
On the picture, there is used red System.Windows.Controls.Border to highlight the problem. Left and top part is OK, but bottom and right not, beacause the browser overflows its bounds. The hierarchy used for illustration is Window > Border > ScrollViewer > WebControl.
I tried to place some panels between ScrollViewer and Webcontrol, but without results.
I also tried to manipulate the Z-Index with no influence on it.
My last try was to solve this problem by following extern function for clipping:
[DllImport("User32.dll", SetLastError = true)]
private static extern Int32 SetWindowRgn(IntPtr hWnd, IntPtr hRgn, Boolean bRedraw);
The function is OK when I clipping whole window, but I don't know (and I think that it is not possible) to clip with it just one specific WPF window descendant.
So here is my question: How to force EO.WebBrowser.Wpf.WebControl to autoclipping, or how to do it manually?
Thanks
P.S. Sorry for my english...

Snap WinForm to program

Is it possible to do the following with WinForms/C#?
Dynamically detect window size and position of a running program (for example Notepad.exe)?
Snap WinForm to specific position within Notepad.exe?
Minimize and maximize WinForm window with other process (so when Notepad is minimize, so is WinForm window)?
See for example (black shape would be WinForm window):
Essentially I need to create a toolbar for a program, and the toolbar should "snap" to that program in the same place regardless of position or size of window.
First find the handle of the notepad window:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
Just pass null for the first parameter and the caption ("Notepad"?) of the window as the second parameter.
An alternative would be to enumerate all windows and select the best match based on the caption:
using System.Runtime.InteropServices;
public delegate bool CallBackPtr(int hwnd, int lParam);
private CallBackPtr callBackPtr;
public class EnumReport
{
[DllImport("user32.dll")]
private static extern int EnumWindows(CallBackPtr callPtr, int lPar);
public static bool Report(int hwnd, int lParam)
{
Console.WriteLine("Window handle is "+hwnd);
return true;
}
}
static void Main()
{
// note in other situations, it is important to keep
// callBackPtr as a member variable so it doesnt GC while you're calling EnumWindows
callBackPtr = new CallBackPtr(EnumReport.Report);
EnumReport.EnumWindows(callBackPtr, 0);
}
Then attach a WndProc to it:
HwndSource src = HwndSource.FromHwnd(windowHandle);
src.AddHook(new HwndSourceHook(WndProc));
In the WndProc respond to the resizing and moving of the window.
I am not sure about setting the toolbar as a child of the notepad window; that might have unexpected effects when Notepad tries to manage it and order its z-depth.
At the same time I doubt this to be a good thing; the user will be able to type 'below' the overlay and lose his cursor/text.
Find Notepad's window (FindWindow).
Create your window without borders.
Set your window as a child of Notepad's window (SetParent).
Your window will be anchored to the top left corner of Notepad's window. Minimizing will be handled automatically, but you'll need to resize your window when Notepad's window is resized (or maximized). You may also want to move Notepad's edit control.
WinForms can be used, but you'll need some interop calls.
I have to warn that this is not a very good idea. Your controls may conflict with controls inside host process's window, host process may rearrange controls the way you don't like, draw over your controls. In general, be ready to fight with numerous issues without a good clean solution, and to accept that there may be glitches when resizing etc.
See also:
Attach form window to another window in C#.

How to change opacity when the form is on desktop level

I'm writing a tool for my own, that replaces the windows desktop.
The code, that puts my program to desktop level is the following (C#):
IntPtr hwndf = this.Handle;
IntPtr hwndParent = FindWindow("ProgMan", null);
SetParent(hwndf, hwndParent);
Cause of this I can't set the opacity of the form to anything else than 100% (otherwise the program won't start).
Since I didn't find a good way to make single controls semi transparent I want to ask if somebody has an idea either how to make the whole form or the controls (eg. GridViews and TextBoxes) opaque or make the background of all controls transparent.

How to put a form behind desktop icons?

How would you put a WinForms form behind desktop icons but in front of the wallpaper? To make the desktop the form's parent, I use:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
IntPtr desktopHandle = (IntPtr)FindWindow("Progman", null);
WallForm wallWindow = new WallForm();//WinForms Form
...
private void SwitchParent()
{
wallWindow.Show();
SetParent(wallWindow.Handle, desktopHandle);
//wallWindow.SendToBack();
}
This works, but it puts the form in front of the desktop icons. If I call SendToBack on my form, it disappears, presumably behind the wallpaper. How could I get the form to be between the icons and the desktop background?
I don't believe that this is possible to do. The desktop window is a single window that renders the desktop image and the icons, so there is no way to insert your window between the desktop image and the icons.
Short of writing a shell replacement that handled the background image and desktop icons differently (a major development task with many hurdles) the only other option I can think of is to hook into the desktop's events and intercept WM_ERASEBKGND or similar to do your own drawing. (See this question or this question for more info.)
Unfortunately this won't let you put a WinForm behind the icons, only an image. You'd have to handle a lot of other windows messages to simulate an actual form. It's a major undertaking regardless.
There is a solution to this problem, at least for Windows 8. I postet it in form of an article on CodeProject, so you can read about it here:
http://www.codeproject.com/Articles/856020/Draw-behind-Desktop-Icons-in-Windows
This works for simple drawing, windows forms, wpf, directx, etc. The solution presented in that article is only for Windows 8.

drawing a string on the screen C#

I would like to simply draw a string (if possible in a specific font and size) on the screen (at a specific location). I am within a C# windows forms application. Unfortunately, I could not found any hint on how to do this in the web.
Please help!
Christian
To draw a string outside of your window, you'll have to CREATE a new window, set it's mask to some color (say magenta) and then draw text onto it - you can use simple label here.
Set your window border style to None, and there you go.
In other words, there is no way of displaying 'free text' without window attached.
For masking color, use 'transparency color' or similar property (I will look up into it later - have no VS at hand)
doing what you are asking for is not really recommended, see e.g. Link
If you really want to do something like this; here is a creepy way to do it:
[DllImport("User32.dll")]
public static extern IntPtr GetDC(IntPtr hwnd);
[DllImport("User32.dll")]
public static extern void ReleaseDC(IntPtr dc);
protected override void OnPaint(PaintEventArgs e)
{
IntPtr desktopDC = GetDC(IntPtr.Zero);
Graphics g = Graphics.FromHdc(desktopDC);
g.DrawString("Test", new Font(FontFamily.GenericSerif, 12), Brushes.Blue, 300, 300);
g.Dispose();
ReleaseDC(desktopDC);
}
Please note that I DON'T recommend anyone doing this as I don't think applications should be doing stuff like this. If you want to draw something you should do it on your own form/controls.
Check this out.
Or may be you are looking for DrawString method
Hope this will help

Categories