NO

Author Topic: WndProc in header file?  (Read 11040 times)

ruben

  • Guest
WndProc in header file?
« on: September 19, 2012, 08:49:35 PM »
Hi to all...
This is (i think ) first time that i post something here.
I have lost my old account long time ago but i am back.
I am not expert in C but im very familiar with windows API GUI programming and one
thing bothering me all the time.
Must say that i don't prefer OOP way of programming than regular procedural way.

Ok.
My question is ...
Is there a way to put main window procedure in header(include)  file and that
can be possible intercept messages from main program?
I have find many examples on net but all this examples using OOP aproach.
Why i as this ...because i want little bit simplify coding in C without constantly writing
wndProc(wnd,msg,wparam,lparam)

switch msg
...
case...
...
case...
....
case...

default:
return DefWinproc (wnd,msg,wparam,lparam)

I hope that you get point about what i'm talking.
Any help about this problematic...?
thanks advance!

Ruben

CommonTater

  • Guest
Re: WndProc in header file?
« Reply #1 on: September 19, 2012, 10:02:09 PM »
I would not suggest putting active code in a header file ... although the language won't stop you.

It might be better to build a "snippet" with your repeated progam skeleton in it... 
Tools -> Customize -> Snippets
 
Otherwise... you might like to try my approach which is to have a folder with your basic skeletons in it... Then just copy the appropriate file to your project's folder and add it to the project. 

 
« Last Edit: September 19, 2012, 10:06:14 PM by CommonTater »

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: WndProc in header file?
« Reply #2 on: September 20, 2012, 02:03:53 PM »
Example of simple window with some code in external files.
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <windowsx.h>
#include "AppFrame.h"

void OnDestroy(HWND hwnd) {
PostQuitMessage(0);
}

MESSAGEMAP_BEGIN(WndProc)
case WM_DESTROY: HANDLE_WM_DESTROY(hwnd, wParam, lParam, OnDestroy);
MESSAGEMAP_END

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)
{
HWND hWnd = AppInitMainWindow(WndProc, lpszCmdLine, nCmdShow);
return AppMainLoop();
}
Code: [Select]
// AppFrame.h
#define MESSAGEMAP_BEGIN(fn) \
LRESULT CALLBACK fn(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){switch(uMsg){

#define MESSAGEMAP_END \
}return DefWindowProc(hwnd, uMsg, wParam, lParam);}
// in AppFrame.c
HWND AppInitMainWindow (void *WndProc, LPSTR lpszCmdLine, int nCmdShow);
int AppMainLoop(void);
May the source be with you

CommonTater

  • Guest
Re: WndProc in header file?
« Reply #3 on: September 20, 2012, 02:32:54 PM »
Example of simple window with some code in external files.

All that to avoid typing two lines?  :D
 
Our friend could also try this...
 
1) Copy and paste the code below into a new editor window in POIDE, he might have to clean up some of the line breaks and stuff since copying from the web sometimes omits blank lines...
 
Code: [Select]
#define UNICODE
#define _UNICODE
#define WIN32_LEAN_AND_MEAN
#define WIN32_DEFAULT_LIBS
 
#include <windows.h>
 
#define CLASS_NAME  L"TEST"           // substitute active class name here
#define WIN_TITLE   L"Window Title"   // substitute actual window title
 
// poject global
HINSTANCE gInst;      // instance handle
HWND      hWin;       // main window handle
 
// page local
HACCEL hAccel = NULL; // keyboard accelerator table

// message tosser
LRESULT CALLBACK WindowTosser(HWND Win, UINT Msg, WPARAM Wparm, LPARAM, Lparm)
  {
     switch (Msg)
       {
 
          default :
             return DefWindowProc(Win, Msg, Wparm, Lparm);
       }
  }
 
// cold entry point
int APIENTRY wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpwszCmdline, int nCmdShow)
  {
    MSG Msg;
    WNDCLASS wc = {0};
   
    gInst = hInst;   // save for global use
 
    // create a window class
    wc.lpszClassName  = CLASS_NAME;
    wc.lpfnWndProc    = WindowTosser;
    wc.hInstance      = hInst;
    // more wc. parameters here
 
    if (! RegisterClass(&wc))
      return -1;
   
    // make first window
    hWin = CreateWindow(CLASS_NAME,WINDOW_NAME,
                        WS_VISIBLE | WS_OVERLAPPEDWINDOW,
                        100, 100, 100, 100, NULL, NULL, hInst, NULL);   
 

    // message tosser
    while(GetMessage(&Msg, NULL, 0, 0) != 0)
      if (! TranslateAccelerator(GetForegroundWindow(), hAccel, &Msg))
        if (! IsDialogMessage(GetForegroundWindow(), &Msg)
          {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
          }
       
    return Msg.wParam;
  } 

2) When it is in POIDE's editor go CTRL-A to select it all then copy it to the clipboard...
3) Now go Tools -> Customize -> Snippets -> New (the dotted square box icon)
4) Give the snippet a name such as "Windows Gui Skeleton"
5) Past the text into the snippet's text window.
6) Click OK.
 
Now when he's starting a new program all he has to do is open a new editor window and press CTRL-ALT-Space and insert his snippet. From there a few modifications to suit his program, add the necessary code and he's off to the races.  Unfortunately there's no way to avoid typing in the case lines, since that's going to be different for every program.
 
With a bit of time, other snippets can be used to create skeletal DLLs, Consoles, ASM source, etc.
 
 
 

 
« Last Edit: September 20, 2012, 03:12:59 PM by CommonTater »

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: WndProc in header file?
« Reply #4 on: September 20, 2012, 04:53:08 PM »
In OOP's you can define class templates and methods in header files then declare the instance of that code in the code file.
C programming doesn't allow to declare templates, so if you write some code in an include file that code will be recompiled in each source file where you included the .h file.
There is nothing strange simply that methodology is related to C++, and many other OOP's, but doesn't make much sense in plain C.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

ruben

  • Guest
Re: WndProc in header file?
« Reply #5 on: October 04, 2012, 07:44:57 PM »
Hi...
Thanks guys on very good respond!
timovjl example are on good track what i wont to do and have.
My next question will be ...
Did i can on similiar way create two window who can share same message loop
and if is possible how to do that?
thanks...

CommonTater

  • Guest
Re: WndProc in header file?
« Reply #6 on: October 04, 2012, 09:16:27 PM »
This might help ....  http://www.winprog.org/tutorial/

Just start on page one and follow it to the end. 


ruben

  • Guest
Re: WndProc in header file?
« Reply #7 on: October 04, 2012, 09:46:24 PM »
Unfortunately not, i have read this page many times it is all basic stuff and is ok but
nothing more then this.

CommonTater

  • Guest
Re: WndProc in header file?
« Reply #8 on: October 04, 2012, 10:08:37 PM »
Ruben, to be honest, that page is all there is to it. 

The usual result when someone tries to get fancy --especially out of laziness-- is failure.

C is not an OOP language and trying to make it into one is a terrible mistake, one that will result in nothing but wasted time as you have to go back and fix the messes you make.  You would be further ahead (by miles) to simply use the standard C methods and write basic, easily followed code.

You won't be the first to regret trying to get out of typing a few lines of code.



ruben

  • Guest
Re: WndProc in header file?
« Reply #9 on: October 04, 2012, 11:46:53 PM »
This is not about typing more or less code then it is about how
use standard C method to subclass window creating in no OOP way.
I hope that you get my point.
By the way termin subclass don't have nothing with OOP.
Interesting i have found on one member here ..i think alex ,his page with links to many
good pages and on one i read something about subclassing and superclassing mathod to
create multiple windows, but unfortunately without concrete example .
It is represented only how subclass controls not windows.
On the other side it is strange that i cannot found source code examples written in pellesC
with 2 or 3 window opened in same time ?

In first place i have search for some scripting things created in C .
I hope that im much clear now ...

 ruben

CommonTater

  • Guest
Re: WndProc in header file?
« Reply #10 on: October 04, 2012, 11:54:45 PM »
If you register a class... you can make as many windows in that class as you like... they will all share the same Message Tosser assigned by the class.  Your Message Loop is global to your application, one instance services all classes and windows in your application.

Subclassing and superclassing are a way of modifying the behaviour of an already working window or control.  For an example take a look in the ADDINS section at my articles on subclassing the IDE's editor windows and tracking window selection changes...  It may be a language thing, but I believe you are rather confused about subclassing and superclassing do... Do some searches on MSDN... It is explained very nicely ... HERE

There are not a lot of Pelles C specific code examples out there... that is because Pelles C is standard c (c-99 and c-11 to be exact) and you should be able to use just about an C language example in Pelles.
 
« Last Edit: October 05, 2012, 12:05:09 AM by CommonTater »

CommonTater

  • Guest
Re: WndProc in header file?
« Reply #11 on: October 05, 2012, 07:34:46 AM »
On the other side it is strange that i cannot found source code examples written in pellesC
with 2 or 3 window opened in same time ?

Here ya go... (Project attached)
Code: [Select]
// example of multiple windows in a single class...

#include <windows.h>
#define CNAME "WINDOWINGTEST"
HINSTANCE gInst;
 
// window factory
BOOL MakeWindow(void)
  {
    HWND pwind;
 
   //make top level window
    pwind = CreateWindow( CNAME,"Test Window",
                  WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                  CW_USEDEFAULT, CW_USEDEFAULT, 200, 200,
                  NULL, NULL, gInst, NULL);
 
    // add a couple of buttons
    CreateWindow("BUTTON", "Make A Window",
                  WS_CHILD | WS_VISIBLE,
                  10, 10, 150, 30,
                  pwind, (HMENU) 100, gInst, NULL);
    CreateWindow("BUTTON", "Exit",
                  WS_CHILD | WS_VISIBLE,
                  10, 40, 150, 30,
                  pwind, (HMENU) 200, gInst, NULL);
   
    return 0;
  }
 

