today I tried to create a windowed application for the first time.I already had the code ready and it worked pretty well in the console (at least from the mathematical side :))
/I apologize in advance that I threw everything into one file, I think it's not very good, but the main thing for me was to check how the interaction with buttons and other things would work. And I will restore the beauty in the code later./ So that's the question itself. I moved my graph drawing function(if you comment out this function, then everything is compiled and calculations are performed correctly )and got the following errors:
Error LNK2019 reference to an unresolved external character "extern "C" void * __stdcall SelectObject(struct HDC__*,void *)" (?SelectObject@@$$J18YGPAXPAUHDC__@@PAX@Z) in the function "void __cdecl DrawGraph(int,float *,int,wchar_t const *,wchar_t const *,int,wchar_t const *,int,wchar_t const *)" (?DrawGraph@@$$FYAXHPAMHPB_W1H1H1@Z).
Error LNK2019 reference to an unresolved external character "extern "C" struct HPEN__ * __stdcall CreatePen(int,int,unsigned long)" (?CreatePen@@$$J212YGPAUHPEN__@@HHK@Z) в функции "void __cdecl DrawGraph(int,float *,int,wchar_t const *,wchar_t const *,int,wchar_t const *,int,wchar_t const *)" (?DrawGraph@@$$FYAXHPAMHPB_W1H1H1@Z).
at the same time, everything worked fine in the console.So I would like to know how to fix it.
And one more thing.Could you tell me how to display the graph in PictureBox?I would be very grateful to those who will help. I haven't figured out what to assign to what yet. there is a function, but I don't understand very well how to apply it yet.
#include "MyForm.h"
#include<math.h>
#include <conio.h>
#include <iostream>
#include <windows.h>
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void main(array<String^>^ args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Practform::MyForm form;
Application::Run(% form);
}
float MaxABSValue(float arr[], int size) {
float max = abs(arr[0]);
for (int i = 1; i < size; i++) {
if (abs(arr[0]) > max)
max = abs(arr[0]);
}
return max;
}
void DrawGraph(int steps, float* graphY, int zeroX, LPCWSTR minX, LPCWSTR maxX, int segStart, LPCWSTR segStartVal, int segEnd, LPCWSTR segEndVal) {
system("cls");
POINT op;
HWND hWnd = GetConsoleWindow();
HDC hDC = GetDC(hWnd);
HPEN graphPen = CreatePen(PS_SOLID, 2, RGB(0, 162, 232));
HPEN segmentPen = CreatePen(PS_SOLID, 2, RGB(237, 28, 36));
HPEN axisPen = CreatePen(PS_SOLID, 1, RGB(195, 195, 195));
HPEN projectionPen = CreatePen(PS_SOLID, 1, RGB(136, 0, 21));
SelectObject(hDC, axisPen);
float substepX = (640 / steps);
float maxY = MaxABSValue(graphY, steps);
MoveToEx(hDC, 0, 150, &op);
LineTo(hDC, 640, 150);
MoveToEx(hDC, zeroX * substepX, 0, &op);
LineTo(hDC, zeroX * substepX, 480);
TextOut(hDC, 632, 151, TEXT("X"), 1);
TextOut(hDC, 0, 151, minX, lstrlen(minX));
for (int i = 0; i < steps - 1; i++) {
int x1 = (i * substepX);
int y1 = -(graphY[i] / maxY) * (150);
int x2 = ((i + 1) * substepX);
int y2 = -(graphY[i + 1] / maxY) * (150);
if (i >= segStart && i <= segEnd)
SelectObject(hDC, segmentPen);
else
SelectObject(hDC, graphPen);
MoveToEx(hDC, x1, 150 + y1, &op);
LineTo(hDC, x2, 150 + y2);
if (i == zeroX) {
TextOut(hDC, (i * substepX) - 8, 151, TEXT("0"), 1);
TextOut(hDC, (i * substepX), 0, TEXT("Y"), 1);
}
if (i == steps - 2)
TextOut(hDC, ((i + 1) * substepX), 151, maxX, lstrlen(maxX));
if (i == segStart) {
if (y1 >= 0)
TextOut(hDC, ((i)*substepX), 134, segStartVal, lstrlen(segStartVal));
else TextOut(hDC, ((i)*substepX), 150, segStartVal, lstrlen(segStartVal));
SelectObject(hDC, projectionPen);
MoveToEx(hDC, x1, 150, &op);
LineTo(hDC, x1, 150 + y1);
}
if (i == segEnd) {
if (y2 >= 0)
TextOut(hDC, ((i + 1) * substepX), 134, segEndVal, lstrlen(segEndVal));
else TextOut(hDC, ((i + 1) * substepX), 150, segEndVal, lstrlen(segEndVal));
SelectObject(hDC, projectionPen);
MoveToEx(hDC, x2, 150, &op);
LineTo(hDC, x2, 150 + y2);
}
}
ReleaseDC(hWnd, hDC);
std::cin.get();
}
double _a, _b,_e;
double dihotom(double a, double b, double e, double(*fp)(double)) {
double c = 0;
double fc, fa = fp(a);
while (fabs(a - b) >= e) {
c = (a + b) / 2;
fc = fp(c);
if (fc == 0) {
break;
}
if (fa * fc <= 0) b = c; else a = c;
fa = fp(a);
}
return c;
}
double f(double x) {
//2x^3-11x^2-17x+115
return sin(x) - x + 0.15 - 4;
}
double f1(double x) {
return x * x - 5 * sin(x);
}
double f2(double x) {
return exp(x) - 2;
}
double f3(double x) {
return cos(1/100*x+exp(x));
}
double f4(double x) {
return x - pow(9 + x, 0.5) + x * x - 4;
}
double (*fcnPtr)(double x) = f1;
System::Void Practform::MyForm::обПрограммеToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show("","");
return System::Void();
}
System::Void Practform::MyForm::выходToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
return System::Void();
}
System::Void Practform::MyForm::func1_Click(System::Object^ sender, System::EventArgs^ e) {
cur_func->Text = "sin(x) - x + 0.15 - 4";
fcnPtr = f;
numericUpDown_a->Text="-4";
numericUpDown_b->Text = "-2";
}
System::Void Practform::MyForm::func2_Click(System::Object^ sender, System::EventArgs^ e) {
cur_func->Text = "exp^x -2";
fcnPtr = f2;
numericUpDown_a->Text = "0";
numericUpDown_b->Text = "1";
}
System::Void Practform::MyForm::func3_Click(System::Object^ sender, System::EventArgs^ e) {
cur_func->Text = "cos(1/100*x+exp(x))";
fcnPtr = f3;
numericUpDown_a->Text = "1";
numericUpDown_b->Text = "2";
}
System::Void Practform::MyForm::func4_Click(System::Object^ sender, System::EventArgs^ e) {
cur_func->Text = "x - (9+x)^(1/2)+ x * x - 4";
fcnPtr = f4;
numericUpDown_a->Text = "2";
numericUpDown_b->Text = "3";
}
System::Void Practform::MyForm:: button5_Click(System::Object^ sender, System::EventArgs^ e) {
_a = Convert::ToDouble(numericUpDown_a->Value);
_b = Convert::ToDouble(numericUpDown_b->Value);
_e = Convert::ToDouble(numericUpDown_e->Value);
double k1 = dihotom(_a, _b, _e, fcnPtr);
textBox1->Text = System::Convert::ToString(k1);
//textBox1->Text = System::Convert::ToString(sum);
}
System::Void Practform::MyForm::pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) {
}
System::Void Practform::MyForm::draw_Click(System::Object^ sender, System::EventArgs^ e) {
float graphY[60];
float substep = 0.25f;
float x = -7.5f;
int zeroX = 0;
int start, end;
for (int i = 0; i < 60; i++) {
double value = x + substep * i;
if (value == _a)
start = i;
if (value == _b)
end = i;
graphY[i] = (float)fcnPtr(value);
if (value == 0)
zeroX = i;
}
//DrawGraph(60, graphY, zeroX, L"-7.5", L"7.5", start, L"a", end, L"b");
pictureBox1->CreateGraphics();
}