C# - Hide created form on switch window (alt + tab) from code - c#

I have a form that can create another form like this.
private void AEGISBot(String option) {
if (AEGIS == null) {
AEGIS = new Form();
AEGIS.ShowInTaskbar = false;
AEGIS.TopMost = true;
AEGIS.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
AEGIS.Size = new Size(396, 191);
//AEGIS.Size = new Size(720, 720);
AEGIS.StartPosition = FormStartPosition.CenterScreen;
AEGIS.BackColor = Color.LightBlue;
AEGIS.TransparencyKey = AEGIS.BackColor;
Label AEGISLabel = new Label();
AEGISLabel.Location = new Point(0, 0);
AEGISLabel.Size = new Size(AEGIS.Size.Width, AEGIS.Size.Height);
AEGISLabel.TextAlign = ContentAlignment.MiddleCenter;
AEGISLabel.Text = "AEGIS";
AEGISLabel.Font = new Font("Agency FB", 120, FontStyle.Bold);
AEGISLabel.ForeColor = System.Drawing.Color.Navy;
AEGIS.Controls.Add(AEGISLabel);
}
if (option == "show"){
AEGIS.Show();
}
}
But how to hide it from alt tab. I tried to add code like this.
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x80;
return cp;
}
}
My main form was successfully hidden from alt tab. But how to use it to created form ??
Thank you
-Edit
I am using Windows Form Application. With some form setting
this.ShowInTaskbar = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.ShowIcon = false;
this.WindowState = FormWindowState.Minimized;

Try using AEGIS.ShowDialog() instead of AEGIS.Show().

Related

Cursor = Cursors.None for popup in code

Im trying to set the cursor to none in code for a popup but I cant get it to work. The cursor is still shown when it is over the popup. What am I doing wrong?
public void SubWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
TextBlock popupText = new TextBlock();
popupText.Text = "Complete" ;
popupText.Background = Brushes.Transparent;
popupText.Foreground = Brushes.White;
popupText.Width = 130;
popupText.FontSize = 30;
popupText.IsHitTestVisible = false;
popupText.Cursor = Cursors.None;
Popup Popup = new Popup();
Popup.AllowsTransparency = true;
Popup.PlacementRectangle = new Rect(1086, 16, 0, 0);
Popup.IsHitTestVisible = false;
Popup.Cursor = Cursors.None;
Popup_Text.Child = popupText;
Popup.IsOpen = true;
}
Don't set the IsHitTestVisible property of the TextBlock to false:
TextBlock popupText = new TextBlock();
popupText.Text = "Complete";
popupText.Background = Brushes.Transparent;
popupText.Foreground = Brushes.White;
popupText.Width = 130;
popupText.Height = 130;
popupText.FontSize = 30;
//popupText.IsHitTestVisible = false;
popupText.Cursor = Cursors.None;
Popup Popup = new Popup();
//Popup.AllowsTransparency = true;
Popup.PlacementRectangle = new Rect(1086, 16, 0, 0);
Popup.IsHitTestVisible = false;
Popup.Cursor = Cursors.None;
Popup.Child = popupText;
Popup.IsOpen = true;
Also note that your app can only change the cursor when the cursor is actually over one of your app's elements. The "background" of a transparent Popup does not belong to your application so Cursors.None will only apply when you move the mouse pointer over the actual text in the TextBlock.

Form not in right position

