How to override toolstrip renderer? - winforms

I am new to visual studio and c++, I have created a new CLR Empty Project and have started adding some controls. I notice this strange artefact on the right edge of the toolstrip and would like to remove it.
This question Why am I getting a vertical line on toolstrip? provides a solution in c# but I am having trouble trying to make it work in visual c++.
First i made new .cpp file called CustomerRenderer.cpp and the code looks like this:
using namespace System::Windows::Forms;
private ref class CustomRenderer : ToolStripRenderer
{
protected:
virtual void OnRenderToolStripBorder(ToolStripRenderEventArgs^ e) override
{
ToolStripRenderer::OnRenderToolStripBorder(e);
}
};
Then on my forms load I put this:
private: System::Void MainForm_Load(System::Object^ sender, System::EventArgs^ e) {
this->toolStrip1->Renderer = gcnew CustomRenderer();
}
And this just makes the toolstrip disappear completely rather than fix the edge artefact. I like the style of the gradient toolstrip so would rather not change to the system renderer.
How can I override the toolstrip renderer in a visual c++ project and remove the edge border artefact?
Thanks

Ok I solved by changing:
private ref class CustomRenderer : ToolStripRenderer
to
private ref class CustomRenderer : ToolStripProfessionalRenderer

Related

Where is ExecWB?

If I use WPF syntax:
<WebBrowser Grid.Row="1" Grid.Column="1" x:Name="htmlView"></WebBrowser>
Or if I add a WindowsFormsHost:
<WindowsFormsHost x:Name="formsHost" Grid.Row="1" Grid.Column="1">
</WindowsFormsHost>
And in the CS file:
public partial class MainWindow : Window
{
System.Windows.Forms.WebBrowser htmlView= new System.Windows.Forms.WebBrowser();
public MainWindow()
{
InitializeComponent();
formsHost.Child = htmlView;
htmlView.Navigate("d:\\test.xml");
}
private void menuFilePageSetup_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Do do");
}
}
Either way, the ExecWB method is not exposed. So I can't port this code over from my MFC C++ CHtmlView derived class:
ExecWB(OLECMDID_PAGESETUP, OLECMDEXECOPT_DONTPROMPTUSER, NULL, NULL);
Am I missing something? At this point in time I can't implement my print preview / page setup / zoom functionality because I can't use ExecWB.
I have tried deriving my own class from the Winforms browser but it is still not listed.
Thank you.
I don't understand why in this question:
How do I programmatically change printer settings with the WebBrowser control?
It refers to ShowPrintDialog but even that is not listed.
Partial success! I don't know why, but now, when I use the hosted Winforms version, I can atleast see some of the methods:
public partial class MainWindow : Window
{
System.Windows.Forms.WebBrowser htmlView= new System.Windows.Forms.WebBrowser();
public MainWindow()
{
InitializeComponent();
formsHost.Child = htmlView;
htmlView.Navigate("d:\\test.xml");
}
private void menuFilePageSetup_Click(object sender, RoutedEventArgs e)
{
htmlView.ShowPageSetupDialog();
}
private void MenuItem_Click(object sender, RoutedEventArgs e)
{
htmlView.ShowPrintPreviewDialog();
}
}
But ExecWb is still missing for managing things like zooming. I found this:
How do I print from the wpf WebBrowser available in .net 3.5 SP1?
One of the answers:
mshtml.IHTMLDocument2 doc = webBrowser.Document as mshtml.IHTMLDocument2;
doc.execCommand("Print", true, null);
But even if I add a reference to the mshtml library the compiler will not let me convert from HtmlDocument to IHtmlDocument2. Eventually I got this:
Clearing the selection in a webbrowser control
So now I know how to select all and copy to clipboard again (as long as I use the Winforms edition). However, having looked here:
https://msdn.microsoft.com/en-us/library/ms533049(v=vs.85).aspx
There seems to be no match for Optical Zoom:
ExecWB(OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vZoom, NULL);
The basic answer for calling ExecWb is to do this:
Use the WinForms WebBrowser control
Then something like htmlView.Document.ExecCommand
Pass in commands like:
SelectAll
Copy
for clipboard items.
There are native methods for displaying the print dialogues etc..
Only thing that does not seem to be supported is:
ExecWB(OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, &vZoom, NULL);

