C# WINFORMS Controls Font not smooth at run time - c#

I have noticed this issue many times since Visual Studio 2010 but not all times. Now I am using Visual Studio 2019 most recent version. At design time I create controls like labels and buttons which shows very smooth text of Segoe UI font. After running Winform app the text on Labels and Buttons get creepy and not clear and smooth.
I have attached Image for your reference to see the difference.
I have also checked Application.SetCompatibleTextRenderingDefault(false); and form AutoScaleMode and also DoubleBuffered but with no luck. Is this a long time bug in Visual Studio or some settings of the system.
Kindly provide me a perfect way around of this issue. Thanks

see this by #Hans Passant:
https://stackoverflow.com/a/13228495/2696230
it has worked for me
[STAThread]
static void Main() {
if (Environment.OSVersion.Version.Major >= 6) SetProcessDPIAware();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); // Edit as needed
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();

Not sure if you want the smoothness only approved for you (on your computer) or for everyone using your application.
If the goal is to only approve it for you then enable Cleartype and make sure to use a mordern font.
If not, then you can make a picture of your text and use a PictureBox instead of a TextBox, Label or whatever you are using and insert the picture of the text to the PictureBox.
You may also create a custom class and use this instead of your item, then you can override the OnPaint event and change the Graphics TextRenderingHint. Check out this Link to see how it can be done with a Label.

Related

Lock PDF while drawing lines using touch

I'm using PDFtron for a Win 10 app. I'm building my custom in house tools for drawing and text. I know I have the controls but I prefer building my own. I know that when executing the sample, and drawing a line using the touch mode, the pdf control doesn't flip the page because it is locked. I've tried to investigate how this happens but I don't see it anywhere in the controls sample project. Does anybody know how to lock the control when drawing by touch?
Regards.
So this did the trick...
internal void UnFreeze()
{
_pdfViewer.SetZoomEnabled(true);
_pdfViewer.SetScrollEnabled(true);
}
public void Freeze()
{
_pdfViewer.SetZoomEnabled(false);
_pdfViewer.SetScrollEnabled(false);
}

Custom TabControl display issues

I've been writing a custom drawn tab control for a few days now and for the most part everything is pretty and it does an amazing job ... except when I use it on my Windows 10 computer (at run-time).
I've gone back to the most basic few lines of code to trace the error and I can't for the life of me figure this out.
Below is the only code being used, in a nutshell I'm designing a horizontal aligned tab control.
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1.UI
{
class TabControlTest : TabControl
{
public TabControlTest()
{
Alignment = TabAlignment.Left;
SizeMode = TabSizeMode.Fixed;
}
}
}
I've simply added the custom tab control to the form, added a couple of group boxes for reference purposes and changed the background colour of the form to grey so you can clearly see the tab control.
Now, at design time the 2 group boxes (1 in the tab control, 1 on the form) align perfectly.
But at run-time I see a very different result.
As you can see the tab part of the control is now larger than it was at design time and the resulting change means the contexts of the tab have also moved.
If I do this on a Windows 7 computer everything is displayed as it appears at design time, as it should!
I've added ImageSize but it makes no difference.
ItemSize = new System.Drawing.Size(30, 150);
I've reinstalled VS on my (Win10) development machine. I'm at a loss to explain why and how to resolve this.
Any/all help would be immensely appreciated.
Looking at your tab width in your comparison images, I believe this is another issue caused by automatic Windows control scaling. I found that it is the dpiAware option is automatically set when it's run from within Visual Studio and then reverts back to the default Windows Scaling that windows Implements when outside Visual Studio.
To prevent that auto-scaling when run outside Visual Studio altogether you need to Notify the OS that you're application is dpiAware by calling the Win32 P/Invoke SetProcessDPIAware() method from within your Main() before Application.Run() is called, like the example below demonstrates. This will let your controls use the native resolution which your designing the coordinates from.
static class Program
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
static void Main(string[] args)
{
if (Environment.OSVersion.Version.Major >= 6)
SetProcessDPIAware();
Application.Run(new UIMonitor());
}
}
Alternatively, if you want to keep the scaling, you may be able to set the GroupBox location based off the Width of the Tab Control instead of a specific location. (Or by using some combination of Control measurements instead of exact picel placement.)

