Thread: WinProc in separate file

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    WinProc in separate file

    I'm trying to move the WinProc from the winmain.cpp file into another cpp file(winproc.cpp) because it has become rather large. It didn't work so I am trying to move it with just the skeleton win32 app but I'm getting all sorts of syntax errors with that too. The basic windows program compiles fine when all the parts are in the same file(winmain.cpp), the problems occur(about 30 errors and warnings which weren't there before) when I start splitting it up into these parts. I'm using Visual Studio .NET and this is my first ever problem that I can't solve myself. If you need more information don't hesitate to ask.

    The files are;

    winmain.cpp

    Code:
    // Includes
    #include <windows.h>; // The Windows stuff
    #include <stdio.h>; // Some other useful stuff
    #include "winproc.h";
    
    // Defines
    #define WIN_32_LEAN_AND_MEAN // Cut the crap
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    // hInstance - handle to the instance of the window
    // hPrevInstance - Previous instance of the application
    // szCmdLine - Holds information about the command line calling procedure
    // iCmdShow - Holds the state of the window
    
    {
    	HWND hwnd; // The main window handle
    	MSG msg; // The message
    	WNDCLASSEX wndclass; // The window class
    
    	wndclass.cbSize        = sizeof(wndclass); // required information
    	wndclass.style         = CS_HREDRAW | CS_VREDRAW; // The styles of the window
    	wndclass.lpfnWndProc   = WndProc; // Pointer to the WinProc
    	wndclass.cbClsExtra    = 0;	// Useless crap
    	wndclass.cbWndExtra    = 0;	// Useless crap
    	wndclass.hInstance     = hInstance;	// Handle to the instance of the window passed by WinMain
    	wndclass.hIcon         = LoadIcon(NULL, IDI_WINLOGO); // Load the application icon
    	wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW); // Load the cursor
    	wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); // Set background white
    	wndclass.lpszMenuName  = NULL; // Menu stuff
    	wndclass.lpszClassName = "Window Class"; // The name of the class
    	wndclass.hIconSm       = LoadIcon(NULL, IDI_WINLOGO); // Loads the small icon at the top left of the window
    
    	RegisterClassEx(&wndclass); // Register the class
    
    	hwnd = CreateWindow("Window Class", // The name of the class again
    						"Win32App", // The title of the window
    						WS_OVERLAPPEDWINDOW, // Window style
    						CW_USEDEFAULT, // Initial x position - top left corner
    						CW_USEDEFAULT, // Initial y position - top left corner
    						CW_USEDEFAULT, // Initial x size
    						CW_USEDEFAULT, // Initial y size
    						NULL, // The parent window handle
    						NULL, // The menu handle
    						hInstance, // Handle to the previous instance
    						NULL); // Space for a struct - not required
    
    	ShowWindow(hwnd, iCmdShow); // This shows the window
    	UpdateWindow(hwnd); // Adds a WM_PAINT message to the message queue
    
    	while(1)
    	{
    		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
    		// &msg - address of msg, GetMessage() will fill in the msg structure
    		// PM_REMOVE - A flag to tell windows to remove the message once it has been passed
    		{
    			if(msg.message == WM_QUIT)
    				break;
    
    			TranslateMessage(&msg); // Translates the messages into something understandable
    			DispatchMessage(&msg); // Dispatches the message to the WinProc
    		}
    		else
    		{
    
    		}
    	}
    
    	UnregisterClass("Window Class", hInstance);	// Free up the memory used to register the class
    	return msg.wParam; // Return wParam of the message
    }
    winproc.h

    Code:
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // WndProc prototype
    winproc.cpp

    Code:
    #include "winproc.h"
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    		case WM_CREATE:
    			break;
    
    		case WM_SIZE:
    			break;
    
    		case WM_PAINT:
    		    break;
    
    		case WM_DESTROY:
    			PostQuitMessage(0); // Send the application the WM_QUIT message
    			break;
    	}
    
    	return DefWindowProc(hwnd, msg, wParam, lParam); // Sort out any unhandled messages
    }
    Thanks in advance!
    Last edited by ChrisWard1000; 09-10-2004 at 03:48 AM. Reason: Tidying up and adding source code

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    1. Remove the semi-colons from the end of your #include lines ie.
      Code:
      #include <windows.h> // <-- no semi-colon
      #include <stdio.h>   // <-- no semi-colon
      #include "winproc.h" // <-- no semi-colon
    2. #include <windows.h> in your winproc.h file.
    3. Put an include guard in your include files, eg.
      Code:
      //winproc.h
      #if !defined THIS_INCLUDE_FILE
      #define      THIS_INCLUDE_FILE
      
      #include <windows.h>
      
      LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // WndProc prototype
      
      #endif
    This thread from the faq board may be of some interest to you.

    For future reference, please post at least some of the errors you're getting as it helps us to help you.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    3
    Thanks for your help

    I have no idea why those semi colones were there, they weren't in my source file.

    It should have been obvious to me about the include being in the wrong place!

    I'll post the errors next time, thanks again for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  3. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  4. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM