I'm having problems with my code...the code doesn't show a window, but is in the taskbar manager list...any ideas
// CWnd32.h
Code:
#include <windows.h>
#include <string>
using std::string;
class CWnd32
{
public:
int ShowUI(const string &,int,int,int,bool);
BOOL InitInstance(HINSTANCE,const string &);
CWnd32();
virtual ~CWnd32();
private:
WNDCLASSEX m_wcx;
HWND hwnd;
protected:
static LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
LRESULT CALLBACK fakeWndProc(UINT,WPARAM,LPARAM);
};
// CWnd32.cpp
Code:
#include "Wnd32.h"
CWnd32::~CWnd32()
{
UnregisterClass(m_wcx.lpszClassName, m_wcx.hInstance);
if (hwnd) DestroyWindow(hwnd);
}
CWnd32::CWnd32()
{
ZeroMemory(&m_wcx,sizeof(WNDCLASSEX));
}
BOOL CWnd32::InitInstance(HINSTANCE _hInstance, const string &strClass)
{
m_wcx.cbSize = sizeof(WNDCLASSEX);
m_wcx.cbClsExtra = 0;
m_wcx.cbWndExtra = 0;
m_wcx.hInstance = _hInstance;
m_wcx.lpszClassName = strClass.c_str();
m_wcx.lpszMenuName = 0;
m_wcx.lpfnWndProc = CWnd32::WndProc;
m_wcx.style = CS_DBLCLKS|CS_HREDRAW|CS_VREDRAW;
m_wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
m_wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wcx.hbrBackground = GetSysColorBrush(COLOR_BTNFACE);
return RegisterClassEx(&m_wcx);
}
int CWnd32::ShowUI(const string &strTitle,int w,int h,int nShow,bool centre)
{
hwnd = CreateWindow(m_wcx.lpszClassName,strTitle.c_str(),WS_POPUPWINDOW|WS_CAPTION|WS_MINIMIZEBOX|WS_CLIPCHILDREN|WS_CLIPSIBLINGS,1,2,w,h,NULL,NULL,m_wcx.hInstance,this);
if (!hwnd) return -2;
ShowWindow(hwnd,nShow);
UpdateWindow(hwnd);
return 0;
}
LRESULT CALLBACK CWnd32::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
CWnd32 *self;
if (msg == WM_NCCREATE)
{
self = (CWnd32*)((LPCREATESTRUCT)lParam)->lpCreateParams;
SetWindowLong(hWnd,GWL_USERDATA,(LONG)self);
}
else
{
self = (CWnd32*)GetWindowLong(hWnd,GWL_USERDATA);
if(!self) return DefWindowProc(hWnd,msg,wParam,lParam);
}
self->hwnd = hWnd;
return self->fakeWndProc(msg,wParam,lParam);
}
LRESULT CALLBACK CWnd32::fakeWndProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CREATE:
{
return 0;
}
case WM_COMMAND:
{
return 0;
}
case WM_CLOSE:
{
DestroyWindow(hwnd);
return 0;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
// main.cpp
Code:
#include "Wnd32.h"
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
int ret_val = -1;
CWnd32 *ptr = new CWnd32;
if (ptr->InitInstance(hInstance,"my_damn_class"))
{
MSG Msg;
// show the main window
ptr->ShowUI("hola",150,150,nShowCmd,true);
while(GetMessage(&Msg,0,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
ret_val = Msg.wParam;
}
delete ptr;
return ret_val;
}