Thread: C++ Win32 no window, yes on task manager

  1. #1
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310

    Unhappy C++ Win32 no window, yes on task manager

    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;
    }
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You're not passing your window's handle to GetMessage().
    Devoted my life to programming...

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    Ignore that, because its wrong.

    As for your problem, why didn't you check the return of your showui function and use GetLastError() to see why it's failing? Hint: It's 1407 which means "Invalid window class", also the return from c_str() is only valid as long as you don't modify the std::string it came from. It being destroyed counts as being modified.

  4. #4
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    @adeyblue: Thanks, I modified the code so I pass the class string...thanks
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by adeyblue View Post
    Ignore that, because its wrong.
    Oh, yeah? Do you mean that window handle shouldn't be passed to GetMessage()?!
    Devoted my life to programming...

  6. #6
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    Quote Originally Posted by Sipher View Post
    Oh, yeah? Do you mean that window handle shouldn't be passed to GetMessage()?!
    Not necessary, according to msdn => GetMessage:
    A handle to the window whose messages are to be retrieved. The window must belong to the current thread.

    If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

    If hWnd is -1, GetMessage retrieves only messages on the current thread's message queue whose hwnd value is NULL, that is, thread messages as posted by PostMessage (when the hWnd parameter is NULL) or PostThreadMessage.
    Since my windows in the only in my current thread
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    I see... Man, you never know enough!
    Devoted my life to programming...

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sipher View Post
    I see... Man, you never know enough!
    Thing is, if you pass a window handle to GetMessage() it will only retrieve messages for that window... works like a charm in single window applications but not so handy if you have dialogs and other windows open...

    One filtering trick I sometimes use is...

    Code:
    GetMessage(GetForegroundWindow(),0,0);
    if I don't want messaging going on in background windows ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WIN32 Creating 3rd window failed Error 1407
    By C_Sparky in forum Windows Programming
    Replies: 4
    Last Post: 10-06-2009, 10:19 PM
  2. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. dont want to use all params
    By stallion in forum Windows Programming
    Replies: 2
    Last Post: 02-18-2003, 08:10 AM
  5. win32 apps compiled with GCC start console window!
    By Citrus538 in forum Windows Programming
    Replies: 5
    Last Post: 02-18-2002, 10:35 PM