drawing a string on the screen C# - 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

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...

C# Ghost Outline Form Resize

Normally, my C# app shows the full window contents dynamically resizing while the form itself is resizing.
I clearly remember some apps doing this very nice effect where rather than doing this, it would just show a ghostly border during resize and THEN redraw the window.
How do I get this lovely effect in my forms? I can't find any thing on Google that appears to pertain to this.
PInvoke SystemParametersInfo to change it, but it changes for all windows.
Here is a references for all commands: MSDN SystemParametersInfo
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern int SystemParametersInfo(int uAction, int uParam, int lpvParam, int fuWinIni);
[STAThread]
static void Main() {
int SPI_SETDRAGFULLWINDOWS = 0x0025;
SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,0,0,2);
Application.Run(new Form());
}

Firing events when window is moved

I am currently developing an iTunes plug-in and I need to have my WPF plug-in to stick beside the iTunes window when dragged, resized, etc. The goal is to have iTunes sticked to my wpf app side by side.
I am looking for a way to track the movement (resizing, moving) of another window (in my case iTunes). The iTunes COM for Windows SDK offers events on maximize and minimize, unfortunately there are no events for resizing and moving the window.
I have tried the win32 setParent function without no succes. I don't think it's the appropriate solution for my problem. I have searched thoroughly through out the web but didn't find nothing.
I think the WINDOWPOS structure is what you're looking for. Other window structures may come in handy.
Some google searching turned up another example, that doesn't use WINDOWPOS:
1, Invoke API FindWindow() to retrieve the window handle (Uuse SPY++
to get the two parameters ClassName & WindowName);
2, Invoke API
GetWindowRect() to retrieve the size & postion of the specified
window.
Code Snippet
[DllImport("user32.dll")]
private static extern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
private void button2_Click(object sender, EventArgs e)
{
string className = "yourClassName";
string windowName = "yourWindowName";
Rectangle rect;
IntPtr hwnd = FindWindow(className, windowName);
GetWindowRect(hwnd, out rect);
}

C# Scroll text using DrawString

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.

How to add an extra button to the window's title bar?

I've seen that some apps (maybe not .NET apps) that have an extra button on the left from the minimize button on the form's title bar? How can I achieve this in C#?
UPDATE: Added a solution that will work with Aero enabled for Windows Vista and Windows 7
***Non-Aero Solution***
The non-client area of a window interaction is managed by a series of non-client specfic messages. For example WM_NCPAINT message is sent to the window procedure to paint the non-client area.
I have never done this from .NET, but I suspect you can overide the WndProc and handle the WM_NC* messages to achieve what you want.
Update: Since I never tried this from .NET I got a few minutes and thought I would give it a quick try.
Trying this on Windows 7, I found that I needed to disable the Themes for the Window if I wanted to OS to do the base rendering of the non-client area. So here is a short test. I used GetWindowDC to get the DC of the entire window rather than GetDCEx, that was just because I could interop that from memory and did not have lookup all the flag constants for GetDcEx. And of course the code could do with more error checking.
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class CustomBorderForm : Form
{
const int WM_NCPAINT = 0x85;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("user32.dll", SetLastError = true)]
public static extern void DisableProcessWindowsGhosting();
[DllImport("UxTheme.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr SetWindowTheme(IntPtr hwnd, string pszSubAppName, string pszSubIdList);
public CustomBorderForm()
{
// This could be called from main.
DisableProcessWindowsGhosting();
InitializeComponent();
}
protected override void OnHandleCreated(EventArgs e)
{
SetWindowTheme(this.Handle, "", "");
base.OnHandleCreated(e);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
switch (m.Msg)
{
case WM_NCPAINT:
{
IntPtr hdc = GetWindowDC(m.HWnd);
using (Graphics g = Graphics.FromHdc(hdc))
{
g.FillEllipse(Brushes.Red, new Rectangle((Width-20)/2, 8, 20, 20));
}
ReleaseDC(m.HWnd, hdc);
}
break;
}
}
}
}
Btw. I called DisableProcessWindowsGhosting, this will stop the OS from drawing the non-client area if the application takes too long to respond to windows messages. If you do not do this, then in some situations the border will be renderd but your adornments will not be shown. So that depends on your requirements it that is right for you or not.
***Aero supported solution***
Prompted by the comment from #TheCodeKing, I thought I would take another look at this. It turns out this can be done in a fully documented way while supporting Aero. But it is not for the faint of heart. I will not provide a complete solution here, there are still some kinks to workout, but it does the basics.
This code/solution is based off the Win32 example which can be found at the following location
http://msdn.microsoft.com/en-us/library/bb688195(VS.85).aspx
In principal what you need to do is the following.
Extend the client area of the window to cover the Frame. This is done by handling the WM_NCCALCSIZE message and returning 0. This gives the Non-Client area a size of 0 and therefore the client area now covers the entire window.
Extend the Frame into the client area using DwmExtendFrameIntoClientArea. This gets the OS to render the Frame over the client area.
The above steps will give you a windows with the standard glass frame excluding the system menu (Window Icon) and the title. The minimize, maximize and close buttons will still be drawn and will work. What you will not be able to do is drag or resize the window, this is because the frame is not really there, remember the client area covers the whole window, we have just asked the OS to draw the frame onto the client area.
Now you can draw on the window as normal, even on top of the frame. You can even put controls in the caption area.
Finally, allow the DWM to handle hit-testing for you, by calling DwmDefWindowProc from your WndProc (before you've processed it). It returns a boolean indicating whether the DWM handled the message for you.
Simple Solution:
Step 1: Create a Windows Form (this will be your custom title bar)
-Set Form Border Style to None
-Add whatever controls you would like to this
-I will name this custom form "TitleBarButtons"
Step 2. In the from that you want to use this custom control in add
titleBarBtn = new TitleBarButtons();
titleBarBtn.Location = new Point(this.Location.X + 100, this.Location.Y+5);
titleBarBtn.Show();
titleBarBtn.Owner = this;
To your constructor... you can mess with the offsets this just fit in a nice position for my app
Step 3. Add the move event to your main form
private void Form14_Move(object sender, EventArgs e)
{
titleBarBtn.Location = new Point(this.Location.X + 100, this.Location.Y+5);
}
Please let me know if you would like a better explanation of any of the above code.
I think a way to do this would be to handle WM_NCPAINT message (non-client paint) to draw the button, and to handle non-client mouse clicks to know someone clicked on the "button".

Categories