How can I add a splash screen to my C# application? [duplicate]

How can we show startup picture when my application starts as every software hows like Photoshop ,vs word etc?? I planed to paste it on form and then show it but there is top blue bar coming that has controls etc any idea/
If you're looking for the simplest way, you can use the .NET Framework's excellent built-in support for splash screens. You'll have to put aside any irrational fears that you might have of including something with the name "Visual Basic" in a C# application, but this way will save you from having to roll your own custom solution and worry about things like multi-threading, invoking, and all that. It all compiles down to the same IL in the end anyway. Here's how it works:
Add a reference to Microsoft.VisualBasic to your project.
Add a new form (named something like SplashForm) to serve as your splash screen.
To make it look more like a proper splash screen, set the form's FormBorderStyle property to "None" and its StartPosition property to "CenterScreen". You can add any controls or images to this form that you want to be displayed on the splash screen to this form.
Add the following code to your Project.cs file:
using System;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication1
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
new SplashScreenApp().Run(args);
}
}
public class SplashScreenApp : WindowsFormsApplicationBase
{
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new SplashForm();
this.SplashScreen.ShowInTaskbar = false;
this.SplashScreen.Cursor = Cursors.AppStarting;
}
protected override void OnCreateMainForm()
{
//Perform any tasks you want before your application starts
//FOR TESTING PURPOSES ONLY (remove once you've added your code)
System.Threading.Thread.Sleep(2000);
//Set the main form to a new instance of your form
//(this will automatically close the splash screen)
this.MainForm = new Form1();
}
}
}
If you want to do something fancy like create a transparent splash screen in the style of Adobe Photoshop, you can add an alpha-channel PNG image to your project's Resources file, and then add the following code to your splash screen form, replacing splashImage with the path to your embedded image resource:
protected override void OnPaintBackground(PaintEventArgs pevent)
{
Graphics g = pevent.Graphics;
g.DrawImage(splashImage, new Rectangle(0, 0, this.Width, this.Height));
}
protected override void OnPaint(PaintEventArgs e)
{
//Do nothing here
}
For this to work, make sure that you have double buffering turned off, or else you'll get a black background to your form. There's really no reason to double buffer a splash screen anyway.
You can remove all those blue bars etc, via this.FormBorderStyle = FormBorderStyle.None in your Form_Load().
So if I were you, I'd create a Form of specific Size, then set the following in Form_Load()
or directly in the code generated by the designer:
this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPostition.CenterScreen;
Now you have a splash screen like many other apps - all you have to still do is write the code for all the making it visible or not stuff etc :)
WPF 3.5 and above actually has built in splash screen support, for faster rendering of the splash screen and much (much) less faffing with code. See this page for details.
Are you writing a WinForms or a WPF application? You will have to set different properties depending on which kind you write. If you are writing a WPF application, you can add the WindowStyle="None" and ResizeMode="NoResize" attributes to the Window top-level element in XAML. The first one will remove the title bar with the minimize, resize, and close buttons whereas the second one will remove the border around the form.
Now, you have a borderless form that you can modify to create a splash screen if you want or to simply add your startup image. If you don't want your default form to appear, you need to add Background="Transparent" and AllowsTransparency="True" attributes as well. The first one will set the background color to transparent whereas the second one allows your program to look transparent. Now, you can add any image in any shape, and the user will see only that image at program startup.
P.S. Make sure to load another form once the startup screen has been shown for a set amount of time or once the methods that are supposed to "load" stuff return control.
Pretty simple stuff!!

How to make label & other controls transparent with background image on windows mobile?

