C++/CLI - Textbox HideCaret - hide blinking cursor - winforms

Is there any method to use HideCaret().... or in general, just achieve something like described here:
Prevent Blinking Cursor in Textbox
but in a Managed C++ (clr) ?

If you're in C++/CLI, just call HideCaret directly, no need for DllImport.
#include <windows.h>
System::Windows::Forms::TextBox^ textbox1;
HideCaret((HWND)textbox1->Handle.ToPointer());

Related

VC++ calling a Paint event into a button event

I'm making a Windows form in VC++ and I've found tons of material talking about how to call Paint events into other functions or events, however those tend to be in C# and that doesn't seem to work for me or they don't explain the actual syntax of the code for me to understand. I'm still learning the more detailed aspects of programming so just telling me to use Validate() doesn't do me much good.
private: System::Void Stand_wheel_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
I have my Paint event. That has if statements that read if certain criteria are met to change the points that are used in the painting.
private: System::Void button3_Click_1(System::Object^ sender, System::EventArgs^ e)
And my button to click that I want to trigger the painting. I have tried using the Stand_wheel_Paint name and it is fraught with errors. I've tried almost everything to my knowledge to get it under the event in the button and I just can't. I also know that I have to 'erase' the old paint and redo the current work but from what I have found tends to almost get me there but is from C#. I would love any help that someone could offer.
using ( // <- I get an error on this parantheses
var g = Graphics.FromImage(Stand_wheel.Image))
{
e->Graphics->DrawLines(pen, points);
Stand_wheel.Refresh();
}
This looks close to a solution but still has errors.
The using block exists only in C#, a necessity because it doesn't have deterministic destruction. C++/CLI, like standard C++, does. If you declare a variable of a ref class type, rather than a tracking handle, its Dispose method will be automatically called when the variable goes out of scope.
No using needed, and the C++/CLI approach is much more powerful than the C# one. For example, C++/CLI deterministic destruction works with class member data as easily as locals.

How can I tell if a control is being visualized?

Is there a property (in WinForms) that allows me to tell whether a specific control is being visualized on the screen (even if partially)? That is,
its Visible property is true, and
it is not covered by other windows.
This is no longer possible on Vista and up with Aero enabled. Windows are visible in for example window thumbnails and Aero Peek, even if they are overlapped by other windows. Rely on Windows asking for paints, don't try to optimize them. And use Invalidate() if you have a reason to repaint, never paint directly.
Check to see if the Paint event is firing is probably your best bet.
private void myControl_Paint(object sender, PaintEventArgs e)
{
this.Text = "Painted at " + DateTime.Now.ToString();
}

Display an Adorner over a WebBrowser control

I'm using the System.Windows.Controls.WebBrowser for various things in my app and I've noticed that adorners are cut off when they are supposed to appear over a WebBrowser. I realize that the WebBrowser control is really a wrapper around a COM component and probably renders differently, but I wondered if anyone figured out how to solve this.
This is the problem I'm seeing. Here I have just a sample adorner that is supposed to draw a big red circle in the top corner of something (as a sample).
When I adorn the WebBrowser with this, I get this result:
I expect to see the full circle.
Here's the code for this worthless adorner, in case that is helpful:
public class SillyAdorner : Adorner
{
public SillyAdorner(UIElement element) : base(element)
{
}
protected override void OnRender(DrawingContext drawingContext)
{
drawingContext.DrawEllipse(new SolidColorBrush(Colors.Red), new Pen(), new Point(7, 7), 30, 30);
base.OnRender(drawingContext);
}
}
And here is how I apply it to the browser in the OnRender method of the host control:
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
var layer = AdornerLayer.GetAdornerLayer(browser);
layer.Add(new SillyAdorner(browser));
}
Anyone have any hacks or workarounds for this?
Edit: I'm using .NET 4.0, if that makes a difference.
Edit #2: WebBrowser appears to inherit from HwndHost, which I've seen another question or two regarding adorners and hwndsources, but I'm not seeing anything that looks like I could implement it for the WebControl, but hopefully this is useful information for someone.
I don't think that will work with an Adroner, but you can float content over a WebBroswer control using a transparent Popup control. More details and a code sample can be found here.
Here's my blog post introducing a library I wrote for layering a WPF adorner over any hwnd. A simple web browser demo looks like this:
This is caused by airspace issues. Since a WebBrowser is a native, non-WPF control, there is no way to directly render adorners (or other WPF content) on top of it.
In order to do this, you need to use a separate window of some sort, and put the content into that separate window. This typically means using a transparent WPF window layered over the top of your main window. Unfortunately, this will not be as integrated of a solution as a true native WPF control would provide.
Try use another transparent TOP LEVEL overlay window. Only supported on Windows 2000 or higher of course, Win9x does not have layered windows.

