Example of setting italics in richtextbox with windows forms - c++

I'm looking for a complete, working example of how to set text to italics in a Windows Forms Rich Text Box, using C++.
All the examples are similar to the following:
System::Drawing::Font comment_font =
gcnew System::Drawing::Font(m_rich_text_script->SelectionFont,
System::Drawing::FontStyle::Italic);
m_rich_text_script->SelectionFont = comment_font;
I'm getting the error:
error C3673: 'System::Drawing::Font' : class does not have a copy-constructor
I believe the error is because I'm not including the header file for System::Drawing::Font.
I searched the web and stack overflow for "C++ Windows Forms italic" and none of them show the filename to include. Interestingly, the MSDN sites to not show the #include for C++.
I am using Visual Studio 2010 on a Windows CLR project on the Win7 platform.
Background: I moved the definition of methods from Form1.h to Form1.cpp and #included "Form1.hpp".

Try using a handle to a System::Drawing::Font object instead of a stack variable:
System::Drawing::Font^ comment_font = ...
Below is a complete C++ class that, given a rich text box, sets its selection font to italic:
namespace SetToItalic
{
public ref class SomeClass
{
public:
static void SetRichTextBoxSelectionFontToItalic(
System::Windows::Forms::RichTextBox^ textBox)
{
System::Drawing::Font^ comment_font =
gcnew System::Drawing::Font(
textBox->SelectionFont,
System::Drawing::FontStyle::Italic);
textBox->SelectionFont = comment_font;
}
};
}
I have verified this using the toolchain provided with Visual Studio 2013 by having a C# Windows Forms application call into a C++/CLI assembly with the above code.

Related

Open a Window in JUCE and put a PluginEditor into it

The tl;dr How do I open a window in Juce to display a plugin Editor?
I am following the Juce AudioProcessGraph tutorial at https://docs.juce.com/master/tutorial_audio_processor_graph.html which shows how to dynamically load AudioProcessor objects.
I would like to open their GUIs as they're loaded. This could be in separate windows or in the window that's got the slots for the graph.
I've tried creating a AudioProcessorEditor, but I'm at a loss on how to tell it to open.
Edit to add: What I've tried so far:
I've downloaded the code from the Tutorial: Cascading plug-in effects from https://docs.juce.com/master/tutorial_audio_processor_graph.html
I've also used ProJucer to create a new plugin. I've put the new plugin's files into the tutorial's files and used ProJucer to add PluginEditor.cpp and PluginEditor.h to the tutorial project.
I've changed the PluginEditor.h so the private class member audioProcessor is of type ProcessorBase&. And made ProcessorBase& the expected type for the data passed to the constructor.
In the tutorial code, in the header file, I've changed the ProcessorBase class so that the methods hasEditor() and createEditor() are just declarations and then modified main.cpp as follows:
#include "PluginEditor.h"
#include <iostream>
//=======================================================
juce::AudioProcessorEditor* ProcessorBase::createEditor()
{
std::cout "asking for an editor\n";
return new NewProjectAudioProcessorEditor (*this);
}
bool ProcessorBase::hasEditor() const
{ return true; }
In the included header, in the TutorialProcessor class, in the updateGraph() method, under the if (hasChanged), I've added slot->getProcessor()->createEditorIfNeeded (); on line 387-ish.
I can tell from my cout that createEditor() is being called and know that the constructor for the editor is running, but no window opens.
How do I tell it to open a window?
The code compiles and runs, but no window opens.

Convert Visual C++ code to Borland C++Builder

