NO

Author Topic: How do I define WinProc?  (Read 2793 times)

PabloMack

  • Guest
How do I define WinProc?
« on: March 15, 2019, 06:14:34 PM »
I am writing my first Win32 program in Pelles C. How do I define WindowProc?
This is the callback function that is passed to RegisterClass.

The definition I have used for Watcom is what is given in the MicroSoft website:

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

The 64-bit Pelles C compiler is giving me the following error message:

error #2168: Operands of '=' have incompatible types 'long long int __stdcall (*)(HWND, unsigned int, unsigned long long int, long long int)' and 'void *'.

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2091
Re: How do I define WinProc?
« Reply #1 on: March 15, 2019, 06:40:49 PM »
Working example:
Code: [Select]
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

TCHAR *szAppName = TEXT("WinFrame");
TCHAR *szFrameClass = TEXT("cWinFrame");
HWND hFrame;
HANDLE hInst;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wcx;
MSG msg;

wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = WndProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground= (HBRUSH)COLOR_APPWORKSPACE+1;
wcx.lpszMenuName = NULL;
wcx.lpszClassName= szFrameClass;
wcx.hIconSm = 0;

if (!RegisterClassEx(&wcx))
return 0;
hInst = hInstance;

hFrame = CreateWindowEx(0, szFrameClass, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInst, NULL);
if(!hFrame) return 0;
ShowWindow(hFrame, nCmdShow);
//UpdateWindow(hFrame);

while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg) {
// case WM_CREATE:
case WM_DESTROY:
PostQuitMessage(0);
return(0);
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
« Last Edit: March 15, 2019, 06:42:35 PM by TimoVJL »
May the source be with you

Jokaste

  • Guest
Re: How do I define WinProc?
« Reply #2 on: May 24, 2019, 01:11:05 PM »
Or replace
Quote
wcx.lpfnWndProc = WndProc;
with
Quote
wcx.lpfnWndProc = DefWindowProc;
if the window does nothing particular. You don't have to write a WndProc in that case.
« Last Edit: May 24, 2019, 01:12:57 PM by Jokaste »

Abraham

  • Guest
Re: How do I define WinProc?
« Reply #3 on: June 21, 2019, 07:24:18 AM »
Clearly, like TimoVJL showed without stating, you are missing your function prototype by having the function below WinMain.

I prefer to have WinMain at the bottom of my source code so that I do not have to deal with having prototypes. Even though a prototype is just a copy and paste of a single line.