我想在窗口中创建一个按钮,但老是编译错误,搞了几天了,还是找不到错在哪里,请问谁能帮我看看,谢谢!!
程序如下(由于已有错误,有些按钮没有添加代码)#include "stdafx.h"
#include <windows.h>#define IDB_PUSH 1;
#define IDB_DEFPUSH 2;
#define IDB_RADIO 3;
#define IDB_AUTORADIO 4;
#define IDB_CHECKBOX 5;
#define IDB_AUTOCHECKBOX 6;
#define IDB_GROUPBOX 7;
HWND hwndmain,hwndPush,hwndDefPush,hwndRadio,hwndAutoRadio,hwndCheckBox,hwndAutoCheckBox,hwndGroupBox;
LPCTSTR lpszClassName="BUTTON";
LPCTSTR lpszTitle="子窗口按钮示例";
HINSTANCE hInst;BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if(!InitWindowsClass(hInstance)){
MessageBeep(0);
return FALSE;
}
if(!InitWindows(hInstance,nCmdShow)){
MessageBeep(0);
return FALSE;
}
hInst=hInstance;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
}
BOOL InitWindowsClass(HINSTANCE hInstance){
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=lpszClassName;
wndclass.lpszMenuName=NULL;
wndclass.style=0;
return RegisterClass(&wndclass);
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow){
HWND hwnd=CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hwnd)
return 0;
hwndmain=hwnd;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return 1;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_CREATE:
hwndPush=CreateWindow(0,lpszClassName,"这是按钮",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,50,90,80,150,hWnd,(HMENU)IDB_PUSH,(HINSTANCE)hInst,NULL);
break;
case WM_COMMAND:
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}编译后显示如下信息:
D:\button\button.cpp(77) : warning C4002: too many actual parameters for macro 'CreateWindowA'
D:\button\button.cpp(77) : error C2143: syntax error : missing ')' before ';'
D:\button\button.cpp(77) : error C2660: 'CreateWindowExA' : function does not take 11 parameters
D:\button\button.cpp(77) : error C2143: syntax error : missing ';' before ','
D:\button\button.cpp(77) : error C2059: syntax error : ')'
程序如下(由于已有错误,有些按钮没有添加代码)#include "stdafx.h"
#include <windows.h>#define IDB_PUSH 1;
#define IDB_DEFPUSH 2;
#define IDB_RADIO 3;
#define IDB_AUTORADIO 4;
#define IDB_CHECKBOX 5;
#define IDB_AUTOCHECKBOX 6;
#define IDB_GROUPBOX 7;
HWND hwndmain,hwndPush,hwndDefPush,hwndRadio,hwndAutoRadio,hwndCheckBox,hwndAutoCheckBox,hwndGroupBox;
LPCTSTR lpszClassName="BUTTON";
LPCTSTR lpszTitle="子窗口按钮示例";
HINSTANCE hInst;BOOL InitWindowsClass(HINSTANCE hInstance);
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
if(!InitWindowsClass(hInstance)){
MessageBeep(0);
return FALSE;
}
if(!InitWindows(hInstance,nCmdShow)){
MessageBeep(0);
return FALSE;
}
hInst=hInstance;
while(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
} return msg.wParam;
}
BOOL InitWindowsClass(HINSTANCE hInstance){
WNDCLASS wndclass;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hInstance=hInstance;
wndclass.lpfnWndProc=WndProc;
wndclass.lpszClassName=lpszClassName;
wndclass.lpszMenuName=NULL;
wndclass.style=0;
return RegisterClass(&wndclass);
}
BOOL InitWindows(HINSTANCE hInstance,int nCmdShow){
HWND hwnd=CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hwnd)
return 0;
hwndmain=hwnd;
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
return 1;
}
LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
switch(message){
case WM_CREATE:
hwndPush=CreateWindow(0,lpszClassName,"这是按钮",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,50,90,80,150,hWnd,(HMENU)IDB_PUSH,(HINSTANCE)hInst,NULL);
break;
case WM_COMMAND:
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return 0;
}编译后显示如下信息:
D:\button\button.cpp(77) : warning C4002: too many actual parameters for macro 'CreateWindowA'
D:\button\button.cpp(77) : error C2143: syntax error : missing ')' before ';'
D:\button\button.cpp(77) : error C2660: 'CreateWindowExA' : function does not take 11 parameters
D:\button\button.cpp(77) : error C2143: syntax error : missing ';' before ','
D:\button\button.cpp(77) : error C2059: syntax error : ')'
hwndPush=CreateWindow(lpszClassName,"这是按钮",WS_CHILD ¦WS_VISIBLE ¦BS_PUSHBUTTON,50,90,80,150,hWnd,(HMENU)IDB_PUSH,(HINSTANCE)hInst,NULL); 编译后显示如下信息:
D:\button\button.cpp(77) : error C2143: syntax error : missing ')' before ';'
D:\button\button.cpp(77) : error C2660: 'CreateWindowExA' : function does not take 11 parameters
D:\button\button.cpp(77) : error C2143: syntax error : missing ';' before ','
D:\button\button.cpp(77) : error C2059: syntax error : ')'
HWND hwnd=CreateWindow(lpszClassName,lpszTitle,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,hInstance,NULL);
if(!hwnd)
return 0; 这里的CreateWindow是不是少了什么参数呢?第一个参数应该是NULL?
你到底要用CreateWindow还是CreateWindowEx?
你贴的代码是CreateWindow,结果错误信息里显示CreateWindowEx?CreateWindow是11个参数,CreateWindowEx是12个参数
hwndPush=CreateWindow(lpszClassName,"这是按钮",WS_CHILD ¦WS_VISIBLE ¦BS_PUSHBUTTON,50,90,80,150,hWnd,(HMENU)IDB_PUSH,(HINSTANCE)hInst,NULL); 编译后显示如下信息:
D:\button\button.cpp(77) : error C2143: syntax error : missing ')' before ';'
D:\button\button.cpp(77) : error C2660: 'CreateWindowExA' : function does not take 11 parameters
D:\button\button.cpp(77) : error C2143: syntax error : missing ';' before ','
D:\button\button.cpp(77) : error C2059: syntax error : ')'
hwndPush=CreateWindow(lpszClassName,"这是按钮",WS_CHILD ¦WS_VISIBLE ¦BS_PUSHBUTTON,50,90,80,150,hWnd,(HMENU)IDB_PUSH,(HINSTANCE)hInst,NULL); 编译后显示如下信息:
D:\button\button.cpp(77) : error C2143: syntax error : missing ')' before ';'
D:\button\button.cpp(77) : error C2660: 'CreateWindowExA' : function does not take 11 parameters
D:\button\button.cpp(77) : error C2143: syntax error : missing ';' before ','
D:\button\button.cpp(77) : error C2059: syntax error : ')'
后面的";"去掉 CreateWindow 改为 CreateWindowEX
看看你的IDB_PUSH是什么:
#define IDB_PUSH 1; 宏替换后变成了:
hwndPush=CreateWindow(lpszClassName,lpszClassName,"这是按钮",WS_CHILD |WS_VISIBLE |BS_PUSHBUTTON,50,90,80,150,hWnd,(HMENU)1;,(HINSTANCE)hInst,NULL);
自己看看错在什么地方
要先加语句
case WM_DESTROY:
PostQuitMessage(0);
break;