Change Caret (Cursor) in Textbox in C# - c#

I want to change the Caret in a Textbox in C# that it appears wider like in does in old DOS applications.
What i have:
Example what i want:
I tried:
using System.Runtime.InteropServices;
...
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
...
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 20, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}
But it still looks the same. Any help would be nice. Thanks in advance!
Edit:
This was just an example. My real code looks like:
TextBox textbox = new TextBox();
textbox.MaxLength = fieldLength;
textbox.Width = fieldLength*24;
textbox.MaxLength = maxChars;
this.Controls.Add(textbox);
CreateCaret(textbox.Handle, IntPtr.Zero, 20, textbox.Height);
ShowCaret(textbox.Handle);
The code gets called but doesnt change anything.
Edit2:
I tried the example and it works fine but my problem still exisits:
I cant change the Caret when creating the textbox. Its only possible for a textbox created with the form.

You didn't link the event correctly, you should change to :
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
this.Shown += Form1_Shown;
}
private void Form1_Shown(object sender, EventArgs e)
{
CreateCaret(textBox1.Handle, IntPtr.Zero, 20, textBox1.Height);
ShowCaret(textBox1.Handle);
}
}

What is going on it seems is that the shown event will only initially work. When you leave the textbox by tabbing away and back to the control the caret will be reset by underlying code.
Take a look the answer in this thread.
I took their DrawDaret Method and changed it a bit. Calling DrawCaret on the textbox1.Enter event doesn't work. possibly the textbox implementation will notify the Enter event and then change the caret. This would undo a change done in the Enter event handler regarding to the caret.
EDIT
But the control also has a GotFocus event which you can use.
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern bool CreateCaret(IntPtr hWnd, IntPtr hBitmap, int nWidth, int nHeight);
[DllImport("user32.dll")]
static extern bool ShowCaret(IntPtr hWnd);
public Form1()
{
InitializeComponent();
textBox1.GotFocus += new EventHandler(textBox1_GotFocus);
}
void textBox1_GotFocus(object sender, EventArgs e)
{
DrawCaret(textBox1);
}
public void DrawCaret(Control ctrl)
{
var nHeight = 0;
var nWidth = 10;
nHeight = Font.Height;
CreateCaret(ctrl.Handle, IntPtr.Zero, nWidth, nHeight);
ShowCaret(ctrl.Handle);
}
}

Related

Disable text selection and edit for ComboBox

How to prevent any editing and selection attempts for ComboBox with ComboBoxStyle.DropDown (not DropDownList!) setting?
I override OnKeyDown and cut any keyboard input but mouse selection events still works and caret is visible.
I can propose you two (simple) methods to set a ComboBox text read-only-like.
Using SendMessage:
In this case, the ComboBox underlying Edit Control is set to ReadOnly and - as already said - its background color is painted with the non-active/disabled color (LTGRAY_BRUSH).
The same as if you set a TextBox control ReadOnly property to true.
[DllImport("User32.dll")]
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll", SetLastError = true)]
internal static extern int SendMessage(IntPtr hWnd, uint uMsg, int wParam, int lParam);
private const uint EM_SETREADONLY = 0x00CF;
private void Form1_Load(object sender, EventArgs e)
{
IntPtr EditHandle = FindWindowEx(comboBox1.Handle, IntPtr.Zero, "Edit", null);
SendMessage(EditHandle, EM_SETREADONLY, 1, 0);
}
Discarding any key press using the Control KeyDown event:
In this case, the ComboBox background color of course does not change, but there might be undesired behaviours, depending on its use.
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
Update:
A method to hide the caret (without subclassing or inheriting):
Here I'm using FindWindowEx again. You can assign the IntPtr EditHandle to a class scope variable and get the Edit control handle once in the Form Load event.
[DllImport("user32")]
internal static extern bool HideCaret(IntPtr hWnd);
[DllImport("user32")]
internal static extern bool ShowCaret(IntPtr hWnd);
private void comboBox1_MouseUp(object sender, MouseEventArgs e)
{
comboBox1.Select(0, 0);
IntPtr EditHandle = FindWindowEx(comboBox1.Handle, IntPtr.Zero, "Edit", null);
HideCaret(EditHandle);
}