I am developing the smart device application in C#. I am new to the windows mobile. I have added the background image to the form in my application by using the following code. I want to make label & other controls on this form transparent so that my windows form will be displayed properly.
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Bitmap CreateCustomerImage = new Bitmap(#"/Storage Card/background.png");
e.Graphics.DrawImage(CreateCustomerImage, 0, 0);
}
how to do this ? How to solve this problem? Can you provide me any code or link through which I can solve the above issue?
Windows CE doesn't inherently support transparent controls, which tends to be a huge pain. You have to use something like ColorKey transparency, so in your OnPaint, you need to fill the background with a color (magenta is a popular one) and use SetColorKey to make that color transparent.
There are several tutorials online for colorkey transparency. Here is one that I just found with a search engine that looks reasonable but feel free to search for others as well.
The place this falls down is when you have controls in a container control, which is then on the Form. To get that to work right you have to cascade calls to clipping regions from the Form all the way down. I don't have a ready sample of this that isn't inside a shipping project, so I can't easily post it. If you run into this, though, update the question and I'll see if I can extract something.

Resizing window causes black strips

I have a form, which sets these styles in constructor:
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
And I draw some rectangles in Paint event. There are no controls on the form. Hovewer, when I resize the form, there are black strips at right and bottom of the form. Is there any way to get rid of them? I've tried everything, listening for WM_ERASEBKGND in WndProc, manually drawing the form on WM_PAINT, implementing custom double buffer, etc. Is there anything else I could try?
I've found this:
https://connect.microsoft.com/VisualStudio/feedback/details/522441/custom-resizing-of-system-windows-window-flickers
and it looks like it is a bug in DWM, but I just hope I can do some workaround.
Please note that I must use double buffering, since I want to draw pretty intense graphic presentation in the Paint event. I develop in C# .NET 2.0, Win7.
Status Update 1
I've managed to get rid of most of the black stripes by implementing the resize functionality by myself. Hovewer there are still some minor glitches. Is there any way to do resize and paint operation at once? Here is a pseudo-code of what I need to do:
IntPtr hDC;
var size = new Size(250, 200);
IntPtr handle = API.PaintAndResizeBegin(this.Handle /* Form.Handle */,
size.Width, size.Height, out hDC);
using (var g = Graphics.FromHdc(hDC)) {
this.backBuffer.Render(g, size);
}
API.PaintAndResizeCommit(handle);
Is there any way to implement the above code?
The second solution could be to back-buffer whole form, including non-client area. But how to do that? I don't want to paint the non-client area by myself, as I want to keep the nice aero effect on Vista/7. Any help will be deeply appreciated.
Status Update 2
It looks like this problem is unsolvable, since it is omnipresent on Windows, in every application. We can just hope that MS will take some inspiration in Mac OS X and will provide appropriate APIs in new Windows.
I've found the function which can paint and resize window at the same time - UpdateLayeredWindow.
So now it should be possible to create resizable windows, which do not have any strips while being resized. However, you need to paint the window content yourself, so it is a little inconvenient. But I think that using WPF and UpdateLayeredWindow, there shouldn't be any problem.
Update
Found problems. :-) When using UpdateLayeredWindow, you must paint the window's border yourself. So, if you want standard window painted using UpdateLayeredWindow with nice glass effect in win7, you are screwed.
On Microsft Connect is even a thread about this problem, where Microsoft says it is a bug by design, and if it ever gets fixed, then probably in Win8 or some newer system. So there isn't much we could do about this.
I found that it is best not to do any custom rendering directly on the Form surface. Instead, put a docked PictureBox on the form, create Bitmap object that will be displayed in the PictureBox, draw everything onto that using the System.Drawing.Graphics.FromImage(Image) method.
I used that method with a game loop to make a simple shooter game (Crimsonland-style) and got pretty good performance (with anti-aliased lines), above 100 FPS.

Categories