PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [C++ Win] WndProc Funktionszeiger



Xato
04-01-2007, 16:22
Hi Leute, ich habe folgendes Problem:

Ich habe eine Klasse erstellt, zum erzeugen von Fenstern. Dem RegisterClass Aufruf muss man ja einen Zeiger auf die WncProc funktion mitgeben. Diese Funktion exisitiert außerhalb der Klasse und wird beim erzeugen eines Fensters mit angegeben. Aber irgendwie klappt das ganze nicht so, wie es soll.

winclass.cpp

#include <windows.h>
#include "winclass.h"


int Win::CreateWin(HINSTANCE hGivenInstance, WNDPROC feuer, PSTR szAppName,const char *szTitle, DWORD iProperties, int SizeX, int SizeY)
{
hInstance=hGivenInstance;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (*feuer);
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = szAppName;
wc.lpszMenuName = NULL;
RegisterClass(&wc);

hWnd = CreateWindow(szTitle,
szTitle,
iProperties, //WS_OVERLAPPED | WS_SYSMENU,
CW_USEDEFAULT,
CW_USEDEFAULT,
SizeX,
SizeY,
NULL,
NULL,
hInstance,
NULL);
return 0;
}


int Win::RunWin(void)
{
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}


winclass.h

class Win
{

MSG msg;
WNDCLASS wc;
HINSTANCE hInstance;

public:
HWND hWnd;
int Win::CreateWin(HINSTANCE hGivenInstance, WNDPROC WndProc, PSTR szAppName,const char *szTitle, DWORD iProperties, int SizeX, int SizeY);
int Win::RunWin(void);

void ShowWin(int iCmdShow)
{
ShowWindow(hWnd,iCmdShow);
};
int UpdateWin(void)
{
UpdateWindow(hWnd);
};
};


sandbox.cpp - Hauptdatei

#define STRICT

#include <windows.h>
#include "winclass.h"

const char szAppName[] = "Ein eigenes Fenster";

const UINT TimerSec = 1;
const UINT SizeX = 60;
const UINT SizeY = 80;

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
/*
HWND hWnd;
MSG msg;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszClassName = szAppName;
wc.lpszMenuName = NULL;

RegisterClass(&wc);

hWnd = CreateWindow(szAppName,
"Titelleiste",
WS_OVERLAPPED | WS_SYSMENU,
CW_USEDEFAULT, // /* X-Position auf dem Monitor
CW_USEDEFAULT, // /* Y-Position auf dem Monitor
SizeX, // /* Fensterbreite
SizeY, // /* Fensterhoehe
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hWnd, iCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}*/

Win MyWinX;

// MyWin=new Win(hInstance,WndProc,"Hallo Apfel");
MyWinX.CreateWin(hInstance,WndProc,"Hallo Apfel","Apfel Nr 1",WS_OVERLAPPED | WS_SYSMENU,100,100);
ShowWindow(MyWinX.hWnd,iCmdShow);
// MyWinX.ShowWin(iCmdShow);
MyWinX.UpdateWin();
return MyWinX.RunWin();
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static BOOL isActive;
static int iSec;
static int iMin;
static RECT rect;

switch (message)
{
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_SPACE:
{
isActive = !isActive;
if(isActive)
{
SetTimer(hWnd, TimerSec, 1000, NULL);
}
else
{
KillTimer(hWnd, TimerSec);
}
return 0;
}
case VK_BACK:
{
if (isActive)
{
isActive = FALSE;
KillTimer(hWnd, TimerSec);
}
iMin = iSec = 0;
InvalidateRect(hWnd, NULL, TRUE);
return 0;
}
case VK_ESCAPE:
{
SendMessage(hWnd, WM_CLOSE, 0, 0);
return 0;
}
}
return 0;
}
case WM_TIMER:
{
if (iSec < 59)
++iSec;
else
{
iSec = 0;
++iMin;
}
InvalidateRect(hWnd, NULL, TRUE);
return 0;
}

case WM_SIZE:
{
rect.right = LOWORD(lParam);
rect.bottom = HIWORD(lParam);
return 0;
}

case WM_PAINT:
{
const char szUeberschrift[] = "Der ASCII Zeichensatz";

PAINTSTRUCT ps;
HDC hDC;
hDC = BeginPaint(hWnd, &ps);
{
SIZE size;
char sTime[6];
int iLength;
iLength = wsprintf(sTime, "%i:%02i", iMin, iSec);
GetTextExtentPoint32(hDC, sTime, iLength, &size);
TextOut(hDC, rect.right / 2 - size.cx / 2, rect.bottom / 2 -
size.cy / 2, sTime, iLength);
}
EndPaint(hWnd, &ps);
return 0;
}

case WM_DESTROY:
{
if (isActive)
KillTimer(hWnd, TimerSec);
PostQuitMessage(0);
return 0;
}
}
return DefWindowProc(hWnd, message, wParam, lParam);
}




Das ganze ist zwar etwas wirr, aber ich hoffe, man kann durchsteigen, ich mach das noch schöner, wenns fertig ist:D

mfg
Xato