I have seen many other samples out there that draw smooth text on glass. But I can't use them. I need every single label that gets added at runtime to be smooth. I can't just "draw" text onto the screen.
Is this at all possible, and are there and sources around?
Thank you
Take a long at this article http://msdn.microsoft.com/en-us/magazine/cc163435.aspx#S6
It's a bit long but it answers alot of your question and alore more in regards to glass.
but the relevant part for you directly is
One particular gotcha is that
rendering a GDI item in black uses the
bit pattern 0x00000000-which also
happens to be a completely transparent
black if you are using an alpha
channel. This means that if you draw
with a black GDI brush or pen you'll
get a transparent color, not a black
one. The biggest problem this presents
is when you try to use the default
text color in a control of a text
label that sits on the glass area.
Since the default text color is
usually black, the DWM will consider
this to be transparent and the text
will be written in the glass
incorrectly. An example can be seen in
Figure 10. The first line is written
with GDI+, the second is a text label
control using the default color. As
you can see, it's nearly illegible
because it's actually incorrectly
rendered text that shows up as gray,
not black.
Happily, there are a number of ways
around this problem. Using owner-draw
controls is one. Rendering to a bitmap
that has an alpha channel is another.
Fortunately, the easiest way to get
text on controls is to let the .NET
Framework 2.0 use GDI+ for you. This
is easily accomplished by setting the
UseCompatibleTextRendering property on
your controls. By default, this
property is set to false so that
controls written for previous versions
of the .NET Framework will render the
same. But if you set it to true, your
text will come out looking correct.
You can set the property globally with
the
Application.SetUseCompatibleTextRenderingDefault
method.
He also provides example code you can place in your Main()
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(true);
Application.Run(new GlassForm());
}
But I recommend reading the article, It'll clear up alot of what's going on with Aero/Glass
Cheers,
Phyx
Related
Is it possible to set the textbox text to align at the center while using auto size property to false.I tried with Text align property, but it does't work.Here is my code.
textBox1.AutoSize = false;
textBox1.Size = new Size(100,35);
textBox1.TextAlign = HorizontalAlignment.Center;
Below is a screen sample :
I want the result to be as show in figure.
TextBox is one of the grand-daddy controls in the toolbox. Goes back all the way to 1987 and Windows version 2.0. Back when it was still a 16-bit real-mode operating system and had to run in 640 kilobytes of memory. It is also notorious for breaking the rules, painting itself without using WM_PAINT. The kind of thing Microsoft had to do to get acceptable perf from a 386SUX processor. The dearth of memory was a major reason to cut down on its features.
They did not do much to improve the control, although it certainly looks a heckofalot better than it did 28 years ago. Changing it behavior is very risky, TextBox is a major app-compat nightmare with 28 years of programmers trying to hack it to do more.
But there is no way to hack it to look the way you want it, it always renders the text top-aligned. You must have noticed the fight that it put up to stop you from writing that code. It is also very, very wrong code, hard-coding the size produces very undesirable accidents when it runs on a high DPI machine, displaying text with the descenders sheared-off. AutoSize should always be set to True for a single-line TextBox to prevent such accidents.
You can otherwise emulate it pretty easily, just embed it in a panel that is as tall as you want it and set its BorderStyle property to None. Use its Resize event to center it in the panel. Easy peasy.
As the question is about vertical arrangement and also for single mode there is a method but it would be working around with panels.
Place textbox inside a panel with paddings there you go :
My goal is to draw text in a single layout with certain ranges different sizes and opacities.
The ID2D1RenderTarget::DrawTextLayout method seems to be the way to go.
The documentation for the defaultForegroundBrush parameter:
The brush used to paint any text in textLayout that does not already
have a brush associated with it as a drawing effect (specified by the
IDWriteTextLayout::SetDrawingEffect method).
According to the Remarks section of the IDWriteTextLayout::SetDrawingEffect method,
An ID2D1Brush, such as a color or gradient brush, can be set as a
drawing effect if you are using the ID2D1RenderTarget::DrawTextLayout
to draw text and that brush will be used to draw the specified range
of text.
This drawing effect is associated with the specified range and will be
passed back to the application by way of the callback when the range
is drawn at drawing time.
It sounds like ID2D1RenderTarget::DrawTextLayout will definitely use any brush set by IDWriteTextLayout::SetDrawingEffect. This unmanaged C++ answer seems to corroborate this idea.
However, in practice, DrawTextLayout ignores any SolidColorBrush I set using SetDrawingEffect. I get styles and sizes in the appropriate ranges, but everything is painted using the default brush.
I worked around this by implementing a custom text renderer (gist) which is dead simple and drew exactly what I expected from ID2D1RenderTarget::DrawTextLayout as per the documentation. I would have been satisfied but the performance of a TextRendererBase and DrawGlyphRun are more than 25% slower than ID2D1RenderTarget::DrawTextLayout.
What might be causing this issue? Can I use color as the documentation suggests and still use ID2D1RenderTarget::DrawTextLayout?
instead of:
layout.SetDrawingEffect(myBrush, new TextRange(1, 5));
call it like this:
layout.SetDrawingEffect(myBrush.NativePointer, new TextRange(1, 5));
How do I set the background color of a piece of text in a PDF document using iTextSharp without taking a form field?
The answer in this post uses a FormField, which according to me is an overkill and too long-winded a way to do something really simple.
Is there a simple way of coloring the background of a piece of text?
You can use the method SetBackground that is available in the Chunk class. There are two variations of this method: one that takes default padding and one that allows you to change the padding.
If you use the onGenericTag() method on a Chunk, you can draw a custom background (and do much more). For instance: you'd use onGenericTag() if you want to draw a rectangle with rounded corners. See my answer to your duplicate question Draw a rectangle at the *current position* and then get its position coordinates
After some trying, I have come to the conclusion that there are 3 ways to do this other than using the FormField (which is the fourth way and how to do that is already linked in the question):
1) Judging from this answer to another similar question, it appears as though there is no concept of a background color for text in the PDF specification. Therefore, one has to draw a rectangle at an absolute position before drawing the text (at that position).
This is like drawing on a Win32 DeviceContext.
2) You can draw a table and set the background color of the cell in which you want a background color.
3) You can write a chunk. The Chunk class has a method named SetBackground(). This doesn't look very nice because it doesn't let you control the padding around the text and between the borders of the box. You can control how far above the baseline the bottom of the text will appear by calling the chunk.SetTextRise(float f) method but that's about it. Still, it's a fast and easy way to get things done if you don't want too much beautification.
In VS2008 I designed a form for a C# dll. The dll is a plugin for a somewhat older app (ca. 2005): let's call it "OldApp". In VS form designer, the text in Label controls on my form is nicely rendered: antialiased and properly kerned. But when I bring up this form within OldApp (where the C# dll runs as a plugin), the text in Label controls looks ugly. It's legible, but the kerning is poor: the letters are spaced further apart and at seemingly random offsets. Anything I can do to make the text labels from within OldApp look as good as they do in VS's form designer? I doubt the specific font matters, but it's Arial, 7.2 pt (VS2008 default). I tried playing with the two relevant lines in Program.cs (see below), to no effect.
Application.EnableVisualStyles(); // tried using it and commenting it out
Application.SetCompatibleTextRenderingDefault(true); // tried true and false
I found a similar problem on MSDN forums that mentions adding the following line after the EnableVisualStyles() method.
Application.DoEvents()
Seems to be a bug in older .NET versions...which version are you using?
After an investigation I have some findings, so I'll just answer my own question:
The bad news: the old-style text rendering used by OldApp is what's causing the problem. I verified it by toggling the UseCompatibleTextRendering property for the label control in VS. The font distortion I see is the same one I see in OldApp. Which means that the Application.SetCompatibleTextRenderingDefault(false) line in my code has no effect. OldApp will ignore it and do old-style rendering anyway.
As suggested by DeviantSeev using a bigger font helps a bit. It doesn't get rid of the bad kerning, it just makes it less noticeable. I increased the font from 7.2pt to 8pt only (not 12pt), because the dialog box becomes too big otherwise. The way to do this is in the form's Font property (not the control's). This way, you'll change all controls uniformly (if their Font property is set to default).
The font sizes in VS appear to be discrete rather than continuous, or maybe there's an int() rounding off involved. Increasing the font from 7.2pt to 7.4pt results in very little change, while at 7.5pt the font makes a sudden jump in size.
Forms have an AutoScaleMode property. If it's set to Font and the form is resizeable, the form will resize in VS in proportion to the change in font size. This way, in VS you can find an acceptable middle ground between a (legible) font size and a bloated dialog. However, be careful: the auto-scale operation can suddenly go awry, for example if you change the Font units from points to pixels, inches, etc. You may suddenly end up with microscopic controls or a form bigger than your screen and hitting undo won't fix it. You really don't want to re-design your form again, so save it before any font unit change and then again when you're happy with what you see.
I'm writing a C# (ActiveX) plugin for an application that uses SetCompatibleTextRenderingDefault(true) (forces .net 1.1 text rendering style) This setting mangles some of the text I output with Graphics.DrawString() causing it to look slightly smudged and bolded. Unlike individual controls neither the Graphics class nor the BitMap have UseCompatibleTextRendering properties that can be used to override the individual behavior. Short of fiddling around to try and figure out what's special about the places where I'm drawing text that doesn't get mangled is there anything I can do about this?
The app my plugin is for belongs to a third party so simply changing the SetCompatibleTextRenderingDefault call it inflicts on me is not an option.
Edit: The 'special' thing appears to be the color of the background and how it's affecting the anti-aliasing used; so fiddling to fix it by how I setup the rectangles doesn't seem to be an option.
I'd advise using TextRenderer.DrawText instead of Graphics.DrawString - even with compatible text rendering disabled, it seems to produce crisper, more consistent results.
I found a fix for my problem by changing the TextRenderingHint to SingleBitPerPixelGridFit which is the default when not using compatible text rendering. When it's instead set to true it uses a the ClearType enumeration except that for whatever reason unlike normal cleartype text the results are ugly and extremely hard to read.
textGraphics.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;