Need an easy WinAPI grid in C

I am looking for a grid control that can be controlled via WinAPI messages. I have tried the BabyGrid from CodeGuru but it is very slow when it comes to displaying on large screen.
Anyone know of a good one?
ListView with Report style is not enough ?
Use any of the native Win32 grids controls. Ask on Adv. Win32 group for sample code (C/Win32)
Yes, there are several built-in Win32 Grid controls (documented, MS, and undocumented)
C code had also been posted on IRC
(and apparently a kid is playing by removing answers from Microsoft experts (Shell team)... pathetic)
Use any of the native Win32 grids controls.
Ask on Adv. Win32 group for sample code (C/Win32)

How do I hide the input caret in a System.Windows.Forms.TextBox?

I need to display a variable-length message and allow the text to be selectable. I have made the TextBox ReadOnly which does not allow the text to be edited, but the input caret is still shown.
The blinking input caret is confusing. How do I hide it?
You can do through a win32 call
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public void HideCaret()
{
HideCaret(someTextBox.Handle);
}
When using the win32 call don't forget to hide the cursor in the textbox's GotFocus event.
Just for completeness, I needed such a functionality for using with a DevExpress WinForms TextEdit control.
They already do provide a ShowCaret and a HideCaret method, unfortunately they are protected. Therefore I created a derived class that provides the functionality. Here is the full code:
public class MyTextEdit : TextEdit
{
private bool _wantHideCaret;
public void DoHideCaret()
{
HideCaret();
_wantHideCaret = true;
}
public void DoShowCaret()
{
ShowCaret();
_wantHideCaret = false;
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
if (_wantHideCaret)
{
HideCaret();
}
}
}
To use the code, simply use the derived class instead of the original TextEdit class in your code and call DoHideCaret() anywhere, e.g. in the constructor of your form that contains the text edit control.
Maybe this is helpful to someone in the future.
If you disable the text box (set Enable=false), the text in it is still scrollable and selectable. If you don't like the visual presentation of a disabled text box (gray background usually) you can manually override the colors.
Be warned, manually overriding colors is going to make your form/control look weird on systems that do not use the default color/theme settings. Don't assume that because your control is white that everyone's control is going to be white. That's why you should always use the system colors whenever possible (defined in the System.Drawing.SystemColors enumeration) such as SystemColors.ControlLight.
I know this is an old thread but it is a useful reference.
I solved the problem with a much easier but very kludgie solution, which may depend on how much control you have over the user's access to the form. I added a textbox (any focus-able control) which I gave prime tabIndex value and then positioned it off-form so that it was not visible. This works fine on a dialog because the user can't resize. If the form is resizeable, this may not work.
As I said, a kludge - but a lot easier to set up. (BTW I found the HideCaret approach didn't work - but I didn't pursue it hard.)
AFAIK, this cannot be done. The TextBox control is a funny control because it actually has a lot of behaviour that can't be modified due to the way it taps into the operating system. This is why many of the cool custom TextBoxes are written from scratch.
I am afraid you may not be able to do what you wish to do :(

Resources