Is it possible to change caret size for RichTextBox?
I want to change the Width of Caret.
you may try this.
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 richTextBox1_KeyUp(object sender, KeyEventArgs e)
{
CreateCaret(richTextBox1.Handle, IntPtr.Zero, 10, richTextBox1.Font.Height);
ShowCaret(richTextBox1.Handle);
}
}
Source
Related
It works on other winform programs, but not in the game itself.
I'm trying to make an extra menu or "mod menu" but it's invisible.
The button for message box to test if winform works. When I click "nowhere" in up left corner, it shows me the "hello" in the message box.
I tried editing the code but unable to find the problem. What should I do?
namespace myapp
{
public partial class Form1 : Form
{
[System.ComponentModel.Browsable(false)]
public bool AllowTransparency { get; set; }
[System.ComponentModel.Browsable(false)]
public bool AllowTransparencykey { get; set; }
[DllImport("user32.dll")]
public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public static int GWL_STYLE = -16;
public static int WS_CHILD = 0x40000000;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Process hostProcess = Process.GetProcessesByName("game").FirstOrDefault();
if (hostProcess != null)
{
Hide();
FormBorderStyle = FormBorderStyle.None;
SetBounds(0, 0, 0, 0, BoundsSpecified.Location);
IntPtr hostHandle = hostProcess.MainWindowHandle;
IntPtr guestHandle = this.Handle;
SetWindowLong(guestHandle, GWL_STYLE, GetWindowLong(guestHandle, GWL_STYLE) | WS_CHILD);
SetParent(guestHandle, hostHandle);
Show();
FormBorderStyle = FormBorderStyle.None;
SetBounds(0, 0, 0, 0, BoundsSpecified.Location);
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello");
}
}
}
I have some C# code that allows the user to control a LabVIEW VI from a C# Windows Forms application.
As of right now, when the user clicks on the "open dialog" button, it opens the VI in another separate, LabVIEW-styled window. What I would like, if possible, is to have that window open as a child form inside the parent.
Everything else works as far as the LabVIEW/C# interface. I just would like to have everything self-contained in one aesthetically pleasing window.
Here's Form1.cs:
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LabVIEW_DLL_Call
{
public partial class Form1 : Form
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("SharedLib.dll", CallingConvention = CallingConvention.Cdecl)]
static extern long Launch();
[DllImport("SharedLib.dll", CallingConvention=CallingConvention.Cdecl)]
static extern long SetParams(ushort signalType, double frequency, double amplitude);
[DllImport("SharedLib.dll", CallingConvention=CallingConvention.Cdecl)]
static extern long GetData(double[] Array, long len);
[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnLaunch_Click(object sender, EventArgs e)
{
Launch();
var hWnd = FindWindow("dialog.vi", null);
SetParent(hWnd, panel1.Handle);
}
private void btnSetParams_Click(object sender, EventArgs e)
{
SetParams((ushort)this.dropSignalType.SelectedIndex, (double)this.numFreq.Value, (double)this.numAmplitude.Value);
}
private void btnGetData_Click(object sender, EventArgs e)
{
int dataCount = 1000;
double[] results = new double[dataCount];
GetData(results, dataCount);
string txt = String.Join("\r\n", results);
this.textBox1.Text = txt;
}
}
}
Basically, what happens is that Form2 loads up within Form1, but it also generates the LabVIEW window. (And the second photo is one of the launch.vi)
That's a great start!
It could look something like this:
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
[DllImport("user32.dll", EntryPoint = "SetWindowPos")]
public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
private const int SWP_NOSIZE = 0x0001;
private async void btnLaunch_Click(object sender, EventArgs e)
{
bool foundIt = false;
DateTime timeOut = DateTime.Now.AddSeconds(10);
Launch();
do
{
await Task.Delay(250);
var hWnd = FindWindow(null, "dialog.vi");
if (!hWnd.Equals(IntPtr.Zero))
{
SetParent(hWnd, panel1.Handle);
SetWindowPos(hWnd, 0, 0, 0, 0, 0, SWP_NOSIZE);
foundIt = true;
}
}
while (!foundIt && (DateTime.Now <= timeOut));
if (!foundIt)
{
MessageBox.Show("Failed to find the LabView window.");
}
}
I am having a problem calling DefWindowsProc on Windows 8 from a C# winform. I have this form that I need it to be dragabble from anywhwere inside the form.
Here is my code.
[DllImport("user32.dll")]
static extern IntPtr DefWindowProc(IntPtr hWnd, uint uMsg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool ReleaseCapture(IntPtr hwnd);
const uint WM_SYSCOMMAND = 0x112;
const uint MOUSE_MOVE = 0xF012;
public void Drag()
{
DefWindowProc(this.Handle, WM_SYSCOMMAND, (UIntPtr)MOUSE_MOVE, IntPtr.Zero);
}
private void OnMainPanelMouseDown(object sender, MouseEventArgs e)
{
Control ctrl = sender as Control;
ReleaseCapture(ctrl.Handle);
this.Drag(); // put the form into drag mode.
}
DefWindowProc always return 0 yet I am unable to drag my window. This call works on XP, Vista and 7 but not on 8. I am guessing it has something to do with the decleration of DefWindowProc that is not working well on Windows 8.
Please note that on Windows 8 I am building my application with the .NET 4.0 framework yet on other platforms I am using the 2.0 version to build the software.
I achieved your functionality with a slightly different approach.
I'm running Windows 8 and Visual Studio 2012.
I tested my solution an all versions of the framework, versions 2, 3, 4, 4.5.
Of course your main form will need to trap the mouse_down() method.
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 253);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.mouse_down);
this.ResumeLayout(false);
}
First I defined a class called UnsafeNativeMethods:
[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods
{
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, uint Msg, int wParam, int lParam);
[DllImport("User32.dll", EntryPoint = "ReleaseCapture")]
public static extern bool ReleaseCapture();
}
Inside the form you wish to move with mouse from anywhere:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void mouse_down(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
UnsafeNativeMethods.ReleaseCapture();
UnsafeNativeMethods.SendMessage(Handle, UnsafeNativeMethods.WM_NCLBUTTONDOWN, UnsafeNativeMethods.HT_CAPTION, 0);
}
}
}
I re-wrote the example to see if the behavior was different with DefWindowProc(). It wasn't. The form will still dock to the edge when dragged beyond.
Nevertheless, here is the code:
[SuppressUnmanagedCodeSecurity]
internal static class UnsafeNativeMethods2
{
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int msg, int wParam, ref int lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32")]
public static extern int ReleaseCapture(IntPtr hwnd);
public const int WM_SYSCOMMAND = 0x112;
public const int MOUSE_MOVE = 0xF012;
}
Here is the code inside the form.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void mouse_down(object sender, MouseEventArgs e)
{
Control ctrl = sender as Control;
UnsafeNativeMethods2.ReleaseCapture(ctrl.Handle);
this.Drag(); // put the form into drag mode.
}
public void Drag()
{
UnsafeNativeMethods2.DefWindowProc(this.Handle, UnsafeNativeMethods2.WM_SYSCOMMAND, (IntPtr) UnsafeNativeMethods2.MOUSE_MOVE, IntPtr.Zero);
}
}
My DefWindowProc() is declared slightly differently from yours.
Bottom line, both approaches achieve the same result - the ability to drag a window from anywhere in a form.
I have a WinForms project. I have a panel on the top of my window. I want that panel to be able to move the window, when the user clicks on it and then drags.
How can I do this?
Add the following declerations to your class:
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HTCAPTION = 0x2;
[DllImport("User32.dll")]
public static extern bool ReleaseCapture();
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
Put this in your panel's MouseDown event:
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
}
is it possible for it to detect and differentiate cut,copy, or paste of files? I only can detect a change in the clipboard to far.
public partial class Form1 : Form
{
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SetClipboardViewer(IntPtr hWnd);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
private IntPtr _ClipboardViewerNext;
private const int WM_DRAWCLIPBOARD = 0x0308;
// private const int WM_CUT = 0x0301;
public Form1()
{
InitializeComponent();
}
private void StartClipboardViewer()
{
_ClipboardViewerNext = SetClipboardViewer(this.Handle);
}
private void StopClipboardViewer()
{
ChangeClipboardChain(this.Handle, _ClipboardViewerNext);
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_DRAWCLIPBOARD)
{
MessageBox.Show("Copied");
SendMessage(_ClipboardViewerNext, m.Msg, m.WParam, m.LParam);
}
else
{
base.WndProc(ref m);
}
}
private void Form1_Load(object sender, EventArgs e)
{
StartClipboardViewer();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
StopClipboardViewer();
}
}
No, but you could write a wrapper for the Clipboard (as it's a sealed class you can't derive from it) to keep track of the get/set operations.
The clipboard does not differentiate between cut and copy. It's a semantic difference in the way that the source application treats the data (or files).