|
-
March 15th, 2010, 04:17 PM
#1
Api static LRESULT WndProc in class, Window appears, can't move Window
Hi Everyone,
I've read a lot of win32 api wrapper in c++, i don't have something like a pair list.
I only want to create a window in OOP, that i can move.... and change the WndProc
Funktion with inheriting.
I tried the solution of "Super KOKO" to get the pointer in the wm_create message.
The problem is, that I want to return my own virtual WndProc function.
But when i try to create the stuff with the getwindowlong and setwindowlong,
short, it doensn't work. I've tried to create the WNDCLASSEX in the class.....
When you look at the end i've there suggestions, where the problem could be
Code:
#ifndef _WINDOW_H_
#define _WINDOW_H_
#include <windows.h>
class Window {
public:
Window();
virtual ~Window();
operator::HWND();
virtual int setWindow(HWND hWnd,
WNDCLASSEX wc,
HINSTANCE hInstance,
LPSTR lpCmdLine, int nCmdShow);
virtual LRESULT WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ return DefWindowProc(hWnd, msg, wParam, lParam); }
//bool ShowWindow(hWnd, nCmdShow);
//bool UpdateWindow(hWwnd);
//getter
HWND getmhWnd();
WNDCLASSEX getmwc();
MSG getmmsg();
//setter
setmhWnd(HWND hWnd);
setmwc(WNDCLASSEX wc);
setmmsg(MSG msg);
protected:
WNDCLASSEX m_wc;
HWND m_hWnd;
MSG m_msg;
private:
static LRESULT CALLBACK _WndProc(HWND, UINT, WPARAM, LPARAM);
};
#endif
Code:
/*============================window.cpp===============================/*
*/
#include "window.h"
/*------------------------------KONSTRUKTOR----------------------------/*
*/
Window::Window() {
//m_hWnd = hWnd;
}
/*---------------------------------GETTER------------------------------/*
*/
HWND Window::getmhWnd() {
return m_hWnd;
}
WNDCLASSEX Window::getmwc() {
return m_wc;
}
MSG Window::getmmsg() {
return m_msg;
}
/*--------------------------------SETTER-------------------------------/*
*/
Window::setmhWnd(HWND hWnd) {
m_hWnd = hWnd;
}
Window::setmwc(WNDCLASSEX wc) {
m_wc = wc;
}
/*---------------------------PUBLIC FUNKCTIONS-------------------------/*
*/
Window::operator HWND() {
return m_hWnd;
}
int Window::setWindow(HWND hWnd, // dont need this, only an option
WNDCLASSEX wc, // dont need this, only an option
HINSTANCE hInstance,
LPSTR lpCmdLine, int nCmdShow) {
ZeroMemory(&m_wc, sizeof m_wc);
//Step 1: Registering the Window Class
m_wc.cbSize = sizeof(WNDCLASSEX);
m_wc.style = 0;
m_wc.lpfnWndProc = (WNDPROC)Window::_WndProc;
m_wc.cbClsExtra = 0;
m_wc.cbWndExtra = 0;
m_wc.hInstance = hInstance;
m_wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wc.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
m_wc.lpszMenuName = NULL;
m_wc.lpszClassName = "myWindowClass";
m_wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);/**/
if(!RegisterClassEx(&m_wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}/**/
// Step 2: Creating the Window
m_hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"myWindowClass",
classN,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 500, 500,
NULL, NULL, hInstance, this);
if(hWnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}/**/
return 0;
}
LRESULT Window::_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
Window* firstWindow = NULL;
if (msg == WM_CREATE) {
CREATESTRUCT* m_lParam = (CREATESTRUCT*)(lParam);
SetWindowLong(hWnd, GWL_USERDATA, (LONG)m_lParam->lpCreateParams);
if(!m_lParam->lpCreateParams) {
MessageBox(NULL, "kein Pointer wurde angelegt", "INFO", MB_OK);
}
/*firstWindow->setmhWnd(hWnd);*/
} else {
Window* firstWindow = (Window*) (GetWindowLong(hWnd, GWL_USERDATA));
}
/**/
if( firstWindow ) {
return firstWindow->WndProc( hWnd, msg, wParam, lParam );
} else {
//MessageBox(hWnd, "no own window", "INFO", MB_OK);
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
Window::~Window() {
if (m_hWnd != NULL) {
//delete theWindow;
::DestroyWindow(m_hWnd);
}
}
Code:
==============WINMAIN===============
Window firstWindow;
MessageBox(NULL, "Hier vor Error", "Error", MB_OK);
firstWindow.setWindow(hwnd, wc, hInstance, lpCmdLine, nCmdShow);
ShowWindow(firstWindow.getmhWnd(), nCmdShow);//hwnd, nCmdShow);//
UpdateWindow(firstWindow.getmhWnd());//hwnd);//
// Step 3: The Message Loop
while(GetMessage(&firstWindow.getmmsg(), NULL, 0, 0) > 0)
{
TranslateMessage(&firstWindow.getmmsg());//&Msg);//
DispatchMessage(&firstWindow.getmmsg());//&Msg);//
}
return firstWindow.getmmsg().wParam;//Msg.wParam;//
I don't know why the window doesn't react it was only drawn.
Maybe it's the GetMessage LOOP. or the link to the lpfnWndProc
IN THE window.cpp in the LRESULT Window::_WndProc(....)
there is a row with a MESSAGEBOX with the string "no own window"
when i commend it out, then i only get the MessageBox the whole time.
I ALSO have to exit my program always with the Task Manager.....
Sorry, when i get on your nerves, yeah many articles have been written about
these stuff. But i don't get it.......
Best regards
RiggiT
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|