how can minimize and maximize window folders in C#

I'm trying to minimize window folders and applications with voice ( speech recognition ) and this is my code :
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern bool SetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
private struct POINTAPI
{
public int x;
public int y;
}
private struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
private struct WINDOWPLACEMENT
{
public int length;
public int flags;
public int showCmd;
public POINTAPI ptMinPosition;
public POINTAPI ptMaxPosition;
public RECT rcNormalPosition;
}
private void button1_Click(object sender, EventArgs e)
{
WindowAction_MinimizeNotepad();
}
void WindowAction_MinimizeNotepad()
{
System.IntPtr app_hwnd;
WINDOWPLACEMENT wp = new WINDOWPLACEMENT();
app_hwnd = FindWindow("chrome", null);
GetWindowPlacement(app_hwnd, ref wp);
wp.showCmd = 2;
SetWindowPlacement(app_hwnd, ref wp);
}
i know how to use that code with button but i don't know how to use it with speech recognition so, how can i minimize windows with voice ? thanks.
First You need to Register for speech recognition event like:
On Page load
// Register handler for the SpeechRecognized event.
recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(srEvent_SpeechRecognized);
Then create a speech recognition event handler, Like :
void srEvent_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
You may want to have a look at Microsoft.Speech.Recognition.SpeechRecognitionEngine.
Using this you can subscribe to events and proceed with whatever action you want to perform on those events.
Get more information at https://msdn.microsoft.com/en-us/library/hh378426(v=office.14).aspx

Customize Save file dialog to be similar to underlying OS save dialog C#