I have coded a program with Visual C++. But now I must put my code in a program which is coded with Borland C++Builder. I have a WebBrowser item on my form. In Visual C++, I write data to a textbox, get data from the textbox, and click a button on the WebBrowser with this code:
Write Data:
WebBrowser1->Document->GetElementById("okul_kod")->SetAttribute("value", TextBox2->Text);
Get Data:
textBox17->Text = WebBrowser1->Document->GetElementById("kay_cev")->GetAttribute("value");
Button Click:
WebBrowser1->Document->GetElementById("panelden_kayit")->InvokeMember("click");
I tried lots of things, and searched online, but I can't find how to convert this code to Borland C++Builder.
Can you please give me a clue or advice?
In C++Builder 6, its TCppWebBrowser VCL component is a thin wrapper for Internet Explorer's ActiveX control. Its Document property returns an IDispatch that you can use to gain access to IE's raw DOM interfaces directly (whereas Visual C++ appears to have wrapped those interfaces a little more nicely for you).
Try something like this:
#include <mshtml.h>
#include <utilcls.h>
// helpers for interface reference counting
// could alternatively use TComInterface instead of DelphiInterface
typedef DelphiInterface<IHTMLDocument3> _di_IHTMLDocument3;
typedef DelphiInterface<IHTMLElement> _di_IHTMLElement;
...
// Write Data:
_di_IHTMLDocument3 doc = CppWebBrowser1->Document;
_di_IHTMLElement elem;
OleCheck(doc->getElementById(WideString("okul_kod"), &elem));
if (elem) OleCheck(elem->setAttribute(WideString("value"), TVariant(Edit2->Text)));
// Get Data:
_di_IHTMLDocument3 doc = CppWebBrowser1->Document;
_di_IHTMLElement elem;
OleCheck(doc->getElementById(WideString("kay_cev"), &elem));
TVariant value;
if (elem) OleCheck(elem->getAttribute(WideString("value"), 2, &value));
Edit17->Text = value;
//Button Click:
_di_IHTMLDocument3 doc = CppWebBrowser1->Document;
_di_IHTMLElement elem;
OleCheck(doc->getElementById(WideString("panelden_kayit"), &elem));
if (elem) elem->click();

MFC Dialog Property Sheet usage produces errors in VS 2017, works on VS 2013

Have an existing Visual Studio 2017 MFC dialog app project where the controls are getting too dense and complicated to add more functionality.
I wanted to employ a tabbed dialog interface to group and simplify the control layout. Found some useful articles detailing the usage of Property Pages and Sheets written for Visual Studio 2015.
Created the needed property page resource(s) for two tabs to start and created the style sheet in my Aps' .cpp file. Compiles fine but produces a runtime exception when it tries to create the dialog box:
Unhandled exception at 0x7786C54F in Utlities.exe: Microsoft C++
exception: CResourceException at memory location 0x0040F430.
Stack window not very helpful for me.
Code segment:
CPropertySheet cSheet;
CConverter convTab; // class for first tab
CYield yieldTab; // class for second tab
cSheet.AddPage(&convTab);
cSheet.AddPage(&yieldTab);
m_pMainWnd = &cSheet;
INT_PTR nResponse = cSheet.DoModal(); // Exception thrown here
I noticed that the CPropertyPage class was apparently replaced by CMFCPropertyPage as CPropertyPage is no longer listed as a parent class in VS 2017.
I thought that it may be incompatible with CPropertySheet. I found that using CMFCPropertySheet complies fine but produces the same error. So I went through and manually derived my Property pages from CPropertyPage and also derived the sheet from CPropertySheet. Same error.
On a whim I fired up an older computer that still had Visual Studio 2013 loaded and tried that by deriving from CPropertySheet and CPropertyPages. It worked just fine.
At a loss as to why VS 2013 works but VS 2017 won't. Would hate to downgrade my 2017 installation to 2015 just for this.
Any ideas?
Had the same issue but managed to solve it. I'm in VS 2017.
My main code:
CMFCPropertySheet sheet;
CWizardPage1 page1; // class derived from MFCPropertyPage
CWizardPage2 page2; // class derived from MFCPropertyPage
sheet.AddPage(&page1);
sheet.AddPage(&page2);
sheet.DoModal();
This throws the same CResourceException at sheet.DoModal().
What I changed to make it work is in the CWizardPage1 constructor (and similar for CWizardPage2).
Constructor which results in an exception:
CWizardPage1::CWizardPage1()
{
}
Constructor which doesn't result in an exception:
CWizardPage1::CWizardPage1()
: CMFCPropertyPage(IDD_WIZARD_PAGE1)
{
}

Substitute fn for CWnd::SubclassCtl3d() that is supported in Visual Studio 2017's MFC

I'm porting an MFC application from VC6 (MFC42.dll) to Visual Studio 2017. Everything else built just fine except the calls to the fn CWnd::SubclassCtl3d(). I googled to get this fn's documentation but could not get anything useful.
With which API/member fn in VS2017's MFC I can replace the calls to this fn?
The descriptive comment about this calls says this:
// Since this window is really an edit control but does not
// have "edit" as its class name, to make it work correctly
// with CTL3D, we have to tell CTL3D that is "really is" an
// edit control. This uses a relatively new API in CTL3D
// called Ctl3dSubclassCtlEx.
and then the call is made like so:
pEdit->SubclassCtl3d(CTL3D_EDIT_CTL);
pEdit is a pointer to a CEdit object (and CEdit derives from CWnd).

