我在CALLBACK 函数中想调用WinCreate这个函数来创建一个新的窗体,但是编译有错  不知道怎么去改 望指点一二
还有一个问题就是  我可不可不调用WinCreate这个函数 而直接调用WinMain主函数来创建窗口可以的吗 ??
想法:单击进入这个按钮  进入另外一个窗体  当前窗体消失
#include<windows.h>
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
BOOL InitWindowClass(HINSTANCE hInstance);
BOOL InitWinsow(HINSTANCE hInstance,int nCmdShow);
int  WinCreate(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow);BOOL InitWindows(HINSTANCE hInstance,int nCmdShow)
{
   HWND hwnd;
   hwnd=CreateWindow(
                   "实验",
   "实验",
    WS_OVERLAPPED|WS_MINIMIZE|WS_SYSMENU|WS_CAPTION|WS_MINIMIZEBOX,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL
                 );
   ShowWindow(hwnd,nCmdShow);
   UpdateWindow(hwnd);
   return TRUE;
}
BOOL InitWindowClass(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=WinProc;
   wndclass.lpszClassName="实验";
   wndclass.lpszMenuName=NULL;
   wndclass.style=CS_HREDRAW|CS_VREDRAW;
   return RegisterClass(&wndclass);
}
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
   
   MSG msg;
   if(!InitWindowClass(hInstance))
   return FALSE;
   if(!InitWindows(hInstance,nCmdShow))
   return FALSE;
   while(GetMessage(&msg,NULL,0,0))
   {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
   }
   return msg.wParam;
}
int  WinCreate(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
   
   MSG msg;
   if(!InitWindowClass(hInstance))
   return FALSE;
   if(!InitWindows(hInstance,nCmdShow))
   return FALSE;
   while(GetMessage(&msg,NULL,0,0))
   {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
   }
   return msg.wParam;
}LRESULT CALLBACK WinProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
 int  nCtrlId = LOWORD(wparam);
     static HWND hStatic;
     switch(message)
 {
       case WM_CREATE:
                    CreateWindow(
         "button",
         "进入",
 WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                                 400,
 200,
 70,
 24,
 hwnd,
 (HMENU)1050,
 ((LPCREATESTRUCT)lparam)->hInstance,
 NULL
 );
CreateWindow(
         "button",
         "退出",
 WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                                 500,
 200,
 70,
 24,
 hwnd,
 (HMENU)1060,
 ((LPCREATESTRUCT)lparam)->hInstance,
 NULL
 );
     break;
           case WM_COMMAND:
                  switch (nCtrlId)
  {
                   case 1050:
                    //MessageBox(hwnd, "clicked me", "note", MB_OK);
                    //SetWindowText(hStatic, "CSDN");
    //WinCreate( hInstance, hPrevInstance, lpCmdLine, nCmdShow);
                    break;
case 1060:
                    MessageBox(hwnd, "clicked me", "note", MB_OK);
                    SetWindowText(hStatic, "CSDN");
                    break;
  }
                  break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,message,wparam,lparam);
 }
 return 0;
}

解决方案 »

  1.   

    楼主,你这个程序,是在VS2005或VS2008里,设了UNICODE的环境,而你用的却是ASCII环境下的参数char那些,而不是TCHAR(兼容ASCII与UNICODE)的类型,所以出错的.
    按PROJECT->属性,在 Configuration Properties -> General ->Character Set 改成 Not Set
    这样用你的代码就可以过编译,但还有错误.调用WINCREATE时的hInstance变量是WinMain的局部变量来的.这里用不了.我也再试过一下,就算用全局保存再传过来.也还是有其他问题的.
    问题就在你进入RegisterClass注册的是同名的窗口类而返回了0值而使你的WinCreate函数提前返回.
      

  2.   

    你的代码,我VS2005编译有错。原因如1楼。
    但是把ProjectSetting的characterset,设置为No Set就可以编译通过。