C# Prevent Blinking Cursor in Textbox, Solution not Working - c#

I have found many pages describing how to prevent the blinking caret. Looks simple enough.
[DllImport("user32")]
public static extern bool HideCaret(IntPtr hWnd);
private void OnFocusEnterSpecificTextbox(object sender, EventArgs e)
{ HideCaret(SpecificTextbox.Handle); }
It's not working. When I click on the Textbox, there's the caret. I can breakpoint and see that I am hitting that code.
What boneheaded mistake am I making?

This works (VS 2008 on Windows 7):
public partial class Form1 : Form
{
[DllImport("user32")]
public static extern bool HideCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
HideCaret(textBox1.Handle);
}
}

Here's another way to stop blinking cursor in TextBox:
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
}
}

Related

Detect new app and run it in panel

I am creating application that runs another app inside panel.
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public Form3() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
Process p = Process.Start(#"path\app.exe");
Thread.Sleep(200); // Allow the process to open it's window
SetParent(p.MainWindowHandle, panel1.Handle);
}
But the problem is, that app.exe sometimes (I know when) creates new window as a new app. I want to add this new window into new panel.
private Process GetProcess() {
//do some magic stuff and find actually running app
return NewAppProcess;
}
private void button2_Click(object sender, EventArgs e) {
Process p = GetProcess();
SetParent(p.MainWindowHandle, panel2.Handle);
}
Thanks for everything that can push me to right way
Using ManagementEventWatcher, you can watch Win32_ProcessStartTrace to receive an event when a new process starts.
Example
In this example, I shows how you can watch starting of mspaint.exe and adding it as child of a Panel in your form. To so add a reference to System.Management dll to your project and then use the following code.
Note 1: The watcher is not super fast and you probably see the the window opens in desktop and then sits in the panel.
Note 2: It's an example and showing hot to do it with mspaint.exe. If you have any problem applying the solution on your real app.exe, you need to specifically ask about the solution for your app.exe.
Note 3: Make sure you run your as administrator.
using System.Management;
using System.Runtime.InteropServices;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]
static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
ManagementEventWatcher watcher;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
watcher = new ManagementEventWatcher(
"Select * From Win32_ProcessStartTrace Where ProcessName = 'mspaint.exe'");
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Start();
}
void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
var id = (UInt32)e.NewEvent["ProcessID"];
var process = System.Diagnostics.Process.GetProcessById((int)id);
this.Invoke(new MethodInvoker(() => {
SetParent(process.MainWindowHandle, panel1.Handle);
}));
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
watcher.Stop();
watcher.Dispose();
base.OnFormClosed(e);
}
}

After child form is hidden parent is not activated [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have 3 form: A, B, C
Form1 A;
Form2 B, C;
A is parent of B and C
public partial class Form1 : Form
{
Form2 formB = null;
Form2 formC = null;
public Form1()
{
InitializeComponent();
formB = new Form2();
formB.Owner = this;
formC = new Form2();
formC.Owner = this;
}
private void showBC_Click(object sender, EventArgs e)
{
formB.Visible = true;
formC.Visible = true;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void hide_Click(object sender, EventArgs e)
{
this.Hide();
}
}
When application start, form A is show.
I open another program (ex: cmd), to active cmd window
I click on form A, to active form A
I click on button ShowBC -> showBC_Click
Form B and C is shown
I click button hide on C then B is actived
I click button hide on B and I hope A is active (you think so?)
cmd window is active
// ==============================================
#Sinatr
I have same problem with only A and B form
public partial class Form1 : Form
{
Form2 formB = null;
public Form1()
{
InitializeComponent();
formB = new Form2();
formB.Owner = this;
}
private void showB_Click(object sender, EventArgs e)
{
formB.Visible = true;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void hide_Click(object sender, EventArgs e)
{
this.Hide();
}
private void MsgBox_Click(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
}
When application start, form A is show.
I open another program (ex: cmd), to active cmd window
I click on form A, to active form A
I click on button ShowB -> showB_Click
Form B is shown
I click button MsgBox on form B
MessageBox "Test" is shown
I click button OK on MessageBox
Messagebox is closed
I click button hide on B and I hope A is active (you think so?)
cmd window is active
I expect A is active in the final step
Solution:
Because some child window is not form, like SelectColor Dialog, .. so I need to use win32 api to list child window to active them.
In every child form I need to do that:
[DllImport("user32.dll")]
private static extern IntPtr GetTopWindow(IntPtr parentHandle);
private static uint GW_HWNDNEXT = 2;
[DllImport("user32.dll")]
private static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
[DllImport("user32.dll")]
private static extern int IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr SetFocus(IntPtr parentHandle);
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);
public static void SetAppFocus()
{
IntPtr topWindowHandle = GetTopWindow(IntPtr.Zero);
while (topWindowHandle != null)
{
if (IsWindowVisible(topWindowHandle) != 0)
{
int currentProcessId = Process.GetCurrentProcess().Id;
int processId = 0;
GetWindowThreadProcessId(topWindowHandle, out processId);
if (processId == currentProcessId)
{
SetFocus(topWindowHandle);
break;
}
}
// goto next window
topWindowHandle = GetWindow(topWindowHandle, GW_HWNDNEXT);
}
}
I was able to reproduce the issue.
It should be something related to activating Owner when more than one forms has same owner, because it works correctly with just one Form2.
Bug here is that another Form2 is activated if either one is closed.
Try to
void showBC_Click(object sender, EventArgs e)
{
B.Visible = C.Visible = true;
Activate();
}
Now everything work "properly": closing either Form2 will activate the owner.
If you want to keep original behavior, then here is a workaround:
public Form1()
{
InitializeComponent();
formB = new Form2 { Owner = this };
formC = new Form2 { Owner = this };
formB.VisibleChanged += Child_VisibleChanged;
formC.VisibleChanged += Child_VisibleChanged;
}
void Child_VisibleChanged(object sender, EventArgs e)
{
if (!Application.OpenForms.Cast<Form>().OfType<Form2>().Any(o => o.Visible))
Activate();
}
You are leaving it up the OS to figure out which window should be activated when the one with the focus disappears. What it does here certainly doesn't win any prizes. Also a pretty big problem with WPF dialogs btw. Exactly why it does this is hard to guess, it just doesn't seem to pay enough attention to the window owner. Do note that it works just fine when you minimize the window instead of hiding it, why that acts differently is, well, weird. Let's not hesitate calling it a bug.
The workaround is pretty straight-forward, just don't force it to find another window by itself:
if (this.Owner != null) this.Owner.Activate();
this.Hide();
Also the solution in a WPF app.

How to make a picturebox dragable?

I am an enthusiastic programmer and new to stack overflow.
I am trying to build a prototype of an OS(Operating System) in C#...Just a test.
Just like we see that we can drag things in the desktop and put it anywhere I am creating a desktop for my OS.
So how should I make an icon(a picturebox) drag-able and how would I save its position so that next time I open my desktop I see it in the same place?
I would love the dragging without any freezes or those sneaky bugs. I would love if, it is as close and smooth as the ones in Windows(dragging items(icons) in desktop)..
Thanks...
Yes, it is.
Assume a Picturebox named "pbxBigCat" (load it with a pPicture ...)
Add this lines to your form:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
And then write an eventhandler for the pbxBigCat MouseDown event:
private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(pbxBigCat.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}
If you test it you will se it works. For more Information (like saving positions etc.) I refere to Make a borderless form movable?
Another possibility to do this (now we have a Label called label1)
public partial class Form1 : Form
{
private bool mouseDown;
private Point lastLocation;
public Form1()
{
InitializeComponent();
}
private void pbxBigCat_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDown = true;
lastLocation = e.Location;
}
}
private void pbxBigCat_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
pbxBigCat.Location = new Point((pbxBigCat.Location.X - lastLocation.X + e.X), (pbxBigCat.Location.Y - lastLocation.Y) + e.Y);
label1.Text = pbxBigCat.Location.X.ToString() + "/" + pbxBigCat.Location.Y.ToString();
}
}
private void pbxBigCat_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
}
Everything drawn from the example on the mentioned SO article.

