Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

winAPI - problem z subclassingiem

Ostatnio zmodyfikowano 2014-03-23 17:01
Autor Wiadomość
Rosumad
Temat założony przez niniejszego użytkownika
winAPI - problem z subclassingiem
» 2014-03-23 16:39:00
Otóż mam sobie taki kod:

C/C++
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure( HWND, UINT, WPARAM, LPARAM );

/*  Make the class name into a global variable  */
char szClassName[] = "WinBasket";
//  !!  --  Uchwyty  --  !!
HWND hText;
HMENU hMenu;
WNDCLASS edit2;
WNDPROC OldWndProc = WindowProcedure;
void ScreenResolution( int & poziom, int & pion )
{
    RECT desktop;
    const HWND hDesktop = GetDesktopWindow();
    GetWindowRect( hDesktop, & desktop );
    poziom = desktop.right;
    pion = desktop.bottom;
}

void AppResolution( int & szerap, int & wysap, HWND hwnd )
{
    RECT app;
    GetWindowRect( hwnd, & app );
    szerap = app.right - app.left - 14;
    wysap = app.bottom - app.top - 56;
}

int WINAPI WinMain( HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil )
{
    HWND hwnd; /* This is the handle for our window */
    MSG messages; /* Here messages to the application are saved */
    WNDCLASSEX wincl; /* Data structure for the windowclass */
    int poziom, pion, lewaap, goraap, szerap, wysap;
   
   
    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
    wincl.style = CS_DBLCLKS; /* Catch double-clicks */
    wincl.cbSize = sizeof( WNDCLASSEX );
   
    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon( NULL, IDI_APPLICATION );
    wincl.hIconSm = LoadIcon( NULL, IDI_APPLICATION );
    wincl.hCursor = LoadCursor( NULL, IDC_ARROW );
    wincl.lpszMenuName = NULL; /* No menu */
    wincl.cbClsExtra = 0; /* No extra bytes after the window class */
    wincl.cbWndExtra = 0; /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground =( HBRUSH ) COLOR_BACKGROUND;
   
    GetClassInfo( hThisInstance, "EDIT", & edit2 );
    edit2.lpszMenuName = NULL;
    edit2.lpszClassName = "EDIT2";
    edit2.hInstance = hThisInstance;
    edit2.lpfnWndProc = NowyProcesFunkcja;
   
    /* Register the window class, and if it fails quit the program */
    if( !RegisterClassEx( & wincl ) )
         return 0;
   
    if( !RegisterClass( & edit2 ) )
         return 0;
   
    hMenu = LoadMenu( hThisInstance, MAKEINTRESOURCE( 1000 ) );
    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx(
    0, /* Extended possibilites for variation */
    szClassName, /* Classname */
    "WinBasket", /* Title Text */
    WS_OVERLAPPEDWINDOW, /* default window */
    CW_USEDEFAULT, /* Windows decides the position */
    CW_USEDEFAULT, /* where the window ends up on the screen */
    700, /* The programs width */
    700, /* and height in pixels */
    HWND_DESKTOP, /* The window is a child-window to desktop */
    hMenu, /* No menu */
    hThisInstance, /* Program Instance handler */
    NULL /* No Window Creation data */
    );
   
    /* Make the window visible on the screen */ //
    hText = CreateWindowEx( WS_EX_DLGMODALFRAME, "EDIT", NULL, WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL, 0, 0, 700, 700, hwnd, NULL, hThisInstance, NULL );
    ShowWindow( hwnd, nFunsterStil );
   
    /* Run the message loop. It will run until GetMessage() returns 0 */
    while( GetMessage( & messages, NULL, 0, 0 ) )
    {
        AppResolution( szerap, wysap, hwnd );
        SetWindowPos( hText, HWND_TOP, 0, 0, szerap, wysap, SWP_SHOWWINDOW );
        /* Translate virtual-key messages into character messages */
        TranslateMessage( & messages );
        /* Send message to WindowProcedure */
        DispatchMessage( & messages );
    }
   
    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch( message ) /* handle the messages */
    {
    case WM_DESTROY:
        {
            PostQuitMessage( 0 ); /* send a WM_QUIT to the message queue */
            break;
        }
    case WM_COMMAND:
        {
            if( LOWORD( wParam ) == 999 )
                 PostQuitMessage( 0 );
           
            break;
        }
        default: /* for messages that we don't deal with */
        return DefWindowProc( hwnd, message, wParam, lParam );
    }
   
    return 0;
}

LRESULT CALLBACK NowyProcesFunkcja( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    switch( message ) /* handle the messages */
    {
    case WM_RBUTTONDOWN:
        {
            POINT point;
            point.x = LOWORD( lParam );
            point.y = HIWORD( lParam );
            ClientToScreen( hText, & point );
            TrackPopupMenu( GetSubMenu( hMenu, 1 ), 0, point.x, point.y, 0, hText, NULL );
        }
        default: /* for messages that we don't deal with */
        return WindowProcedure( hwnd, message, wParam, lParam );
    }
   
    return WindowProcedure( hwnd, message, wParam, lParam );
}

I problem jest taki, że gdy próbuję to skompilować, to mój kochany Dev-C++ twierdzi, że NowyProcesFunkcja nie jest w zasięgu WinMain, a dokładniej miejsca edit2.lpfnWndProc = NowyProcesFunkcja;, w którym to chcę przypisać klasie "edit2" obsługującą ją funkcję "NowyProcesFunkcja"... Proszę o pomoc, nie mam pojęcia dlaczego tak się dzieje.
Pozdrawiam

@edit Dorzucam log próby kompilacji

E:\C++\Programy\WinBasket\main.cpp In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':
62 E:\C++\Programy\WinBasket\main.cpp `NowyProcesFunkcja' undeclared (first use this function)
  (Each undeclared identifier is reported only once for each function it appears in.)
 E:\C++\Programy\WinBasket\main.cpp In function `LRESULT NowyProcesFunkcja(HWND__*, UINT, WPARAM, LPARAM)':
130 E:\C++\Programy\WinBasket\main.cpp `LRESULT NowyProcesFunkcja(HWND__*, UINT, WPARAM, LPARAM)' used prior to declaration
 E:\C++\Programy\WinBasket\Makefile.win [Build Error]  [main.o] Error 1
P-107086
Monika90
» 2014-03-23 17:01:23
Przenieś definicję NowyProcesFunkcja przed WinMain, albo dodaj deklarację tej funkcji przed WinMain.

Nie należy też wywoływać oryginalnej procedury okna bezpośrednio - użyj funkcji CallWindowProc.

EDIT: Zresztą, to co robisz jest nawet gorsze, bo wywołujesz tam procedurę okna głównego.
P-107087
« 1 »
  Strona 1 z 1