Here are some frequently asked questions about Windows Forms and their answers.
Browse by Tags
All Tags » Appearance (RSS)
-
|
We have a small sample that shows how to do this. Download PropTab.zip . After you download and unzip the sample, build the project. Both the control assembly and a small driver assembly get built. After that add the control to your toolbox using 'Customise...
|
-
|
I set a control's Visible property to true and in the next statement, it returns false. Why doesn't setting the Visible property work? A control's Visible property depends on its parent control's Visible property, if it has a parent control...
|
-
|
You should override the WndProc method in the control and process the WM_DISPLAYCHANGE message. You could also do this at the form level. For details on overriding the WndProc method, see How do I process Windows messages in a control? in this FAQ. Contributed...
|
-
|
Here is a method using Reflection to achieve this. [ DllImport( "user32.dll" ) ] private extern static int SendMessage( IntPtr hwnd, uint msg, int wParam, int lParam); object o = typeof( ToolTip ).InvokeMember( "Handle", BindingFlags...
|
-
|
Include a \n as part of the tiptext. toolTip1.SetToolTip( button1, "Press for\ninformation..." ); Contributed from George Shepherd's Windows Forms FAQ
|
-
|
From within the designer: 1) From the ToolBox, drap a ToolTip control to your form. 2) Check the properties of this toolTip1 object to make sure Active is set to true. 3) Click on your control (e.g., a button), and in its Property page, set the ToolTip...
|
-
|
One generic way for all controls is to get the difference between the ClientRectangle's width and the Control.Bounds' width. That should give the border width (and in general the non-client area width). Use the same approach for height. Contributed...
|
-
|
Override the custom control's CreateParams property as shown in this sample. protected override CreateParams CreateParams { get { CreateParams cparams = base.CreateParams; cparams.ExStyle &= ~512; cparams.Style &= ~8388608; // WS_BORDER cparams...
|
-
|
You will have to first provide some space in the NC area by setting the WS_BORDER flag in CreateParams and then draw the border yourself by listening to the WM_NCPAINT message in your Control, as follows: protected override CreateParams CreateParams ...
|
-
|
Providing a border in the non-client region of a control, rather than in the ClientRectangle, has several advantages. If you include a scrollbar in the control, the scrollbar will appear inside the border. If you draw the border in the client area, the...
|
-
|
Override the control's OnPaint method. Here is a sample custom Button. [C#] using System.Drawing; using System.Windows.Forms; public class ButtonWithCustomBorder : Button { protected override void OnPaint( PaintEventArgs e ) { base.OnPaint( e ); Color...
|
-
|
To draw on any hwnd, you need to get a Graphics object from that hwnd. Once you have the Graphics object, you can then use its methods to draw. Of course, since this drawing is not done in the Paint handler, it will not be automatically refreshed when...
|
-
|
Handle the Paint event for your control or form. private void Form1_Paint( object sender, PaintEventArgs e ) { Graphics g = e.Graphics; Pen pen = new Pen( Color.White, 2 ); SolidBrush redBrush = new SolidBrush( Color.Red ); g.DrawEllipse( pen, 100, 150...
|
-
|
By default a control's background color will be the same as the container's background color. You can set the control's BackColor property to Transparent in the Designer. Select the control in the Designer, select the BackColor property in...
|
-
|
Make sure that you include the manifest file that will enable XP themes support for you application. Then the icons with alpha channel will draw semi-transparently. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Why do the XP Icons when drawn using Graphics.DrawImage not draw transparently? Note that it's only the ImageList class that can draw an Icon with alpha channel properly. So, instead of using the Graphics.DrawImage method, first associate this icon...
|
-
|
This is possible if the Control is drawn by the system, rather than by the framework. This is the case for example with the Label control when its set to FlatStyle.System. You should then set to to something else other than System for your transparent...
|
-
|
Sometimes the framework will throw an exception if you try to set the bg color to be transparent. This is because the Control doesn't support transparent colors. To work around this you should call this method from within the Control: SetStyle( ControlStyles...
|
-
|
The Window.Forms framework offers support for double buffering to avoid flickers through ControlStyles. Double buffering is a technique that attempts to reduce flicker by doing all the drawing operations on an off-screen canvas, and then exposing this...
|
-
|
Use the DrawImageDisabled method of the ControlPaint class. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
When I try to set a particular font style, say italics, I get an error message "Property cannot be assigned to -- it is read only". Code such as the following will not work . control.Font.Italic = true; Instead create a new Font object and set...
|
-
|
Use the Font property for the control along with the Font class in the System.Drawing namespace. button1.Font = new Font ( "Courier", 10, FontStyle.Bold ); Contributed from George Shepherd's Windows Forms FAQ
|
-
|
How do I programmatically change the color of a control? Set the control's BackColor and ForeColor properties. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
See the Rubber Band Effect in a Form sample from Simon Bond at C# Corner that implements this sizing technique. Be sure to read the article Debugging "Rubber Band Effect" , where Zhanbo Sun suggests a modification that handles a problem he spotted...
|
-
|
This code snippet shows how you can move a borderless form. private const int WM_NCLBUTTONDOWN = 0xA1; private const int HTCAPTION = 0x2; [ DllImport( "user32.dll" ) ] public static extern bool ReleaseCapture(); [ DllImport( "user32.dll"...
|
-
|
The framework automatically resizes the form if the current Font size during runtime is different from the font size in design-time. It however doesn't auto size when the resolution changes. But it should be easy for you to accomplish. You could derive...
|
-
|
You can prevent users from resizing a form by setting the FormBorderStyle to FixedDialog and setting the MaximizeBox property to false. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
You can restrict the size of a form by setting it's MaximumSize and MinimumSize properties. This will help you control the maximum and minimum size the form can be resized to. Also note that WindowState property of the form plays a part in how the...
|
-
|
The following code snippet demonstrates how you can make your form cover the whole screen including the Windows Taskbar. // Prevent form from being resized. FormBorderStyle = FormBorderStyle.FixedSingle; // Get the screen bounds Rectangle formrect = Screen...
|
-
|
Follow these steps to introduce Windows XP visual styles into your Windows application. 1. Create a new Windows Application and add some controls to the default form. 2. For every control you place on the form that has a FlatStyle property, set the property...
|
-
|
The .manifest file is not required if you are using .NET FrameWork 1.1. You can now use the application's EnableVisualStyles() method which should called before creating any controls. You also need to ensure that the FlatStyle property of the control...
|
-
|
Microsoft has confirmed that calling the Application.EnableVisualStyles method (in the System.Windows.Forms namespace) in the Main method resulting in some ImageList corruption is a bug. This workaround appears to solve the problem. public virtual void...
|
-
|
Here's what I use it in my code. 1. Set up a form for the user to select their style preferences. If this is not important to your app, go to step 2 and skip step 3. 2. Set up a Preference class. Imports System.Drawing Public Class StylePreferences...
|
-
|
Use the Form.FormBorderStyle property to control a form's border. public void InitCustomForm() { // Adds a label to the form. Label label1 = new Label(); label1.Location = new System.Drawing.Point( 80, 80 ); label1.Name = "label1"; label1...
|
-
|
Check out this MSDN Library article Shaped Windows Forms and Controls in Visual Studio .NET by Seth Grossman of the Visual Studio Team. There are two ways to create non-rectangular windows. The first way is to change the Control.Region property, which...
|
-
|
The opacity property enables you to specify a level of transparency for the form and its controls. See the .NET documentation for Form.Opacity for differences between Opacity and TransparencyKey properties. Opacity only works with Windows 2000 and later...
|
-
|
Set the form's Text and ControlBox properties. form1.Text = string.Empty; form1.ControlBox = false; Contributed from George Shepherd's Windows Forms FAQ
|
-
|
You can download a working project that uses this code. public void CreateMyBorderlessWindow() { FormBorderStyle = FormBorderStyle.None; MaximizeBox = false; MinimizeBox = false; StartPosition = FormStartPosition.CenterScreen; ControlBox = false; } Contributed...
|
-
|
Set the property Form.ControlBox to false. Contributed from George Shepherd's Windows Forms FAQ
|
-
|
Setting Form.Visible to false does not make my main form start up invisibly. How can I make my main form start up invisibly? This problem is discussed in an article in the .NET docs. Search for "Setting a Form to Be Invisible at Its Inception"...
|
-
|
You could do so as shown in the code below : Form form1 = new Form(); Bitmap bmp = imageList1.Images[index] as Bitmap; form1.Icon = Icon.FromHandle(bmp.GetHicon()); Please refer to the sample attached here that illustrates this. Contributed from George...
|