// message tosser
LRESULT CALLBACK MsgTosser(HWND Win, UINT Msg, WPARAM WParm, LPARAM LParm)
  {
    switch (Msg)
      {
        case WM_COMMAND :
          switch(LOWORD(WParm))
            {
              case 100 :
                return MakeWindow();
              case 200 :
                PostQuitMessage(0);
              default :
                return DefWindowProc(Win, Msg, WParm, LParm);             
            }             
        default:
           return DefWindowProc(Win, Msg, WParm, LParm);
      }
  }
 
 

// cold entry point
INT WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR CmdLine, INT Show)
  {
    WNDCLASS wc = {0};
    MSG      msg;
 
    gInst = hInst;
 
    wc.lpszClassName  = CNAME;
    wc.lpfnWndProc    = MsgTosser;
    wc.hbrBackground  = GetStockObject(WHITE_BRUSH);
    wc.hInstance      = hInst;
 
    if (RegisterClass(&wc))
      {
        for(int x = 0; x < 3; x++)
          MakeWindow();
      }
    else
      {
        Beep(500,100);
        exit(0);
      }
       
    // message loop
    while (GetMessage(&msg, NULL, 0, 0) != 0)
      {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
      }
    return 0;
  }

 

 
« Last Edit: October 05, 2012, 03:02:02 PM by CommonTater »

ruben

  • Guest
Re: WndProc in header file?
« Reply #12 on: October 05, 2012, 08:02:13 PM »
Hi Common tater
Thanks for example , and that is exactly what i want
BUT problematic part is how control each new window with WndProcedure.
As you know when i click Exit then all windows are closed,right?

What i want is that you can close new window that old window stay opened.
Do you know how to do that for example with one window which is created when i doubleclick
exe ,then when i click on this first window 'new window' that new window is created and
when i want to close just this 'new' window that old stay .
any idea is welcome..

ruben

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: WndProc in header file?
« Reply #13 on: October 05, 2012, 09:11:07 PM »
What i want is that you can close new window that old window stay opened.
any idea is welcome..
Have you read links that CT tells to you ???
As programmer you have to know how many windows are created and when all windows are destroyed it is time to close application. So use global counter or FindWindow().
Code: [Select]
// example of multiple windows in a single class...
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define CNAME "WINDOWINGTEST"
HINSTANCE gInst;
int gcntWin;

// window factory
BOOL MakeWindow(void)
{
HWND pwind;

//make top level window
pwind = CreateWindow(CNAME, "Test Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, gInst, NULL);

// add a couple of buttons
CreateWindow("BUTTON", "Make A Window", WS_CHILD | WS_VISIBLE, 10, 10, 150, 30, pwind, (HMENU)100, gInst, NULL);
CreateWindow("BUTTON", "Exit", WS_CHILD | WS_VISIBLE, 10, 40, 150, 30, pwind, (HMENU)200, gInst, NULL);
gcntWin++;
return 0;
}

// message tosser
LRESULT CALLBACK MsgTosser(HWND Win, UINT Msg, WPARAM WParm, LPARAM LParm)
{
switch (Msg)
{
case WM_COMMAND:
switch (LOWORD(WParm))
{
case 100:
return MakeWindow();
case 200:
return DestroyWindow(Win);
}
break;
case WM_DESTROY:
if (!--gcntWin)
{
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(Win, Msg, WParm, LParm);
}

// cold entry point
INT WinMain(HINSTANCE hInst, HINSTANCE hPrev, PSTR CmdLine, INT Show)
{
WNDCLASS wc = { 0 };
MSG msg;

gInst = hInst;

wc.lpszClassName = CNAME;
wc.lpfnWndProc = MsgTosser;
wc.hbrBackground = GetStockObject(WHITE_BRUSH);
wc.hInstance = hInst;

if (!RegisterClass(&wc)) return 1;
for (int x = 0; x < 3; x++)
MakeWindow();

// message loop
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
« Last Edit: October 06, 2012, 10:48:54 AM by timovjl »
May the source be with you

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: WndProc in header file?
« Reply #14 on: October 05, 2012, 09:16:12 PM »
@ruben

Depending on the message you are investigating, the handle to the window or control is handed over as one of the parameters.
This should allow to close only the window that the user wants to close instead of the whole application.

Instead of PostQuitMessage use DestroyWindow.
« Last Edit: October 05, 2012, 09:40:11 PM by Stefan Pendl »
---
Stefan

Proud member of the UltraDefrag Development Team