Creating custom pop up keyboard nothing happen when keys are press

What I want to happen is that when a textbox is clicked a pop up keyboard will show and whatever i type on the keyboard will be putt in the textbox, but nothing happen but when I use notepad, it works. How can I fix this?
Here is my main form code:
private void textBox1_MouseClick(object sender, MouseEventArgs e)
{
Form1 frm1 = new Form1();
frm1.ShowDialog();
}
And here is my code for pop up keyboard
public partial class Form1 : Form
{
const int WS_EX_NOACTIVATE = 0x08000000;
//this line of code fixed the issue
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
public Form1()
{
InitializeComponent();
}
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= WS_EX_NOACTIVATE;
//This line of code fix the issues
param.Style = 0x40000000 | 0x4000000;
param.Parent = GetDesktopWindow();
return param;
}
}
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("1");
}
private void button3_Click(object sender, EventArgs e)
{
SendKeys.Send("2");
}
private void button4_Click(object sender, EventArgs e)
{
SendKeys.Send("3");
}
private void button2_Click(object sender, EventArgs e)
{
}
}
Use form1.Show() ... not ShowDialog()
If you manually added the testbox make sure you hook into the MouseEventHandler like so:
this.textBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox1_MouseClick);
when your form loads.
I fixed the issue by just adding this line of code
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
public static extern IntPtr GetDesktopWindow();
And
param.Style = 0x40000000 | 0x4000000;
param.Parent = GetDesktopWindow();

Shake detection mouse

My code is used to form1
private int X,Y;
private void timer1_Tick(object sender, EventArgs e)
{
if (this.X != Cursor.Position.X ||
this.Y != Cursor.Position.Y)
{
this.Form1_Load(this, e);
}
else
{
this.Text = (Convert.ToInt16(this.Text)+1).ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "0";
this.X = Cursor.Position.X;
this.Y = Cursor.Position.Y;
}
Other forms will not answer
If the form2 is open. The above code does not work.
Well I am not sure what exactly want to accomplish, but try to set form2.Owner = this (if you show form2 from from1) or form2.Owner = form1.
This code for the screensaver :
//inForm1
[DllImport("user32.dll", EntryPoint = "GetDesktopWindow")]
private static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
//...
private const int SC_SCREENSAVE = 0xF140;
private const int WM_SYSCOMMAND = 0x0112;
//...
public static void SetScreenSaverRunning()
{
SendMessage
(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
}
public void Shakedetectionmouse(EventArgs e)
{
//CallTimer1_Tick
timer1_Tick(this,e);
}
private void timer1_Tick(object sender, EventArgs e)
{
//ShowScreenSaver
timer1.Enabled = true;
timer1.Interval = 1000;
SetScreenSaverRunning();
}
private void button1_Click(object sender, EventArgs e)
{
//ShowForm2
using (var Form2 = new Form2())
{
Form2.ShowDialog();
}
}
//Inform2
private void Form2_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Interval = 2000;
}
private void timer1_Tick(object sender, EventArgs e)
{
using(var form1 = new Form1())
{
form1.Shakedetectionmouse(EventArgs.Empty);
}
}
In any form should be used Timer Component
Is it not a way to solve this issue?
screen saver Windows can be used?

Categories