I have been using this example to customize the save dialog,
http://www.codeproject.com/Articles/19566/Extend-OpenFileDialog-and-SaveFileDialog-the-easy
This works well and I could customize the dialog too. However, I see that the customized dialog does not follow the underlying windows style. For example, If I am in Windows 7 the dialog would look like this,
This is a save dialog from word and it does have few options like tags and stuff. But the look and feel is same as OS save dialog.
However, the custom save dialog with the above mentioned link would look like this,
Why would it not follow what OS offers? Is there any way to handle this?
Ok, I researched a bit and got to the point where I can use CommonSaveFileDialog from Microsoft.WindowsAPICodePack.Dialogs and create the underlying Save dialog ( which does match with Windows 7 style. ). I installed the WindowsAPI shell package and used the CommonSaveFileDialog control to create something like this,
The controls marked in red are actually CommonFileDialogLabel / CommonFileDialogTextBox / CommonFileDialogComboBox etc which are provided in those API. But now my question is how do I add a user control / custom control to this? I need full control over what I add so it could be a user control. Any idea.. Please help Thanks.
The suggested solution works as described:
The Save As file dialog (used in this example) is associated to a User Control, called CustomSaveFileDialog. It has the advantage that it is present in the Toolbox, and that it implements automatically the IDisposable interface. However, it could have also been a simple C# class.
This control has a constructor accepting an arbitrary application specific User Control hosting all the elements which are to show up in the File Dialog. When I got the question right, that is what is required.
The CustomSaveFileDialog has the following properties:
Accepting arbitrary User Controls Docked to the bottom of the File
Dialog, i.e. they follow the resizing of the File Dialog
No special behaviour for the additional elements (buttons, images,
checkboxes etc) is necessary. They act quite normally, as in other
windows.
This is the code of the described class:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace customSaveFileDialog
{
public partial class CustomSaveFileDialog : UserControl
{
//https://stackoverflow.com/questions/9665579/setting-up-hook-on-windows-messages
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
const uint WINEVENT_OUTOFCONTEXT = 0;
[DllImport("user32.dll")]
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr
hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess,
uint idThread, uint dwFlags);
[DllImport("user32.dll")]
private static extern bool UnhookWinEvent(IntPtr hWinEventHook);
[DllImport("user32.dll")]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int w, int h, bool repaint);
private struct RECT { public int Left; public int Top; public int Right; public int Bottom; }
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, out RECT rc);
[DllImport("kernel32.dll")]
private static extern uint GetLastError();
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hwndChild, IntPtr hwndNewParent);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr GetParent(IntPtr hWnd);
private IntPtr hDlg; // Save As dialog handle
private IntPtr hHook; // Event hook
private IntPtr hCtrl; // App. specific user control handle
UserControl ctrl; // App. specific user control
//Static variable containing the instance object
private static CustomSaveFileDialog customSaveFileDialog;
//public property for the user
//theSaveFileDialog has been added to the control in the designer from the Toolbox
public SaveFileDialog Dlg { get { return theSaveFileDialog; } }
//Event hook delegate
private static WinEventDelegate procDelegate = new WinEventDelegate(WinEventProc);
/// <summary>
/// Constructor
/// </summary>
/// <param name="ctrl">The User Control to be displayed in the file dialog</param>
public CustomSaveFileDialog(UserControl ctrl)
{
InitializeComponent();
customSaveFileDialog = this;
this.ctrl = ctrl;
hCtrl = ctrl.Handle;
//Setup Hook; for simplicity, hook all possible events from the current process
hHook = SetWinEventHook(1, 0x7fffffff, IntPtr.Zero,
procDelegate, (uint)Process.GetCurrentProcess().Id, 0, WINEVENT_OUTOFCONTEXT);
}
// Hook function
static void WinEventProc(IntPtr hWinEventHook, uint eventType,
IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
CustomSaveFileDialog csfdg = customSaveFileDialog;
if (csfdg.hDlg == IntPtr.Zero)
csfdg.hDlg = FindWindowEx(IntPtr.Zero, IntPtr.Zero, "#32770", "Save As");
if (hwnd == csfdg.hDlg)
{
IntPtr hParent = GetParent(csfdg.hCtrl);
//this is done only once
if (!(hParent == csfdg.hDlg))
SetParent(csfdg.hCtrl, csfdg.hDlg); //Bind the user control to the Common Dialog
RECT cliRect;
GetClientRect(csfdg.hDlg, out cliRect);
//Position the button in the file dialog
MoveWindow(csfdg.hCtrl, cliRect.Left + 130, cliRect.Bottom - 55, 500, 60, true);
}
}
}
}
The essential part is the hooking of the windows events. This has been taken from that post.
It may be noted that the "FindWindowEx" function (in the WinEventProc) finds all Common Dialogs (and probably more) with a title of "Save As". If this should be a problem, more filtering would be necessary, e.g by searching in the current thread only. Such a search function may be found here.
Additionally (not shown in the above code) the "Dispose" method in CustormSaveFileDialog.desinger.cs contains the Unhook function with the hHook handle as the parameter.
The software has been tested in Windows7 in Debug mode. As a test, a simple Forms window with a button has been implemented:
//Test for the customized "Save As" dialog
private void button1_Click(object sender, EventArgs e)
{
//Arbitrary User Control
myUserControl ctrl = new myUserControl();
using (CustomSaveFileDialog csfdg = new CustomSaveFileDialog(ctrl))
{
csfdg.Dlg.FileName = "test";
//Show the Save As dialog associated to the CustomFileDialog control
DialogResult res = csfdg.Dlg.ShowDialog();
if (res == System.Windows.Forms.DialogResult.OK)
MessageBox.Show("Save Dialog Finished");
}
}
And - as well as a test - the applicatioin specific user control handles the following events:
using System;
using System.Windows.Forms;
namespace CustomFile
{
public partial class myUserControl : UserControl
{
public myUserControl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button Clicked");
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MessageBox.Show("Image Clicked");
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (!checkBox1.Checked)
pictureBox1.Visible = false;
else
pictureBox1.Visible = true;
}
}
}
The following output is produced:
The next picture shows another screenshot, File Dialog resized, and the checkbox to display the image is unchecked.

