How to modify a Button Content from a function (c++ UWP app)? - c++

Currently learning C++ and using Visual Studio 2017. My UWP app have 10 buttons (named b0-b9) and I want to create a function that will manage the content change of the buttons.
For this I need to pass the button name and the content. I want to modify to the function but I don't know how to do it.
It would look something like this:
void contentButtonChange(Button BtnName, String myString)
{
BntName->Content = myString;
}
Main()
{
.....
contentButtonChange(b0, string1);
contentButtonChange(b1, string2);
contentButtonChange(b2, string3);
.....
}
added note: I'm currently able to change the Content of the button from the Main but I'm unable to write a function that will accept a Button as a parameter. I'm always getting an error no matter what I try.
In the example above BtnName in the function is highlighted with the error: expression must have a pointer or handle type

I found how to do it. I need to add this to my function call:
Windows::UI::Xaml::Controls::Button^ btnName
like this:
void contentButtonChange(Windows::UI::Xaml::Controls::Button^ btnName, Platform::String^ myString)
{
bntName->Content = myString;
}
works now.

You need to use TextBlock to set to the Button.
void contentButtonChange(Button BtnName, string myString)
{
BntName->Content = new TextBlock() { Text = myString };
}

Related

const char* returns true in C++ CLI

since yesterday I have been struggling to turn text into label with code from another class, I came to this:
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
TestApp::UI_Error form("test", "test");
Application::Run(% form);
Using the above code, i display a winapi form that receives "test", "test" as 2x const char* on loading, the problem appears when im trying to set the text in labels using these variables
The code looks like this:
public:
UI_Error(const char* errorText, const char* errorCode)
{
InitializeComponent();
this->testLabel->Text = System::Convert::ToString(errorText);
}
For some reason, each time the return value shown in the win forms window is "true", although it should be "test" here, does anyone know the solution?
I tried to use std::string instead of const char*, unfortunately for some reason i get the error code that a static variable is required :(
Maybe I am wrong here, but System::Convert::ToString() seems not to have a method that accepts a const char* pointer. It looks like it gets cast to something else.
Try this instead:
Text = gcnew System::String(errorText);

How to get class object of a function in hooked function?

I recently started to work on application interception using madhookc hooking library.I am hooking the methods called in the application to do manipulation of text. At one point i got stuck and need your help.
I have a paragraph of text in which in which some text is Bold and some is regular style. Now i want to differentiate the regular and bold text and do different operations on both. I know that QFont::setFamily(QString) function is called for each line of text that is going to be displayed.So i hooked setFamily(). Now i am able to get font family of the text ,but all text of the paragraph (i.e. regular and bold) has same font family. Now i want to check whether this text is bold or not, so for that i need a object of QFont class so that i can call it's bold(); which returns true or false. Ultimately i want this pointer of the setFamily() function.(Same as like we get target in pointcut of AspectJ interception.)
void WINAPI newsetFamily( QString & family );
void (WINAPI *UnhooksetFamily)( QString & family );
void WINAPI newsetFamily ( QString & family )
{
QFont *font=this_pointer;
if(font->bold())
{
//do this
}
else
{
//do this
}
}
Please help me . Thanx in advance. . .
Finally i got my way !!!
After some research , i come to know that that current class object of a function is nothing but this pointer of the function, and this pointer can be exist only if the function belongs to some class.
I also came to know that QT class functions follows various calling conventions.The class i am referring follows the __fastcall for its functions. __fastcall calling convention requires first 2 hidden parameters to be passed to function if you want to hook it successfully. So i passed void * (void pointers) as first 2 parameters of the function and it worked.
Then i came to know that first void * pointer is nothing but this pointer of the function. That's how i got my target.
solved code:
void (__fastcall *UnhooksetFamily)( QFont *This,void *noUse,QString & family );
void __fastcall newsetFamily ( QFont *This,void *noUse,QString & family )
{
QFont *font=This;
if(font->bold())
{
//do this
}
else
{
//do this
}
}

Connecting a function to a tab in Wt

I am using the Wt library to write in C++ a website. I would like to use tabs on that website. To do so I use the WTabWidget.
In the documentation they make a tab and link each tab to a function :
Wt::WTabWidget *examples = new Wt::WTabWidget(this);
examples->addTab(helloWorldExample(), "Hello World");
examples->addTab(chartExample(), "Charts");
examples->addTab(new Wt::WText("A WText"), "WText");
Based on that I wrote this :
WTabWidget *myTab = new WTabWidget();
myTab->addTab(test(), "Test Tab");
But my compiler tells me :
error: cannot initialize a parameter of type 'Wt::WWidget *' with an rvalue of type 'void'
My "test" function has return type void. It's logic an rvalue of type void cannot be assigned to a parameter of type "WWidget*".
But if they show that example in the documentation, why can't I do it?
that : examples->addTab(chartExample(), "Charts");
Thanks for your help!
But if they show that example in the documentation, why can't I do it?
Their example function returns a widget, so you should do the same:
Wt::WWidget* test()
{
Wt::WText *text = new Wt::WText("This is a test tab text");
return text;
}

How to reference a form and change properties (height and width) from a function in Qt

I'm starting with Qt trying to migrate from VB6. And now I'm trying to change the size of a window (UI form) from a function, so before doing that in the action that opens the form I do this:
void F::on_actionCte_triggered()
{
Frm_ABM_Ctes *W = new Frm_ABM_Ctes(uF->mdiArea);
W->setAttribute(Qt::WA_DeleteOnClose);
W->setWindowState(Qt::WindowMaximized);
W->showNormal();
int Hi = (this->height()/3) - (W->height()/3);
int Wi = (this->width()/3) - (W->width()/3);
W->setGeometry(Wi,Hi,W->width(),W->height());
}
That works fine, the idea is that if I gonna do a lot of forms I want to call a function where it changes the geometry property of the child form. Like: Function(Parent,child) and then use Parent and Child as dynamic objects in my function (as I do in visual basic or VS)
So I did this:
void F::on_actionCte_triggered()
{
Frm_ABM_Ctes *W = new Frm_ABM_Ctes(uF->mdiArea);
W->setAttribute(Qt::WA_DeleteOnClose);
W->setWindowState(Qt::WindowMaximized);
W->showNormal();
FormS(This,W)
}
Where FormS is in a *.h file (which of course I include) and goes like this:
void FormS(QMainWindow Par, QMdiSubWindow Chi)
{
int Hi = (Par.height()/3) - (Chi.height()/3);
int Wi = (Par.width()/3) - (Chi.width()/3);
Chi.setGeometry(Wi,Hi,Chi.width(),Chi.height());
}
and it gives
error: could not convert 'this' from 'F* const' to 'QMainWindow'
FormS(this,W);
^
I don't know which is the best way to approach to my problem. Is there a way to create a public pointer and change any property of that form, or something like that?
Thanks for taking the time to read my problem any help will be appreciated.
First of all, you are trying to pass pointers into this method, so you'll need to adjust the method to take those pointers. Secondly, I'm not sure what F is (you haven't shown the declaration), but if it's a QMainWindow subclass this will work fine like so:
void FormS(QMainWindow *Par, QMdiSubWindow *Chi)
{
int Hi = (Par->height()/3) - (Chi->height()/3);
int Wi = (Par->width()/3) - (Chi->width()/3);
Chi->setGeometry(Wi, Hi, Chi->width(), Chi->height());
}

Using SetWindowText with the combination of two buffers

Alright so I have an edit control and a static text control. When the user clicks a button, I want the program to take the text from the edit control and add it to the text in the static text control and then set that new value as the text in the static control. So I have a function that appends the buffers to one another but my program doesn't seem to be working. This is what I have:
//when button message is recieved:
SendMessage(hwndEditControl, WM_GETTEXT,255,(LPARAM)editbuffer);
GetWindowText(hwndTextControl, (LPWSTR)allText, GetWindowTextLength(hwndTextControl));
allText = appendStrings((char*)editbuffer, (char*)allText);
SetWindowText(hwndTextControl, (LPCWSTR)allText);}
// where appendStrings is defined as:
char* appendStrings (char* buffer1, char* buffer2)
{
std::string string1(buffer1), string2(buffer2);
std::string string3 = string1 + string2;
return (char*)string3.c_str();
}
//and
static char* editbuffer = new char;
static char* allText = new char; //these are defined as so
So anyway, when I push the button, I'm pretty sure that the appendStrings function is working because I think it takes what is in the editbox and adds it to the Text box. The reason I say "i think" though is because the string in the text box is always just jibberish. Its sometimes random symbols or just these "l's" (or what look like "L"s). I think it's a problem with my pointers but I'm not sure. I'm new to this so if there is an easier way, please tell me.
You're returning a pointer to a temporary that's being destroyed before the function returns.
Instead of returning a char *, return a std::string.