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();
Related
I'm having trouble making a simple message dialog in C++/WinRT. Something as simple as "You clicked this: press ok to continue" nothing fancy. In the standard Windows API you can simply write MessageBox() and new popup will show up where you can click ok, and you can do somthing similiar in C++/CX with
auto messageDialog = ref new Windows::UI::Popups::MessageDialog("You clicked on the button!"); messageDialog->ShowAsync();
I've tried using IMessageDialog and ContentDialog classes with no success and can't seem to find a single example out there on how to do it without getting into writing Xaml code which for my perticular project seems unnecisary for something as simple as a message box. Maybe I'm just not setting them up right? But then again there are no examples on how to set it up in the first place (in C++). What am I missing?
Thanks!
I was able to form a simple message dialog using this:
winrt::hstring Title = L"Hello";
winrt::hstring Content = L"Some cool content!";
winrt::Windows::UI::Popups::MessageDialog dialog(Content, Title);
dialog.ShowAsync();
Make sure to also include <winrt/Windows.UI.Core.h> so you can get access to the UI library.
And here is the result:
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).
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.
Is it possible to manipulate .net controls from native code? Particularly, I'd like to set the text of a TextBox from some native code. I want it done this way to keep the business logic separated from the user interface, but it is precisely the native code which only knows how to appropriately format the data (some bytes received within a protocol).
I know I could get the window handle through the Handle property, but once there, how would I call the desired method?
Is string handling my only option? Should the native code build up the message to be displayed on the control?
The "native" way of setting the text on a textbox is to send the textbox the WM_SETTEXT message:
// handle is initialized from TextBox::Handle
PostMessage(handle, WM_SETTEXT, 0, _T("Some text"));
Read up on PostMessage and SendMessage, too. Depending on how you allocate and build up the text, you may need to use SendMessage to avoid deallocating prematurely.
If you want to keep your business logic separated from your user interface, and some of your business logic is in native code, then it would be a really good idea not to change any GUI content from the native code directly, because it is exactly the opposite of what you want to achieve. Better try to write some kind of wrapper method in C++/CLI to call the native data format routine from there, making it available for your use in your GUI code.
If you really want to make calls from native code to managed code, you can declare a (native!) static method in C++/CLI which calls a managed method. You can pass a function pointer to such a method to a lower layer of your program (for example, your native C++ layer), which can be called from there.
Something along the lines of:
// the native code layer:
// "Callbacks.h"
void (*_myNativeCallback)();
void InitCallback(void (*myNativeCallback)())
{
_myNativeCallback=myNativeCallback;
}
// now you can use _myNativeCallback within your native code
// the C++/CLI layer:
#include "Callbacks.h"
//...
static void MyNativeCallback()
{
ManagedClass::StaticMethod();
}
InitCallback(MyNativeCallback);
In ManagedClass::StaticMethod, you have access to your .NET code and you should not have any problems to manipulate a TextBox of any form you can reach from there. If you have to convert a native string to a System::String, you may find this code snippet helpful:
inline System::String StdToSysString(const std::string &s)
{
return gcnew System::String(s.c_str());
}
And the other way round (Ansi code page assumed):
inline std::string SystemToStdString(System::String ss)
{
using namespace System::Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(ss)).ToPointer();
std::string s = chars;
Marshal::FreeHGlobal(System::IntPtr((void*)chars));
return s;
}
I got a stupid question on ListView Control usage.
I created a Windows Form App in VS2005. No I dragged a ListView Control from the toolbox. I want to implement my code to show some content(including both columns and rows).
I know a little of MFC knowledge. I am not sure I must study the past MFC CListCtrol knowledge to implement my application or I can just study the System.Windows.Forms::ListView simply.
I found a good sample working with ListView (but wrote in C#). Can I translate the sample code from C# to C++ in VS2005? If I can. Could you please give me some suggestions?
using System;
using System.Windows.Forms;
using System.Drawing;
public class ListView1 : Form {
ListView listView = new ListView();
public ListView1() {
listView.Dock = DockStyle.Fill;
PopulateListView();
this.Controls.Add(listView);
this.ClientSize = new Size(400, 200);
}
private void PopulateListView() {
// Set the view to show details.
listView.View = View.Details;
// Add columns
listView.Columns.Add("Author",
-2,
HorizontalAlignment.Center);
listView.Columns.Add("Title",
-2,
HorizontalAlignment.Left);
listView.Columns.Add("Price",
-2,
HorizontalAlignment.Left);
// Add items
ListViewItem item1 = new ListViewItem("Steve Martin");
item1.SubItems.Add("Programming .NET");
item1.SubItems.Add("39.95");
ListViewItem item2 = new ListViewItem("Irene Suzuki");
item2.SubItems.Add("VB.NET Core Studies");
item2.SubItems.Add("69.95");
ListViewItem item3 = new ListViewItem("Ricky Ericsson");
item3.SubItems.Add("Passing Your .NET Exams");
item3.SubItems.Add("19.95");
// Add the items to the ListView.
listView.Items.AddRange(
new ListViewItem[] {item1,
item2,
item3}
);
}
public static void Main() {
ListView1 form = new ListView1();
Application.Run(form);
}
}
Actually you don't need that much of your previous knowledge of MFC to implement ListView. C++ under .NET (in layman terms means WinForm applications), you can almost seamlessly translate C# code to C++. If I understood your question correctly, what you need to do is to make sure how objects and properties are accessed in C++ if you are developing a winforms app. Like in C# if you have Object.function, in C++ you may need to write Object::function, this is just an example. Definitely you would need to some more in depth knowledge to run things seamlessly.