CWinApp OpenDocumentFile "unsupported operation" error

Problem
I'm trying to open an MFC program that reads a Microsoft Access database (.mdb) and allows the user to view or modify the data. This is an existing program (and source code) given to me by a group in another lab where the program opens and works just fine.
In our lab, I have yet to see it load properly. When run, it pops up a dialog box that says, "Attempted an unsupported operation". Windows then offers me a chance to debug and such before it crashes.
Environment
In the other lab, they use Windows 7 and Microsoft Office 2010, and it works.
In our lab, I've tried Windows 7 with Office 2013 and Windows XP with Office 2010. The latter crashes without giving me the dialog box. I don't know if we have a Win7/MSO2010 machine.
The Function
I have the source code for the program. The solution file implies it was last developed in VS2010, which the computers I tested on had installed as well. Running it out of Visual Studio 2010 or straight from the executable yields the same results.
I have added additional debug dialog boxes to the code that narrow down the problem to this function call, which the code never gets past:
CwinApp:OpenDocumentFile(LPCTSTR lpszPathName)
The single string passed into the function is a path and filename for the MS Access database to be opened. It exists in a temporary directory created by another program. This is on a drive other than C, though I've tested some there, as well. Problems with programs related to this one often stem from files with "read only" status, but I continually check the temporary files created, and they are write-able.
Documentation
I found this information titled "Breaking Changes in Visual C++" for VS2010 through another SO question:
A new virtual function was added to the CDocTemplate class. This new virtual function is CDocTemplate::OpenDocumentFile. The previous version of OpenDocumentFile had two parameters. The new version has three parameters. To support the restart manager, any class derived from CDocTemplate must implement the version that has three parameters. For more information about the function, see CDocTemplate::OpenDocumentFile. The new parameter is bAddToMRU.
Code
I feel this might be the answer! But I don't have a strong idea of exactly what to change to get this to work. Here's where I stopped:
Program.cpp
CDocument* ProgramApp::OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToLRU, BOOL bMakeVisible)
{
// Add specialized code here and/or call base class
// Debug messages added
CDocument* tempDoc;
AfxMessageBox(lpszFileName);
tempDoc = CWinApp::OpenDocumentFile(lpszFileName, bAddToMRU);
AfxMessageBox("Opened database!");
return tempDoc;
}
Program.h
class ProgramApp : public CWinApp
{
public:
...
virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU, BOOL bMakeVisible);
afxwin.h
class CWinApp : public CWinThread
{
...
virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName);
virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU);
Changing the Program.cpp call to be from a "Template" class caused errors, but I wouldn't be surprised if that's towards the answer. This solution looks similar, but I'm not sure exactly what to do.
My hope is that this problem has a simple solution that someone more knowledgeable can give me. I would be much appreciative, and additional context would help a lot.
Edit: Debugging
I drilled down into the Windows code to see what was precisely going wrong. It seemed too dense to understand, but a coworker and I may have clues based on it. The failure message happens here:
dlgdata.cpp
// Could be a windowless OCX
pSite = m_pDlgWnd->GetOldControlSite(nIDC);
if (pSite == NULL)
{
TRACE(traceAppMsg, 0, "Error: no data exchange control with ID 0x%04x.\n", nIDC);
ASSERT(FALSE);
AfxThrowNotSupportedException();
}
...Although we are seeing debugger issues here:
occcont.cpp
COleControlSiteOrWnd *pemp = new COleControlSiteOrWnd(hwndCtrl, pOccDlgInfo->m_pItemInfo[i].bAutoRadioButton);
ASSERT(IsWindow(pTemp->m_hWnd));
if (IsWindow(pTemp->m_hWnd))
{
hwndStart = pTemp->m_hWnd;
...
My coworker believes this could have little to do with the opening of this document as I suspected and more to do with objects/controls we don't have on our lab computers trying to be used for the program.
I have faced the same problem in opendocumentfile(), there was a control in CFormView class which i was not using so i commented it out but forget to delete from .rc file. Once i remove the control entry from .rc file the problem disappear.
there should be no control variable uninitialize , please check that also