I'd like to show an instance of a form class for a specific time. The form needs to be topmost and not steal focus. Here is my code:
public class mSplashForm : Form
{
public mSplashForm()
{
this.FormBorderStyle = FormBorderStyle.None;
this.BackColor = Color.LightBlue;
this.Opacity = 0.92D;
this.ShowInTaskbar = false;
this.MinimumSize = new System.Drawing.Size(5, 5);
}
}
public static void mSplash(int time = 500)
{
mSplashForm SF = new mSplashForm();
Application.EnableVisualStyles();
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;
SetWindowPos(SF.Handle, HWND_TOPMOST, SF.Left, SF.Top, SF.Width, SF.Height, SWP_NOACTIVATE);
ShowWindow(SF.Handle, mEnumShowWindowCommands.ShowNoActivate);
Application.DoEvents();
Thread.Sleep(time);
SF.Close();
}
It works, but the form is not shown in the right position defined using Top and Left parameters. What is wrong please?
You've got your form set to start in FormStartPosition.WindowsDefaultLocation. Add this into your mSplash function:
SF.StartPosition = FormStartPosition.Manual;
This is why it's trying to position successively down the page (as per your comment) on each opening.
set the start position to manual:
this.StartPosition = FormStartPosition.Manual;
Try this
SF.StartPosition = FormStartPosition.Manual;
SF.Width = 500;
SF.Height = 100;
SF.Left = 500;
SF.Top = 500;

how to make a vertical progress bar

