刚学API,学者书上打了一段代码,可是总是有问题,可和书上是一样的啊。
#include<windows.h>
int CALLBACK wnproc(HWND,UINT,WPARAM,LPARAM);
char szclassname[]="windowclass";
char szapptitle[]="windows api";
int PASCAL winmain(HINSTANCE hinstance,HINSTANCE hprevistance,LPSTR lpszsmdparam,int ncmdshow) 
{
HWND hmainwnd;
MSG msg;
WNDCLASS mywc;
if(!hprevistance)
{
mywc.style=CS_HREDRAM|CS_VREDRAM;
mywc.lpfnWndProc =wnproc;
mywc.cbClsExtra =0;
mywc.cbWndExtra=0;
mywc.hInstance=hinstance;
mywc.hIcon=LoadIcon(0,IDI_APPLIICATION);
mywc.hbrBackground=GetStockObject(WHITE_BRUSH);
        mywc.lpszMenuName=0;
mywc.lpszClassName=szclassname;
RegisterClass(&mywc);
}
hmainwnd=CreateWindow(
szclassname,
szapptitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hinstance,NULL);
ShowWindow(hmainwnd,SW-SHOWMAXIMIZED);
UpdateWindow(hmainwnd);
while(GetMessage(&msg,0,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
int CALLBACK Wndproc (HWND hmainwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
char messageleft[]="the left button have pubshed";
char messageright[]="the right button have been pubshed";
switch(message)
{
case WM_RBUTTONDOWN:
{
message(GetFocus(),messageleft,"RBUTTONpushdown",
MB_OK|MB_ICONFROMARION);
break;
}
case WM_LBUTTONDOWN:
{
message(GetFocus(),messageleft,"LBUTTONPUSHDOWN",
MB_OK|MB_ICONINFORMATION);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
default:break;
}
return DefWindowProc(hmainwnd,message,wparam,lparam);
}
提示的错误是
i.cpp
D:\c++例&my think\wozj\api\api.cpp(12) : error C2065: 'CS_HREDRAM' : undeclared identifier
D:\c++例&my think\wozj\api\api.cpp(12) : error C2065: 'CS_VREDRAM' : undeclared identifier
D:\c++例&my think\wozj\api\api.cpp(13) : error C2440: '=' : cannot convert from 'int (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)' to 'long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)'
        This conversion requires a reinterpret_cast, a C-style cast or function-style cast
D:\c++例&my think\wozj\api\api.cpp(17) : error C2065: 'IDI_APPLIICATION' : undeclared identifier
D:\c++例&my think\wozj\api\api.cpp(18) : error C2440: '=' : cannot convert from 'void *' to 'struct HBRUSH__ *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
D:\c++例&my think\wozj\api\api.cpp(30) : error C2065: 'SW' : undeclared identifier
D:\c++例&my think\wozj\api\api.cpp(30) : error C2065: 'SHOWMAXIMIZED' : undeclared identifier
D:\c++例&my think\wozj\api\api.cpp(48) : error C2065: 'MB_ICONFROMARION' : undeclared identifier
D:\c++例&my think\wozj\api\api.cpp(48) : error C2064: term does not evaluate to a function
D:\c++例&my think\wozj\api\api.cpp(54) : error C2064: term does not evaluate to a function
Error executing cl.exe.
愿高手帮忙解决。

解决方案 »

  1.   

    囧, CS_HREDRAM
    --》
    CS_HREDRAW
      

  2.   

    刚接触api,是从书上抄的代码,看的眼睛都花了
      

  3.   

    #include<windows.h>
    LRESULT CALLBACK wnproc(HWND,UINT,WPARAM,LPARAM);
    char szclassname[]="windowclass";
    char szapptitle[]="windows api";
    int PASCAL WinMain(HINSTANCE hinstance,HINSTANCE hprevistance,LPSTR lpszsmdparam,int ncmdshow)  
    {
    HWND hmainwnd;
    MSG msg;
    WNDCLASS mywc;
    if(!hprevistance)
    {
    mywc.style=CS_HREDRAW|CS_VREDRAW;
    mywc.lpfnWndProc =wnproc;
    mywc.cbClsExtra =0;
    mywc.cbWndExtra=0;
    mywc.hInstance=hinstance;
    mywc.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    mywc.hCursor = LoadCursor(NULL, IDC_ARROW);
    mywc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    mywc.lpszMenuName=0;
    mywc.lpszClassName=szclassname;
    RegisterClass(&mywc);
    }
    hmainwnd=CreateWindow(
    szclassname,
    szapptitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,CW_USEDEFAULT,
    CW_USEDEFAULT,CW_USEDEFAULT,
    NULL,NULL,hinstance,NULL);
    ShowWindow(hmainwnd,SW_SHOWMAXIMIZED);
    UpdateWindow(hmainwnd);
    while(GetMessage(&msg,0,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }LRESULT CALLBACK wnproc (HWND hmainwnd,UINT message,WPARAM wparam,LPARAM lparam)
    {
    char messageleft[]="the left button have pubshed";
    char messageright[]="the right button have been pubshed";
    switch(message)
    {
    case WM_RBUTTONDOWN:
    {
    MessageBox(GetFocus(),messageleft,"RBUTTONpushdown",
    MB_OK|MB_ICONINFORMATION);
    break;
    }
    case WM_LBUTTONDOWN:
    {
    MessageBox(GetFocus(),messageleft,"LBUTTONPUSHDOWN",
    MB_OK|MB_ICONINFORMATION);
    break;
    }
    case WM_DESTROY:
    {
    PostQuitMessage(0);
    return 0;
    }
    default:break;
    }
    return DefWindowProc(hmainwnd,message,wparam,lparam);
    }
      

  4.   

    // test.cpp : Defines the entry point for the application.
    //#include "stdafx.h"
    #include "resource.h"#define MAX_LOADSTRING 100// Global Variables:
    //#include<windows.h>
    long CALLBACK Wndproc(HWND,UINT,WPARAM,LPARAM);
    char szclassname[]="windowclass";
    char szapptitle[]="windows api";
    int PASCAL WinMain(HINSTANCE hinstance,HINSTANCE hprevistance,LPSTR lpszsmdparam,int ncmdshow)  
    {
    HWND hmainwnd;
    MSG msg;
    WNDCLASS mywc;
    if(!hprevistance)
    {
    //mywc.style=CS_HREDRAM|CS_VREDRAM;
    mywc.style=CS_HREDRAW|CS_VREDRAW;
    mywc.lpfnWndProc =Wndproc;
    mywc.cbClsExtra =0;
    mywc.cbWndExtra=0;
    mywc.hInstance=hinstance;
    mywc.hIcon=LoadIcon(0,IDI_APPLICATION);
    mywc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
    mywc.lpszMenuName=0;
    mywc.lpszClassName=szclassname;
    RegisterClass(&mywc);
    }
    hmainwnd=CreateWindow(
    szclassname,
    szapptitle,
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,CW_USEDEFAULT,
    CW_USEDEFAULT,CW_USEDEFAULT,
    NULL,NULL,hinstance,NULL);
    ShowWindow(hmainwnd,SW_SHOWMAXIMIZED);
    UpdateWindow(hmainwnd);
    while(GetMessage(&msg,0,0,0))
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    return msg.wParam;
    }
    long CALLBACK Wndproc (HWND hmainwnd,UINT message,WPARAM wparam,LPARAM lparam)
    {
    char messageleft[]="the left button have pubshed";
    char messageright[]="the right button have been pubshed";
    switch(message)
    {
    case WM_RBUTTONDOWN:
    {
    MessageBox(GetFocus(),messageleft,"LButtonPushDown",MB_OK|MB_ICONINFORMATION); break;
    }
    case WM_LBUTTONDOWN:
    {
    MessageBox(GetFocus(),messageright,"RButtonPushDown",MB_OK|MB_ICONINFORMATION);
    break;
    }
    case WM_DESTROY:
    {
    PostQuitMessage(0);
    return 0;
    }
    default:break;
    }
    return DefWindowProc(hmainwnd,message,wparam,lparam);
    }WIn32对大小写是区分的,很多宏定义打错了,以后注意点这些。