| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1132 人关注过本帖
标题:麻烦帮我看看这个程序(书上的例子)是怎么回事,编译总是通不过
只看楼主 加入收藏
yc0708
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-10-27
收藏
 问题点数:0 回复次数:6 
麻烦帮我看看这个程序(书上的例子)是怎么回事,编译总是通不过
麻烦哪位高手帮我看看,编译通不过呀。
#include<iostream.h>
LRESULT CALLBACK WndProc(HWND,UNIT,WPARAM,LPARAM);
char szClassName[]="windowclass";
char szAppTitle[]="API 建立窗口实例";
INT PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpszCmdParam,INT nCmdShow)
{
     HWND hMainWnd;
     MSG msg;
     WNDCLASS winclass;
  if(!hPrevInstance)
      //判断是否已有应用程序的实例存在运行,给窗口结构体的数据成员赋值来规定所要建立的窗口的特征
  {
      winclass.style=CS_HREDRAW|CS_AVERDRAW;//窗口风格
      winclass.lpfnWndProc=WndProc;         //窗口的消息处理函数
      winclass.cbClsExtra=0;                //窗口类无扩展
      winclass.cbWndExtra=0;                //窗口实例无扩展
      winclass.hInstance=hInstance;         //当前应用程序实例句柄
      winclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);//窗口最小化图标为缺省图标
      winclass.hCursor=LoadCursor(NULL,IDC_ARROW);//窗口采用箭头光标
      winclass.hbrBackground=(HBRUSH)GetStockObject(LTGRAY_BRUSH);//窗口背景色为浅灰色
      winclass.lpszMenuName=NULL;          //无窗口菜单
      winclass.lpszClassName=szClassName;  //给窗口结构体命名
      RegisterClass(&winclass);
  }
  //下面用CreateWindow()函数建立窗口,并返回所建立的窗口句柄
  hMainWnd=CreateWindow(
      szClassName,                        //窗口结构体名称
      szAppTitle,                         //窗口的标题
      WS_OVERLAPPEDWINDOW,                //窗口风格为可重叠窗口
      //下面4个参数代表窗口左上角x,y坐标和窗口的宽度以及高度,都使用缺省值
      CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
      //下面参数分别为父窗口句柄、窗口菜单句柄、应用程序实例句柄和附加参数
      NULL,NULL,hInstance,NULL);
  ShowWindow(hMainWnd,SW_SHOWNORMAL);
  UpdateWindow(hMainWnd);
  while(GetMessage(&msg,NULL,0,0))       //获取消息,填充msg结构体
  {
      TranslateMessage(&msg);            //翻译键盘消息
      DispatchMessage(&msg);             //向窗口函数发送消息,让窗口函数处理
  }
  return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hMainWnd,UNIT message,WPARAM wParam,LPARAM lParam)
//窗口函数的定义
{
    HDC hdc;                             //设备描述表
    PAINTSTRUCT ps;                      //刷新区域
    RECT rect;                           //矩形结构
    char messageleft[]="按下了鼠标左键!";
    char messageright[]="按下了鼠标右键!";
        switch(message)
        {
        case WM_PAINT:
            {
                hdc=BeginPaint(hMainWnd,&ps);
                GetClientRect(hMainWnd,&rect);
                DrawText(hdc,TEXT("使用API建立Windows窗口实例!"),-1,&rect,DT_SINGLELINE|
                    DT_CENTER|DT_Visual C++ENTER);
                EndPaint(hMainWnd,&ps);
                break;
            }
        case WM_RBUTTONDOWN:
            {
                MessageBox(GetFocus(),messageright,"API建立窗口实例!",MB_OK|MB_ICONINFORMATION);
                break;
            }
        case WM_LBUTTONDOWN:
            {
                MessageBox(GetFocus(),messageleft,"API建立窗口实例",MB_OK|MB_ICONINFORMATION);
                    break;
            }
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
        default:
            break;
        }
        return DefWindowProc(hMainWnd,message,wParam,lParam);
}

[[it] 本帖最后由 yc0708 于 2008-11-30 18:36 编辑 [/it]]
搜索更多相关主题的帖子: 麻烦 编译 例子 
2008-11-30 18:35
天使梦魔
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:37
帖 子:564
专家分:2754
注 册:2007-8-29
收藏
得分:0 
书上没有说这是32位程序?
要用iostream.h而不是windows.h?
2008-11-30 19:08
zhangyafei4353
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-11-28
收藏
得分:0 
不能尽信书,你的代码不全,并且好多输入错误:
我改了一下,运行正确,代码如下:
#include "stdafx.h"
#include "resource.h"

#define MAX_LOADSTRING 100