I'm trying to make a vertical progress bar and I understand that there isn't any easy way to do it.
I've seen this code floating around the forums:
public class VerticalProgressBar : ProgressBar
{
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
return cp;
}
}
}
My question is where do I put this code? Does it go in my program.cs file or the form that the progress bar is on?
It doesn't matter where you put the code, you only have to make sure you are creating a VerticalProgressBar in your Form.Designer.cs file.
You have to change
private System.Windows.Forms.ProgressBar progressBar1
to
private VerticalProgressBar progressBar1
(or whatever it is called) and
this.progressBar1 = new System.Windows.Forms.ProgressBar();
to
this.progressBar1 = new VerticalProgressBar();
Still don't have this control available in VS 2022 so I used a TablePanelLayout with one column and two rows. and just changed the RowStyle SizeType percent. You can easily add code to change the panel color when a limit is reached etc.
public class BarGraph : Panel
{
private Panel panel1;
private Panel panel2;
private TableLayoutPanel table;
private float percentValue;
public BarGraph()
{
table = new TableLayoutPanel();
table.ColumnCount = 1;
table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
table.Name = "tableLayoutPanel1";
table.RowCount = 2;
table.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
table.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
table.Dock = DockStyle.Fill;
table.Margin = new Padding(0);
table.Padding = new Padding(0);
table.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;
panel1 = new Panel();
panel2 = new Panel();
panel1.AutoSize = false;
panel1.Dock = DockStyle.Fill;
panel1.Padding = new Padding(0);
panel1.Margin = new Padding(0);
panel2.AutoSize = false;
panel2.Dock = DockStyle.Fill;
panel2.Padding = new Padding(0);
panel2.Margin = new Padding(0);
panel1.BackColor = System.Drawing.Color.White;
panel2.BackColor = System.Drawing.Color.Blue;
table.Controls.Add(panel1, 0, 0);
table.Controls.Add(panel2, 0, 1);
this.Controls.Add(table);
this.Size = new System.Drawing.Size(150, 500);
SetValue("50");
}
public float GetValue()
{
return this.percentValue;
}
public void IncreaseValue()
{
IncrementValue(percentValue - 1);
}
public void DecreaseValue()
{
IncrementValue(percentValue + 1);
}
private void IncrementValue(float value)
{
SetValue(value.ToString());
}
public void SetValue(string value)
{
float.TryParse(value, out percentValue);
if ((percentValue >= 0) && (percentValue <=100))
{
table.RowStyles.RemoveAt(1);
table.RowStyles.RemoveAt(0);
table.RowStyles.Add(new RowStyle(SizeType.Percent, 100 - percentValue));
table.RowStyles.Add(new RowStyle(SizeType.Percent, percentValue));
}
}
}
If this is a brand new application, use WPF. vertical progress bars are built-in
<ProgressBar Orientation="Vertical" />
I found this post while looking for a way to make a vertical progress bar and used code in the question as the answer to my own problem. The accepted is right for sure so ✔ and ✔.
I'm adding this as just a bit of added value hopefully. In my own project I also needed to customize the color. I ended up using the OP's question to make my class and modified it so that ForeColor can be set to change the indicator color. All this and a working sample to boot.
class VerticalProgressBar : ProgressBar
{
public VerticalProgressBar() => SetWindowTheme(Handle, string.Empty, string.Empty);
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.Style |= 0x04;
return cp;
}
}
[DllImport("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
public extern static Int32 SetWindowTheme(IntPtr hWnd,
String textSubAppName, String textSubIdList);
}
Designer code
"Where to put the code" (as stated in the accepted answer).
private void InitializeComponent()
{
this.progressBarTemperature = new weather_client.VerticalProgressBar();
this.SuspendLayout();
.
.
.
//
// progressBarTemperature
//
this.progressBarTemperature.Location = new System.Drawing.Point(35, 12);
this.progressBarTemperature.Name = "progressBarTemperature";
this.progressBarTemperature.Size = new System.Drawing.Size(20, 113);
this.progressBarTemperature.TabIndex = 2;
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(385, 144);
this.Controls.Add(this.progressBarTemperature);
.
.
.
this.Name = "MainForm";
this.Text = "Main Form";
this.ResumeLayout(false);
}
private VerticalProgressBar progressBarTemperature;

Easiest way to create a custom dialog box which returns a value?

I want to create a custom dialog box for my C# project. I want to have a DataGridView in this custom dialog box, and there will also be a button. When the user clicks this button, an integer value is returned to the caller, and the dialog box then terminates itself.
How can I achieve this?
There is no prompt dialog box in C#. You can create a custom prompt box to do this instead.
public static class Prompt
{
public static int ShowDialog(string text, string caption)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 100;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top=20, Text=text };
NumericUpDown inputBox = new NumericUpDown () { Left = 50, Top=50, Width=400 };
Button confirmation = new Button() { Text = "Ok", Left=350, Width=100, Top=70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox);
prompt.ShowDialog();
return (int)inputBox.Value;
}
}
Then call it using:
int promptValue = Prompt.ShowDialog("Test", "123");
On your button set the DialogResult property to DialogResult.OK
On your dialog set the AcceptButton property to your button
Create a public property in your form called Result of int type
Set the value of this property in the click event of your button
Call your dialog in this way
using(myDialog dlg = new myDialog())
{
if(dlg.ShowDialog() == DialogResult.OK)
{
int result = dlg.Result;
// whatever you need to do with result
}
}
public static DialogResult InputBox(string title, string promptText, ref string value,bool isDigit=false)
{
Form form = new Form();
Label label = new Label();
TxtProNet textBox = new TxtProNet();
if (isDigit == true)
textBox.TypeNumricOnly = true;
textBox.Width = 1000;
Button buttonOk = new Button();
Button buttonCancel = new Button();
form.Text = title;
label.Text = promptText;
textBox.Text = value;
buttonOk.Text = "OK";
buttonCancel.Text = "Cancel";
buttonOk.DialogResult = DialogResult.OK;
buttonCancel.DialogResult = DialogResult.Cancel;
label.SetBounds(9, 20, 372, 13);
textBox.SetBounds(12, 36, 372, 20);
buttonOk.SetBounds(228, 72, 75, 23);
buttonCancel.SetBounds(309, 72, 75, 23);
label.AutoSize = true;
textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
form.ClientSize = new Size(396, 107);
form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.StartPosition = FormStartPosition.CenterScreen;
form.MinimizeBox = false;
form.MaximizeBox = false;
form.AcceptButton = buttonOk;
form.CancelButton = buttonCancel;
DialogResult dialogResult = form.ShowDialog();
value = textBox.Text;
return dialogResult;
}
//combo box dialog c#
//
public static string DialogCombo(string text,DataTable comboSource,string DisplyMember,string ValueMember)
{
//comboSource = new DataTable();
Form prompt = new Form();
prompt.RightToLeft = RightToLeft.Yes;
prompt.Width = 500;
prompt.Height = 200;
Label textLabel = new Label() { Left = 350, Top = 20, Text = text };
ComboBox combo = new ComboBox { Left = 50, Top = 50, Width = 400 };
combo.DataSource = comboSource;
combo.ValueMember = ValueMember;
combo.DisplayMember = DisplyMember;
Button confirmation = new Button() { Text = "تایید", Left = 350, Width = 100, Top = 70 };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(combo);
prompt.ShowDialog();
return combo.SelectedValue.ToString();
}
public partial class DialogFormDisplay : Form
{
public DialogFormDisplay()
{
InitializeComponent();
}
string dialogcode;
public void Display(string code, string title, string description)
{
dialogcode = code;
switch (code)
{
case "YesNo":
btnLeft.Text = "Yes";
btnLeft.BackColor = Color.ForestGreen;
btnLeft.ForeColor = Color.White;
btnRight.Text = "No";
btnRight.BackColor = Color.Red;
btnRight.ForeColor = Color.White;
break;
default:
break;
}
displayTitle.Text = title;
displayMessage.Text = description;
}
private void btnLeft_Click(object sender, EventArgs e)
{
dialogResultLeft(dialogcode);
}
private void btnRight_Click(object sender, EventArgs e)
{
dialogResultRight(dialogcode);
}
void dialogResultLeft(string code)
{
switch (code)
{
case "YesNo":
DialogResult = DialogResult.Yes;
MessageBox.Show("You pressed " + DialogResult);
break;
default:
break;
}
}
void dialogResultRight(string code)
{
switch (code)
{
case "YesNo":
DialogResult = DialogResult.No;
MessageBox.Show("You pressed " + DialogResult);
break;
default:
break;
}
}
}
You can check this out on https://github.com/gurvirlochab/CustomNotifications Here is a sample screenshot of the DialogBox

