Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 24th, 2003, 05:25 PM
Authorized User
 
Join Date: Jun 2003
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Window Class problems

Hi all,
I am trying to write a simple program to do all the hard work when initialising a window. Basically I want to just type a few small lines of code to make a window appear. I have written a class to try and handle this but it always fails at the HWND creation. I don't understand why so any help would be very much appreciated.
Thanks
Dylan

firstly the class file
Code:
#include <windows.h>
#include "easywindow.h"

CEasyWindow::CEasyWindow(HINSTANCE hInstance)
: m_wc()
, m_hwnd()
, m_style(NULL)
, m_hIcon(NULL)
, m_hCursor(NULL)
, m_hbrBackground(NULL)
, m_lpszMenuName(NULL)
, m_lpszClassName(NULL)
, m_hIconSm(NULL)
, m_WindowName("WINDOW")
, m_hInstance(hInstance)
{
}

CEasyWindow::~CEasyWindow(void)
{
}

HWND CEasyWindow::Register(int nCmdShow)
{
    memset(&m_wc, 0, sizeof(m_wc));    // clear everything just to be safe
    m_wc.cbSize = sizeof(m_wc);
    m_wc.style = m_style;
    m_wc.lpfnWndProc = ProcessMessage;
    m_wc.cbClsExtra = 0;
    m_wc.cbWndExtra = 0;
    if(m_hIcon != NULL)
        m_wc.hIcon = LoadIcon(m_hInstance, m_hIcon);
    if(m_hIconSm != NULL)
        m_wc.hIconSm = LoadIcon(m_hInstance, m_hIconSm);
    if(m_hCursor != NULL)
        m_wc.hCursor = LoadCursor(NULL, m_hCursor);
    m_wc.hbrBackground = m_hbrBackground;
    m_wc.lpszMenuName = m_lpszMenuName;
    m_wc.lpszClassName = m_lpszClassName;
    RegisterClassEx(&m_wc);

    m_hwnd = CreateWindowEx(0, m_WindowName, m_WindowName, WS_POPUP | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME, 50, 50, 200, 100, NULL, NULL, m_hInstance, NULL);

   //@@@@@@@@@@@@@@@@@@@@@@ This code is always executed @@@@@@@@@@@
    if(!m_hwnd)
    {
        ::MessageBox(NULL, "CREATE WINDOW FAILED", m_WindowName, MB_ICONSTOP);
        exit(-1);
    }
    ShowWindow(m_hwnd, nCmdShow);
    return(m_hwnd);
}

LRESULT CALLBACK CEasyWindow::ProcessMessage(HWND m_hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
        // Add any case situations you want here
    case WM_PAINT:            // paint message
        PaintWindow(m_hwnd);
        return(0);

    case WM_CHAR:            // button pressed
        PostQuitMessage(0);
        return(0);
    }
    return(DefWindowProc(m_hwnd, uMsg, wParam, lParam));
}

void CEasyWindow::PaintWindow(HWND m_hwnd)
{
    PAINTSTRUCT ps;

    BeginPaint(m_hwnd, &ps);
    HDC hdc = ps.hdc;
    char buf[] = "Press a key to exit.";

    TextOut(hdc, 0, 0, buf, strlen(buf));
    EndPaint(m_hwnd, &ps);
}
and now the actual winmain function

Code:
#include <windows.h>
#include "EasyWindow.h"

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // create a window
    CEasyWindow MyWindow(hInstance);
    MyWindow.m_WindowName = "First Window";
    MyWindow.m_style = CS_HREDRAW | CS_VREDRAW;
    MyWindow.Register(nCmdShow);
}
and just in case you want to try and run it yourself here is the header file

Code:
#pragma once
#include <windows.h>

class CEasyWindow
{
public:
    CEasyWindow(HINSTANCE);
    virtual ~CEasyWindow(void);
    WNDCLASSEX m_wc;
    HWND m_hwnd;
    UINT m_style;
    LPCTSTR m_hIcon;
    LPCTSTR m_hCursor;
    HBRUSH m_hbrBackground;
    LPCTSTR m_lpszMenuName;
    LPCTSTR m_lpszClassName;
    LPCTSTR m_hIconSm;
    LPCTSTR m_WindowName;
    HINSTANCE& m_hInstance;

    HWND Register(int);
    static LRESULT CALLBACK ProcessMessage(HWND, UINT, WPARAM, LPARAM);
    static void PaintWindow(HWND);
};
Reply With Quote
  #2 (permalink)  
Old October 1st, 2003, 06:10 PM
Authorized User
 
Join Date: Jul 2003
Posts: 60
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to merediths
Default


try changing

LRESULT CALLBACK CEasyWindow::ProcessMessage(HWND m_hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

to

LRESULT CALLBACK ProcessMessage(HWND m_hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

(i.e. make it a global function). Being a member function is adding a hidden this pointer to the arglist, so your code actually compiles to something along the lines of ....

  ProcessMessage(CEasyWindow* this, UINT msg, WPARAM wParam, LPARAM lParam)

 and therefore it doesn't match the winproc prototype.

Regards,
Meredith Shaebanyan

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
SimpleMail class problems derrida BOOK: Beginning PHP5, Apache, and MySQL Web Development ISBN: 978-0-7645-7966-0 0 July 6th, 2006 05:52 AM
window.opener problems crmpicco Javascript How-To 2 September 30th, 2005 03:46 AM
Problems with Popup Window in IE kevorkian Javascript 2 June 22nd, 2005 10:08 AM
Chapter 5, Exercise 2, Class Problems nmstudent BOOK: Beginning Java 2 1 December 12th, 2003 07:08 PM
Splitter window problems rbevers Visual C++ 4 October 4th, 2003 05:05 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.