HINSTANCE hInst;                                // current instance
TCHAR szTitle[MAX_LOADSTRING];                                // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];                                // The title bar text

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_MY154, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow))
    {
        return FALSE;
    }

    hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_MY154);

    // Main message loop:
    while (GetMessage(&msg, NULL, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return msg.wParam;
}

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_MY154);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = (LPCSTR)IDC_MY154;
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

    return RegisterClassEx(&wcex);
}

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;
}

LRESULT CALLBACK WndProc(HWND hMainWnd,UINT message,WPARAM wParam,LPARAM lParam)
//窗口函数的定义
{
    HDC hdc;                             //设备描述表
    PAINTSTRUCT ps;                      //刷新区域
    RECT rect;                           //矩形结构
    char messageleft[]="按下了鼠标左键!";
    char messageright[]="按下了鼠标右键!";
        switch(message)
        {
        case WM_PAINT:
            {
                hdc=BeginPaint(hMainWnd,&ps);
                GetClientRect(hMainWnd,&rect);
                DrawText(hdc,TEXT("使用API建立Windows窗口实例!"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
                EndPaint(hMainWnd,&ps);
                break;
            }
        case WM_RBUTTONDOWN:
            {
                MessageBox(GetFocus(),messageright,"API建立窗口实例!",MB_OK|MB_ICONINFORMATION);
                break;
            }
        case WM_LBUTTONDOWN:
            {
                MessageBox(GetFocus(),messageleft,"API建立窗口实例",MB_OK|MB_ICONINFORMATION);
                    break;
            }
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
        default:
            break;
        }
        return DefWindowProc(hMainWnd,message,wParam,lParam);
}

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;
}
2008-11-30 19:11
zhangyafei4353
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2008-11-28
收藏
得分:0 
我建立的是Win32 Application工程,在A typical "Hello,World!" application基础上改的,可能有多余的代码。。
2008-11-30 19:17
yc0708
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2008-10-27
收藏
得分:0 
谢谢了呀!能加一下你的QQ吗?
2008-12-01 13:02
tangwen88
Rank: 1
等 级:新手上路
帖 子:27
专家分:0
注 册:2008-11-5
收藏
得分:0 
这个能运行的,你可以改一下
#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WindSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);


int WINAPI WinMain(
  HINSTANCE hInstance,  // handle to current instance
  HINSTANCE hPrevInstance,  // handle to previous instance
  LPSTR lpCmdLine,      // pointer to command line
  int nCmdShow          // show state of window
)
{
    WNDCLASS wndcls;
    wndcls.cbClsExtra=0;
    wndcls.cbWndExtra=0;
    wndcls.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
    wndcls.hCursor=LoadCursor(NULL,IDC_CROSS);
    wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    wndcls.hInstance=hInstance;
    wndcls.lpfnWndProc=WindSunProc;
    wndcls.lpszClassName="tangwen2008";
    wndcls.lpszMenuName=NULL;
    wndcls.style=CS_HREDRAW|CS_VREDRAW;
    RegisterClass(&wndcls);

    HWND hwnd;
    hwnd=CreateWindow("tangwen2008","suidongmei",WS_OVERLAPPEDWINDOW,
        0,0,600,600,NULL,NULL,hInstance,NULL);
    ShowWindow(hwnd,SW_SHOWNORMAL);
    UpdateWindow(hwnd);

    MSG msg;
    while(GetMessage(&msg,NULL,0,0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

LRESULT CALLBACK WindSunProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{
    switch(uMsg)
    {
    case WM_CHAR:
        char szChar[20];
        sprintf(szChar,"char is %d",wParam);
        MessageBox(hwnd,szChar,"tangwen2008",0);
        break;
    case WM_LBUTTONDOWN:
        MessageBox(hwnd,"mouse clicked","tangwen2008",0);
        HDC hdc;
        hdc=GetDC(hwnd);
        TextOut(hdc,0,10,"唐文你错了",strlen("唐文你错了"));
        ReleaseDC(hwnd,hdc);
        break;
    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT ps;
        hDC=BeginPaint(hwnd,&ps);
        TextOut(hDC,0,40,"唐文你错了",strlen("唐文你错了"));
        EndPaint(hwnd,&ps);
        break;
    case WM_CLOSE:
        if(IDYES==MessageBox(hwnd,"你敢按NO老子打你","tangwen2008",MB_YESNO))
        {
            DestroyWindow(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    
    default:
        return DefWindowProc(hwnd,uMsg,wParam,lParam);
    }
    return 0;
}
2008-12-01 18:31
mengfei358
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2009-11-24
收藏
得分:0 
楼主,我想请问一下,你的问题是怎么解决的,我现在也遇到了问题,就是DT_Visual C++ENTER,这,不知道是为啥,
2009-11-24 14:27
快速回复:麻烦帮我看看这个程序(书上的例子)是怎么回事,编译总是通不过
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017879 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved