我在窗口过程中调用SetClassLong(hwnd,GCL_WNDPROC,(long)WndProc2);修改改了窗口的窗口过程为WndProc2
在WndProc2中仅仅调用了DefWindowProc,但是消息还是给我原来的窗口过程WndProc给截获了,是怎么回事?

解决方案 »

  1.   

    #include <windows.h>
    #include <stdio.h>
    #include "resource.h"LRESULT CALLBACK WndProc2(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
    LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam);
    char szMessage[128];
    HINSTANCE hInst;
    char classname[]="myclsname";int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
    {
    static char appname[]="My windows";
    WNDCLASS wc;
    MSG msg;
    HWND hwnd;
    hInst=hInstance;
    wc.cbClsExtra=0;
    wc.cbWndExtra=0;
    wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor=LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
    wc.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
    wc.hInstance=hInstance;
    wc.lpfnWndProc=WndProc;
    wc.lpszClassName=classname;
    wc.lpszMenuName=NULL;
    wc.style=CS_HREDRAW|CS_VREDRAW;
    if(!RegisterClass(&wc))
    return 0;
    hwnd=CreateWindow(classname,appname,WS_OVERLAPPEDWINDOW|WS_VSCROLL,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,nShowCmd);
    UpdateWindow(hwnd);
    // SetTimer(hwnd,123,10000,NULL);
    while(GetMessage(&msg,NULL,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }LRESULT CALLBACK WndProc(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    HDC hdc;
    PAINTSTRUCT ps;
    RECT rect;
    char chr[2];
    char str[40];
    static int cx=0,cy=0,flag=1;
    int i;
    HICON hicon;
    TEXTMETRIC tm;
    HWND wnd;
    static int iCurpos=0;
    switch(message)
    {
    case WM_RBUTTONDOWN:
    SetClassLong(hwnd,GCL_WNDPROC,(long)WndProc2);
    i=GetLastError();
    break;
    case WM_LBUTTONDOWN:
    hicon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_NEWICON));
    SetClassLong(hwnd,GCL_HICON,(LONG)hicon);
    break;
    case WM_CREATE:
    SetScrollRange(hwnd,SB_VERT,0,100-1,FALSE);
    hdc=GetDC(hwnd);
    GetTextMetrics(hdc,&tm);
    ReleaseDC(hwnd,hdc);
    cx=tm.tmAveCharWidth;
    cy=tm.tmHeight+tm.tmInternalLeading;//.tmExternalLeading;
    return 0;
    case WM_DESTROY:
    PostQuitMessage(0);
    return 0;
    case WM_PAINT:
    hdc=BeginPaint(hwnd,&ps);
    for(i=0;i<100;i++)
    {
    sprintf(str,"%d  the screen width is %d",i,GetSystemMetrics(0));
    TextOut(hdc,0,cy*(i-iCurpos),str,strlen(str));
    //sprintf(str,"the screen high is %d",GetSystemMetrics(1));
    //TextOut(hdc,0,cy*i+,str,strlen(str));
    }
    EndPaint(hwnd,&ps);
    return 0;
    case WM_CHAR:
    memset(chr,0,sizeof(chr));
    chr[0]=(int)wParam;
    strcat(szMessage,chr);
    GetClientRect(hwnd,&rect);
    InvalidateRect(hwnd,&rect,TRUE);
    return 0;
    case WM_TIMER:
    if(flag)
    {
    cy=100;
    for(cx=100;cx<600;cx++)
    {
    SetCursorPos(cx,cy);
    Sleep(10);
    }
    flag=0;
    }
    else
    {
    cy=50;
    for(cx=600;cx>100;cx--)
    {
    SetCursorPos(cx,cy);
    Sleep(10);
    }
    flag=1;
    mouse_event(MOUSEEVENTF_LEFTDOWN,cx,cy,0,0);
    mouse_event(MOUSEEVENTF_LEFTUP,cx,cy,0,0);
    }
    Sleep(1000);
    for(;;)
    {
       if((wnd=FindWindow(TEXT("IEFrame"),NULL))!=NULL)
    PostMessage(wnd,WM_CLOSE,0,0);
    else
    break;
    }
    return 0;
    case WM_VSCROLL:
    switch(LOWORD(wParam))
    {
    case SB_LINEUP:
    iCurpos--;
    break;
    case SB_LINEDOWN:
    iCurpos++;
    break;
    }
    SetScrollPos(hwnd,SB_VERT,iCurpos,TRUE);
    InvalidateRect(hwnd,NULL,TRUE);
    return 0;
    }
    return DefWindowProc(hwnd,message,wParam,lParam);
    }LRESULT CALLBACK WndProc2(HWND hwnd,UINT message,WPARAM wParam,LPARAM lParam)
    {
    return DefWindowProc(hwnd,message,wParam,lParam);
    }
      

  2.   

    把你的代码这一行
    SetClassLong (hwnd, GCL_WNDPROC, (LONG)WndProc2);
    换成下面一行,问题搞定
    SetWindowLong(hwnd, GWL_WNDPROC, (LONG)WndProc2);