#include<windows.h>
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);//窗口说明
//--------------以下初始化窗口类-------------
int WINAPI WinMain(HINSTANCE hInstance,  //winmain函数说明
   HINSTANCE hPrevInst,
   LPSTR lpszCmdLine,
   int nCmdShow)
{
HWND hwnd;
MSG Msg;
WNDCLASS wndclass;
char lpszClassName[]="窗口";
char lpszTitle[]="My-windows";
wndclass.style=0; //窗口类型为默认类型
wndclass.lpfnWndProc = WndProc;//窗口处理函数为WndProc
wndclass.cbClsExtra=0;       //窗口类无扩展
wndclass.cbWndExtra=0;  //窗口实例无扩展
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);//为什么说这里是不合法的类型转换?
wndclass.lpszClassName=lpszClassName;
//---------------以下进行窗口类的注册---------------------
if(!RegisterClass(&wndclass))
{
MessageBeep(0);
return FALSE;
}
hwnd=CreateWindow(lpszClassName, //窗口类名
  lpszTitle, //窗口标题
  WS_OVERLAPPEDWINDOW, //窗口的风格
  CW_USEDEFAULT,
  CW_USEDEFAULT, //窗口左上脚坐标为默认
  CW_USEDEFAULT,
  CW_USEDEFAULT, //窗口的高和宽为默认值
  NULL, //无父窗口
  NULL, //无主菜单
  hInstance, //应用程序的当前句柄
  NULL); //不使用该值
//显示窗口
ShowWindow(hwnd,nCmdShow);
//绘制用户区
UpdateWindow(hwnd);
//消息循环
while(GetMessage(&Msg,NULL,0,0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;//消息循环结束将信息返回系统
}
LRESULT CALLBACK WndProc( HWND hwnd,
  UINT message,
  WPARAM wParam,
  LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
PostQuitMessage(0);//调用PostQuitMessage发出WM_QUIT消息
default: //默认时采用系统消息默认处理函数
return DefWindowProc(hwnd,message,wParam,lParam);
}
return (0);
}
请各位前辈指点一二,谢谢。
 

解决方案 »

  1.   

    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);//修改后
      

  2.   

    wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);//需要进行强制转换WNDCLASS结构有十项,你少了一项,再添加下面一行wndclass.lpszMenuName = NULL;
      

  3.   

    wndclass.hbrBackground = GetStockObject(WHITE_BRUSH)该句必须进行强制类型转换,加个(HBRUSH)即可.
    好像这个错误常有人出!
      

  4.   

    你可以试一试先建立一个工程文件,然后在添加入Source Files.
    其它的错误别人已经指出来了。
    我这个方法实现了你的程序。
    如果你还没有实现的话,可以发E_MAIL给我。我帮你哟!
    [email protected]
      

  5.   

    谢谢楼上的各位指点。
    我按照你们说的把错误修改了,编译是通过了,但是link的时候出错了,错误是这样的:
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    Debug/myproj.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    应该是说没有主函数吧?怎样才能在我得机器上生成一个窗口呢?
      

  6.   

    工程建错了,你应该建立“Win32 Application”工程而不是建立“Win32 console Application”工程,你可以新建一个工程,注意在出现的第一个画面时选择“Win32 Application”而不是“Win32 console Application”,然后在.cpp文件中输入你上面的代码。
    CreateWindow(……)就是创建windows窗口了
    ShowWindow(……)则是显示它
      

  7.   

    在VC中建立Win32 Application和Win32 Console Application。前者以WinMain为主函数,可以使用所有Win32 API。后者以main为主函数,不能使用GDI函数,工作方式类似DOS程序,但确实是Win32程序,不能在DOS和Windows 3.x中运行。你可以在VC中建立Win32 Application,然后加入你的C程序(当然假定你的程序中应该有#include <windows.h>),然后编译即可。 
      

  8.   

    就像上一层将的在生成程序的时候选择 Win32 Application,然后选择 Hello World 那个例子程序就可以生成一个有窗口,并且在窗口上有字符串输出的例子乐。#define MAX_LOADSTRING 100// Global Variables:
    HINSTANCE hInst; // current instance
    TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
    TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text// Foward declarations of functions included in this code module:
    ATOM MyRegisterClass(HINSTANCE hInstance);
    BOOL InitInstance(HINSTANCE, int);
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
      // TODO: Place code here.
    MSG msg;
    HACCEL hAccelTable; // Initialize global strings
    LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDC_NT2, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance); // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
    return FALSE;
    } hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_NT2); // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0)) 
    {
    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
    {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
    }
    } return msg.wParam;
    }//
    //  FUNCTION: MyRegisterClass()
    //
    //  PURPOSE: Registers the window class.
    //
    //  COMMENTS:
    //
    //    This function and its usage is only necessary if you want this code
    //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
    //    function that was added to Windows 95. It is important to call this function
    //    so that the application will get 'well formed' small icons associated
    //    with it.
    //
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
    WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX);  wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = (WNDPROC)WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_NT2);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName = (LPCSTR)IDC_NT2;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);
    }//
    //   FUNCTION: InitInstance(HANDLE, int)
    //
    //   PURPOSE: Saves instance handle and creates main window
    //
    //   COMMENTS:
    //
    //        In this function, we save the instance handle in a global variable and
    //        create and display the main program window.
    //
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
       HWND hWnd;   hInst = hInstance; // Store instance handle in our global variable   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
          CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);   if (!hWnd)
       {
          return FALSE;
       }   ShowWindow(hWnd, nCmdShow);
       UpdateWindow(hWnd);   return TRUE;
    }//
    //  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
    //
    //  PURPOSE:  Processes messages for the main window.
    //
    //  WM_COMMAND - process the application menu
    //  WM_PAINT - Paint the main window
    //  WM_DESTROY - post a quit message and return
    //
    //
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    int wmId, wmEvent;
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR szHello[MAX_LOADSTRING];
    LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING); switch (message) 
    {
    case WM_COMMAND:
    wmId    = LOWORD(wParam); 
    wmEvent = HIWORD(wParam); 
    // Parse the menu selections:
    switch (wmId)
    {
    case IDM_ABOUT:
       DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
       break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
    }
    break;
    case WM_PAINT:
    hdc = BeginPaint(hWnd, &ps);
    // TODO: Add any drawing code here...
    RECT rt;
    GetClientRect(hWnd, &rt);
    DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
    EndPaint(hWnd, &ps);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hWnd, message, wParam, lParam);
       }
       return 0;
    }// Mesage handler for about box.
    LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {
    switch (message)
    {
    case WM_INITDIALOG:
    return TRUE; case WM_COMMAND:
    if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
    {
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
    }
    break;
    }
        return FALSE;
    }
      

  9.   

    这些代码自己看看,知道意思就可以了。你不会全是自己写的吧?
    这个自动帮你生成的。不过API确实是一个比较烦的东西,刚开始可能会一天才啃俩页,
    还会不知其所以然,慢慢的结合MFC就能了解深刻了,
    期望楼主的毅力!