Focus form on key down

I'm trying to use the youtube embedded player ( axshockwaveflash) and make a nice program out of it.
Thing is i'm trying to implement a button that'll replay/next/previous the current video.
what I have atm is:
private void btReplay_Click(object sender, EventArgs e)
{
if (!youtubePlayer.Focus())
{
youtubePlayer.Focus();
SendKeys.Send("0");
}
else
{
SendKeys.Send("0");
}
this.BringToFront();
}
The '0' key press makes the video replay from the start. Only it also makes the form dissappear between other open windows.
As you see i've tried using bringtofront but it won't work.
Any thoughts?
Also if anyone has any experience with this, i also want to play how to enable auto play the next video when using the 'END' key. I know about the autoplay=1 function but it won't seem to work when pressing the END key.
EDIT: Using WinForms btw
You didn't specify whether you use winForms or WPF. This snippet is for winforms.
Here I am giving you a static method that forces any given Form to front:
public static void bringWindowToFront(System.Windows.Forms.Form form)
{
uint foreThread = GetWindowThreadProcessId(GetForegroundWindow(), System.IntPtr.Zero);
uint appThread = GetCurrentThreadId();
const uint SW_SHOW = 5;
if (foreThread != appThread)
{
AttachThreadInput(foreThread, appThread, true);
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);
AttachThreadInput(foreThread, appThread, false);
}
else
{
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);
}
form.Activate();
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
private static extern bool BringWindowToTop(System.IntPtr hWnd);
[System.Runtime.InteropServices.DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern System.IntPtr GetForegroundWindow();
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern uint GetWindowThreadProcessId(System.IntPtr hWnd, System.IntPtr ProcessId);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool ShowWindow(System.IntPtr hWnd, uint nCmdShow);
this.BringToFront();
this.TopMost = true;
worked for me, silly i didnt think of this.

Hide TaskBar in WinForms Application [duplicate]

This question already has answers here:
How do I make a WinForms app go Full Screen
(10 answers)
Closed 9 years ago.
How can I hide the Windows taskbar when I run my C# WinForms application?
I tried some code, but it opens in maximized view with the taskbar.
Do you have any sample code or suggestions?
Just add this class into your project .it works as you expected.
using System;
using System.Runtime.InteropServices;
public class Taskbar
{
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
[DllImport("user32.dll")]
public static extern int FindWindowEx(int parentHandle, int childAfter, string className, int windowTitle);
[DllImport("user32.dll")]
private static extern int GetDesktopWindow();
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
protected static int Handle
{
get
{
return FindWindow("Shell_TrayWnd", "");
}
}
protected static int HandleOfStartButton
{
get
{
int handleOfDesktop = GetDesktopWindow();
int handleOfStartButton = FindWindowEx(handleOfDesktop, 0, "button", 0);
return handleOfStartButton;
}
}
private Taskbar()
{
// hide ctor
}
public static void Show()
{
ShowWindow(Handle, SW_SHOW);
ShowWindow(HandleOfStartButton, SW_SHOW);
}
public static void Hide()
{
ShowWindow(Handle, SW_HIDE);
ShowWindow(HandleOfStartButton, SW_HIDE);
}
}
USAGE:
Taskbar.Hide();
You need to set WinForms application from property like below
private void Form1_Load(object sender, EventArgs e)
{
this.TopMost = true;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
}
You need to use P/INVOKE
[DllImport("user32.dll")]
private static extern int FindWindow(string className, string windowText);
[DllImport("user32.dll")]
private static extern int ShowWindow(int hwnd, int command);
private const int SW_HIDE = 0;
private const int SW_SHOW = 1;
int hwnd = FindWindow("Shell_TrayWnd","");
ShowWindow(hwnd,SW_HIDE);
I hope that helps

Categories

Resources