Adding custom components programmatically at runtime

I'm having a problem using custom components at runtime.
I have this custom FlowLayoutPanel component:
public partial class UserControl1 : System.Windows.Forms.FlowLayoutPanel
{
[Browsable(false)]
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x20;
return cp;
}
}
protected override void OnPaintBackground(PaintEventArgs e){}
public UserControl1(){}
}
it has nothing complicated. All it does is making the background transparent.
I added this component to my Toolbox using it's dll and it works perfectly fine when I drag and drop it to my Form. The problem is, I can't add it programmatically at runtime.
When I run the code below it should draw the picture on top of my custom FlowLayoutControl.
But unfortunately it does nothing.
The custom component is under the WindowsFormsControlLibrary1 namespace.
namespace MyFilm_v2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
UserControl1 test = new UserControl1();
test.BackColor = Color.Transparent;
test.Location = new Point(0, 110);
test.Width = 660;
test.Height = 478;
PictureBox b = new PictureBox();
b.Location = new Point(100, 100);
b.Width = 320;
b.Height = 475;
b.Image = Properties.Resources.movie;
this.Controls.Add(b);
//this.customScrollbar1.Minimum = 0;
//this.customScrollbar1.Maximum = test.DisplayRectangle.Height;
//this.customScrollbar1.LargeChange = customScrollbar1.Maximum / customScrollbar1.Height + test.Height;
//this.customScrollbar1.SmallChange = 15;//when click the arrows
//this.customScrollbar1.Value = Math.Abs(test.AutoScrollPosition.Y);
}
....
You adding picturebox to your test controls, but you not adding test to forms controls
public Form1()
{
InitializeComponent();
UserControl1 test = new UserControl1();
test.BackColor = Color.Transparent;
test.Location = new Point(0, 110);
test.Width = 660;
test.Height = 478;
PictureBox b = new PictureBox();
b.Location = new Point(100, 100);
b.Width = 320;
b.Height = 475;
b.Image = Properties.Resources.movie;
test.Controls.Add(b);
this.Controls.Add(test);//<- here
}

Categories