Is there any way to get MainWindow in custom control without using Application class?

I have one custom control which is placed inside the WPF Window,is there any possibility to get that WPF Window in Custom control and hook some events on that Window? without using Application class(ex Application.Current.Mainwindow)
Ahh... how about the Window.GetWindow method?:
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Window parentWindow = Window.GetWindow(this);
}
Note that it won't work in the constructor, but if you use the Loaded event, it works just fine.

WPF Control: where is "OnLoaded" virtual function?

In WinForm's control, there is an OnLoaded virtual function, but this seems to be missing in WPF control. I found this function very useful in some situations. For example, I could do something here after the control is "completely" initialized. In WPF control, there is an OnInitialized virtual function, but this function is called from InitializeComponent function which is too early and it doesn't allow derived class to setup. Is there any reason not to have this function in WPF? Or did I miss anything?
You can attach to the Loaded event of your Window object and do what you want to do inside the event handler (assuming you are using c#):
public MyWindow() //constructor
{
this.Loaded += MyWindow_Loaded;
}
private void MyWindow_Loaded(object sender, RoutedEventArgs e)
{
// do your stuff here
}
you will be looking for FrameworkElement.EndInit()
This will work after the initialization process of the Element...

How do i make a picturebox selectable?

I am making a very basic map editor. I'm halfway through it and one problem i hit is how to delete an object.
I would like to press delete but there appears to be no keydown event for pictureboxes and it will seem like i will have it only on my listbox.
What is the best solution for deleting an object in my editor?
You'll want the PictureBox to participate in the tabbing order and show that it has the focus. That takes a bit of minor surgery. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Implement the KeyDown event.
using System;
using System.Drawing;
using System.Windows.Forms;
class SelectablePictureBox : PictureBox {
public SelectablePictureBox() {
this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
}
protected override void OnMouseDown(MouseEventArgs e) {
this.Focus();
base.OnMouseDown(e);
}
protected override void OnEnter(EventArgs e) {
this.Invalidate();
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e) {
this.Invalidate();
base.OnLeave(e);
}
protected override void OnPaint(PaintEventArgs pe) {
base.OnPaint(pe);
if (this.Focused) {
var rc = this.ClientRectangle;
rc.Inflate(-2, -2);
ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
}
}
}
i think this is the best methode:
http://felix.pastebin.com/Q0YbMt22
... 8 years after ...
An alternative to Hans Passant's code, which doesn't require you to create a new class just so your PictureBox is in the tab order, is to set TabStop to true, and call SetStyle() directly on the PictureBox, optimally after InitializeComponent() is called.
TabStop is public, so it's easily set, but SetStyle() is protected, so reflection comes to the rescue!
myPictureBox.TabStop = true;
typeof(PictureBox)
.GetMethod("SetStyle", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(myPictureBox, new object[] { ControlStyles.Selectable, true });
This of course, doesn't do anything like getting the focus when the PictureBox is clicked, so you have to do that in its various events as you see fit.

textbox in winforms with sunken border

How do i set Border properties on a TextBox Control in Winforms so that It displays sunken borders?
Any ideas?
Thanks
Unusual request. But you can do it by selectively disable the theming for the control. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
class SunkenTextBox : TextBox {
protected override void CreateHandle() {
base.CreateHandle();
SetWindowTheme(this.Handle, "", "");
}
[System.Runtime.InteropServices.DllImport("uxtheme.dll")]
private static extern void SetWindowTheme(IntPtr hWnd, string appname, string idlist);
}
Just add the Microsoft Forms textbox Control to your toolbox.
alt text http://img262.imageshack.us/img262/2989/28550946.png
You need to remove the Application.EnableVisualStyles() call from your Program.cs file.

Resources