• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gretty

[Win32 C++] Control Confusion

3 posts in this topic

Hello, Why wont this simple way I made of creating a Custom Control(in Win32 API) work when I want to use the class as so: - I will declare the custom control using CreateWindowEx(0,"updateBox",0,WS_VISIBLE|WS_CHILD,0,0,100,100,hwnd,NULL,hInstance,NULL); in my WinMain WndProc WM_CREATE: msg. - Will the updateBox object be created? Will the UpdateBoxWndProc() be created? Will the window receive messages? Is this the way to code custom controls? My code, any advice would be really helpful to tell me how to get this to work. I have commented where the errors occur. WinMain.cpp
#include <windows.h>
#include <cstdlib>

#include "updateBox.h"

// Global Variables
static HINSTANCE gInstance;

// Functions
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
        case WM_CREATE:
        {
             RegisterUpdateboxClass(); // Register custom control
             
             HWND updateB = CreateWindowEx(0,"updateBox",NULL,WS_CHILD|WS_VISIBLE|WS_BORDER,
                                           0,0,100,100,hwnd,NULL,gInstance,NULL);
                                           
             if (!updateB) {
                 MessageBox(hwnd,"Failed to create Update Box Control.","Error",MB_OK|MB_ICONERROR);
             }
        }     
        break;
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
    }
    return DefFrameProc(hwnd, NULL, msg, wParam, lParam); // Error Here I dont know what to put for the MDI (argument2) to pass the messages onto the custom control?}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
    // create dialog...I forgot the code to do it :P but here I open my main dialog
}




Custom Control Header file
#ifndef UPDATEBOX_H
#define UPDATEBOX_H

#include <windows.h>

void RegisterUpdateboxClass();

class updateBox {
      
      public:
           updateBox();
           LRESULT CALLBACK UpdateBoxWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
             
      private:
           POINT pos;     
};

#endif




Custom Control Implementation
#include <windows.h>

#include "updateBox.h"

using namespace std;

/// Register Custom Control Class ///
///                               ///
void RegisterUpdateboxClass() 
{
   // Post: Register custom class
   WNDCLASSEX wc;
   
   wc.cbSize        = sizeof(wc);  
   wc.lpszClassName = "updateBox";
   wc.hInstance     = GetModuleHandle(NULL);
   wc.lpfnWndProc   = UpdateBoxWndProc;    // THIS FUNCTION IS A MEMBER OF CLASS updateBox so I get an error here?
   wc.hCursor       = LoadCursor(NULL,IDC_ARROW);
   wc.hIcon         = NULL;
   wc.lpszMenuName  = NULL;
   wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_BTNFACE);
   wc.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
   wc.cbClsExtra    = 0;
   wc.cbWndExtra    = sizeof(updateBox*);
   wc.hIconSm       = NULL;
   
   if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Update Box Class Registration Failed", "Error",
            MB_ICONEXCLAMATION | MB_OK);
    }  
}

/// Update Box Control Class ///
///                          ///
updateBox::updateBox()
{
   // Post: Constructor
   
}

LRESULT CALLBACK updateBox::UpdateBoxWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
   // Post: Receives all UpdateBox window messages
   
   updateBox *udb = (updateBox*)GetWindowLong(hwnd,0);
   
   switch (msg) 
   {
         case WM_NCCREATE:
         {
              udb = (updateBox*)malloc(sizeof(updateBox));
              
              if (udb==false) {
                  MessageBox(NULL,"Failed to create Update Box window","Error",MB_OK|MB_ICONERROR);
                  return 0;
              }
              
              SetWindowLong(hwnd,0,(LONG)udb); // Attach custom class to this window.
         }
         break;
         case WM_PAINT:
         {
              HDC hdc;
              PAINTSTRUCT ps;
              hdc = BeginPaint(hwnd,&ps);
                
              HRGN foo = CreateRectRgn(0,0,100,100);
              FillRgn(...);
               
              EndPaint(hwnd,&ps);
         }    
         break;
         case WM_NCDESTROY:
              free(udb);
         break;
         default:
         break;
   }
   return DefWindowProc(hwnd, msg, wParam, lParam);
}




[Edited by - gretty on April 10, 2010 7:24:47 PM]
0

Share this post


Link to post
Share on other sites
If you want the window procedure to be a class member function, you are going to have to make it a static member function. As it is, updateBox::UpdateBoxWndProc() is implicitly expecting its first argument to be an updateBox* -- that is, the this pointer -- so its signature isn't a valid WndProc.

You could make a handleMessage() routine that isn't static then make updateBox::UpdateBoxWndProc() be static and have it get a pointer to the C++ object to which a message refers by calling GetWindowLong() and then have it call handleMessage() using that pointer.

I'm not sure why you're allocating an updateBox sized chunk of memory using malloc() and stuffing a pointer to it in the zeroth slot accessible by SetWindowLong, but unless you have a good reason to be doing it that way you should just use new and delete like normal and you probably meant to call SetWindowLong() using GWL_USERDATA? i.e. I think you might mean SetWindowLong(hwnd, GWL_USERDATA, static_cast<LONG>(new updateBox()));

Anyway that's one way of doing it. The other way is to have your custom class be the thing that calls CreateWindow in its constructor and then store the this pointer in the GWL_USERDATA slot.

[Edited by - jwezorek on April 9, 2010 8:52:35 PM]
0

Share this post


Link to post
Share on other sites
Quote:
Original post by jwezorek
If you want the window procedure to be a class member function, you are going to have to make it a static member function. As it is, updateBox::UpdateBoxWndProc() is implicitly expecting its first argument to be an updateBox* -- that is, the this pointer -- so its signature isn't a valid WndProc.

You could make a handleMessage() routine that isn't static then make updateBox::UpdateBoxWndProc() be static and have it get a pointer to the C++ object to which a message refers by calling GetWindowLong() and then have it call handleMessage() using that pointer.


I'm not sure why you're allocating an updateBox sized chunk of memory using malloc() and stuffing a pointer to it in the zeroth slot accessible by SetWindowLong, but unless you have a good reason to be doing it that way you should just use new and delete like normal and you probably meant to call SetWindowLong() using GWL_USERDATA? i.e. I think you might mean SetWindowLong(hwnd, GWL_USERDATA, static_cast<LONG>(new updateBox()));

Anyway that's one way of doing it. The other way is to have your custom class be the thing that calls CreateWindow in its constructor and then store the this pointer in the GWL_USERDATA slot.


Thanks for your reply :). The first part of what you said I kindof get but alot went over my head :P. Would you be able to give an example of how/what you mean referring to the handleMessage() routine?

I am malloc()'ing really because I was following a tutorial a read concerning making my own custom control but I like using new & delete instead, thanks for the advice :)

0

Share this post


Link to post
Share on other sites
I wouldn't recommend using GWLP_USERDATA to store your instance pointer. The user of the control (yourself maybe in months from now when you've forgotten) should be able to use the user data space of a control for something application specific.

When you register the window class, set cbWndExtra to something like sizeof(void*) then pop the instance pointer in there with


SetWindowLongPtr(Hw,0,reinterpret_cast<LONG_PTR>(ptr));


I tend to do this in response to the WM_CREATE message, allocating a structure that holds all the state for the custom control and popping it in, then grabbing it back in response to WM_DESTROY and deleting it.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0