Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Adaptor usb3.1gigabit vs Adaptor ...

La multi ani @Atreides!

La multi ani @KENSINGTON!

La multi ani @burebista!
 La multi ani de Florii!

Stihl fs 70 c-e

Challengers (2024)

Care mai sunt mediile de admitere...
 Laptop cu HDD atasare memorie MMC...

Hartile google nu mai au chenarul...

Tomate in ghiveci la curte?

Idei cale de actiune recuperare g...
 Intoleranța lactoza- vegan v...

Tobe acustice insonorizare in blo...

Cine canta? Fragment din melodie...

Tablou sigurante Dacia Sandero 2012
 

Cum declar doua clase in care se pot apela membrii celeilalte

- - - - -
  • Please log in to reply
4 replies to this topic

#1
mariusg7

mariusg7

    New Member

  • Grup: Candidate Members
  • Posts: 7
  • Înscris: 17.06.2022
Cum declar doua clase in care se pot apela membrii celeilalte?

Deci am incercat in headere App.h
#ifndef _APP_H_
#define _APP_H_
class API;
class APP {
public:
APP() {
g.winw=600; g.winh=500;
}
~APP() {}
void Test();
API *pAPI;
};
#endif

in APIwin.h
#ifndef _APIWIN_H_
#define _APIWIN_H_
#include <windows.h>
#include "App.h"
class API {
HFONT hfont;
public:
API(APP &rAPP_): rAPP(rAPP_) {
test=69;
}
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
HRESULT Create(HINSTANCE hInstance);
bool Close();
int test;
private:
HWND hWndMain, hWndChild;
HINSTANCE mHInstance;
APP &rAPP;
};
#endif

in surse App.cpp
#include "App.h"
#include "APIwin.h"
void APP::Test() {
char out[255];
sprintf(out, "APP::Test() %d", pAPI->test);
}

inAPIwin.cpp
#include "APIwin.h"
HRESULT API::Create(HINSTANCE hInstance) {
HWND hWnd; WNDCLASSEX wCs;
static const char* wndClassName = "WndClass";
wCs.cbSize	 = sizeof wCs;
wCs.style	 = CS_HREDRAW | CS_VREDRAW;
wCs.lpfnWndProc = WindowProc;
wCs.cbClsExtra = 0;
wCs.cbWndExtra = 0;
wCs.hInstance = hInstance;
wCs.hIcon	 = LoadIcon(hInstance, MAKEINTRESOURCE(ID_MYICON));
wCs.hCursor	 = LoadCursor(NULL, IDC_ARROW);
wCs.hbrBackground = NULL;
wCs.lpszMenuName = NULL;
wCs.lpszClassName = wndClassName;
wCs.hIconSm	 = LoadIcon(hInstance, MAKEINTRESOURCE(ID_MYICON));
if (RegisterClassEx(&wCs)) {
hWnd = CreateWindowEx(
	 WS_EX_CLIENTEDGE | WS_EX_ACCEPTFILES, // extended window style
	 wndClassName, // registered class name
	 TEXT("netMonitorPlugins"), // and window title
	 WS_OVERLAPPEDWINDOW, // window style
	 CW_USEDEFAULT, CW_USEDEFAULT, rAPP.g.winw, rAPP.g.winh,
	 NULL, NULL, hInstance, // handle to application instance
	 this	 // if need to take pointer
);
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LPARAM)this);
}
if(hWnd==NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return E_FAIL;
}
if (rAPP.g.winstate==3)
ShowWindow(hWnd, SW_MAXIMIZE);
else
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
mHInstance=hInstance;
hfont = CreateFont(15, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Arial");
SendMessage(hWnd, WM_SETFONT, (WPARAM)hfont, TRUE);
ChangeWindowMessageFilter (WM_DROPFILES, MSGFLT_ADD);
ChangeWindowMessageFilter (WM_COPYDATA, MSGFLT_ADD);
ChangeWindowMessageFilter (0x0049, MSGFLT_ADD);
return S_OK;
}
LRESULT CALLBACK API::WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if (msg == WM_CREATE) {
LPCREATESTRUCT lpcs = (LPCREATESTRUCT) lParam;
SetWindowLongPtr(hWnd, GWLP_USERDATA, (LPARAM) (lpcs->lpCreateParams));
}
API* pAPI = (API*) GetWindowLongPtr(hWnd, GWLP_USERDATA);
switch(msg) {
case WM_CREATE: {
	 pAPI->hWndMain=hWnd;
	 return 0;
}
case WM_CLOSE:
	 pAPI->Close();
break;
case WM_DESTROY:
	 KillTimer(hWnd, 0);
	 PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
bool API::Close() {
char className[50];
GetClassName(hWndMain, className, sizeof(className));
DestroyWindow(hWndMain);
UnregisterClass(className, mHInstance);
return true;
}

si in final Main.cpp
#include <windows.h>
#include "APIwin.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
int nAppResult = -1;
APP oAPP;
API oAPI(oAPP);
oAPP.Test();
if (SUCCEEDED(oAPI.Create(hInstance))) {
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
if (msg.message == WM_KEYDOWN) {
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
nAppResult = msg.wParam;
}
return nAppResult;
}


Se deschide fereastra dar este o problema cu API *pAPI; probabil e vorba de o alta instanta pentru ca test=69; nu este egal cu cel din APP::Test()

#2
mariusg7

mariusg7

    New Member

  • Grup: Candidate Members
  • Posts: 7
  • Înscris: 17.06.2022
Intre timp am gasit ce lipsea:

  API(APP &rAPP): oAPP(rAPP) {
	oAPP.pAPI = this;

Pentru ca pAPI sa fie pointer catre instanta din Main.cpp.

O alta intrebare: se poate face acelasi lucru fara pointerul pAPI? Deci in loc de pointer sa folosesc tot referinta API &rAPI; ?

#3
dani.user

dani.user

    Guru Member

  • Grup: Senior Members
  • Posts: 30,240
  • Înscris: 24.02.2007
Nu-mi dau seama de ce ai si APP si API. Poti si doar cu una.

#4
mariusg7

mariusg7

    New Member

  • Grup: Candidate Members
  • Posts: 7
  • Înscris: 17.06.2022
Se vede ca nu ai facut aplicatii mai complexe, in niciun caz crossover. Dupa ce faci, o sa intelegi de ce.

#5
MarianG

MarianG

    be that as it may

  • Grup: Moderators
  • Posts: 31,445
  • Înscris: 10.08.2005
Crossover ce ?
Vrei ca o aplicatie sa spioneze ce face alta aplicatie ?
Visual Studio 6 avea Spy++ cu tot cu codul sursa.

Edited by MarianG, 29 March 2023 - 08:48.


Anunturi

Bun venit pe Forumul Softpedia!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate