鱼C论坛

 找回密码
 立即注册
查看: 3265|回复: 9

一个关于mfc的问题

[复制链接]
发表于 2011-12-4 13:47:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是怎么回事,程序没有错也可以运行,但就是不能做出窗口。我是在win7下,用vc++6。0写的。

WinMain.rar

952 Bytes, 下载次数: 6

源文件

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-4 18:00:47 | 显示全部楼层
最好把关键代码上来看看。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-4 22:51:37 | 显示全部楼层
  刚学C  什么都不懂 蛋疼中
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-6 12:07:08 | 显示全部楼层
#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinProc(
  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,          // command line
  int nCmdShow              // show state
)
{
        WNDCLASS winclass;
        winclass.cbClsExtra=0;
        winclass.cbWndExtra=0;
        winclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
        winclass.hCursor=LoadCursor(NULL,IDC_CROSS);
        winclass.hIcon=LoadIcon(NULL,IDI_ERROR);
        winclass.hInstance=hInstance;
        winclass.lpfnWndProc=WinProc;
        winclass.lpszClassName="Win";
        winclass.lpszMenuName=NULL;
        winclass.style=CS_HREDRAW|CS_VREDRAW;


        RegisterClass(&winclass);
        HWND hwnd;
        hwnd=CreateWindow("Win","FirstWin",WS_OVERLAPPEDWINDOW,0,0,400,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 WinProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{  

        switch(uMsg)
        {
        case WM_PAINT:
                HDC hdc;
            PAINTSTRUCT ps;
                hdc=BeginPaint(hwnd,&ps);
                EndPaint(hwnd,&ps);
                break;
    case WM_CLOSE:
                if(MessageBox(hwnd,"程序退出","是否退出程序",MB_YESNO))
                DestroyWindow(hwnd);
                break;
        case WM_LBUTTONDOWN:
                MessageBox(hwnd,"oye","click once again",MB_OK);
                break;
        case WM_CHAR:
                MessageBox(hwnd,"oo","try again",MB_OK);
                break;
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default: DefWindowProc(hwnd,uMsg,wParam,lParam);
                break;
        }
        return 0;
}
就这么多
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-6 12:07:38 | 显示全部楼层

#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WinProc(
  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,          // command line
  int nCmdShow              // show state
)
{
        WNDCLASS winclass;
        winclass.cbClsExtra=0;
        winclass.cbWndExtra=0;
        winclass.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
        winclass.hCursor=LoadCursor(NULL,IDC_CROSS);
        winclass.hIcon=LoadIcon(NULL,IDI_ERROR);
        winclass.hInstance=hInstance;
        winclass.lpfnWndProc=WinProc;
        winclass.lpszClassName="Win";
        winclass.lpszMenuName=NULL;
        winclass.style=CS_HREDRAW|CS_VREDRAW;


        RegisterClass(&winclass);
        HWND hwnd;
        hwnd=CreateWindow("Win","FirstWin",WS_OVERLAPPEDWINDOW,0,0,400,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 WinProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
)
{  

        switch(uMsg)
        {
        case WM_PAINT:
                HDC hdc;
            PAINTSTRUCT ps;
                hdc=BeginPaint(hwnd,&ps);
                EndPaint(hwnd,&ps);
                break;
    case WM_CLOSE:
                if(MessageBox(hwnd,"程序退出","是否退出程序",MB_YESNO))
                DestroyWindow(hwnd);
                break;
        case WM_LBUTTONDOWN:
                MessageBox(hwnd,"oye","click once again",MB_OK);
                break;
        case WM_CHAR:
                MessageBox(hwnd,"oo","try again",MB_OK);
                break;
        case WM_DESTROY:
                PostQuitMessage(0);
                break;
        default: DefWindowProc(hwnd,uMsg,wParam,lParam);
                break;
        }
        return 0;
}
就这么多
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-6 15:16:03 | 显示全部楼层
请改成:
  1. switch(uMsg)
  2. {
  3. ....
  4.   default:
  5.        return DefWindowProc(hwnd, uMsg, wParam, lParam);
  6. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-6 16:03:19 | 显示全部楼层
正如楼上说的,回调函数最后必须返回默认回调函数。
return DefWindowProc(hwnd,uMsg,wParam,lParam);

另外,你确定可以在switch....case中定义变量吗?
        switch(uMsg)
        {
        case WM_PAINT:
                HDC hdc;
            PAINTSTRUCT ps;
       。。。。。。
反正在我的电脑上,这样编译是通不过的。要在switch case 前面定义才通过编译。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-7 20:46:50 | 显示全部楼层
有用了呢,太感谢各位大神了,好几天都没弄明白的呢。我这可以在switch。。。case中定义变量,我用的是vc++6.0企业版
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
 楼主| 发表于 2011-12-7 21:03:00 | 显示全部楼层
Mr.C 发表于 2011-12-6 16:03
正如楼上说的,回调函数最后必须返回默认回调函数。
return DefWindowProc(hwnd,uMsg,wParam,lParam);

这里可以呢,我用的是vc++6.0,能不能在告诉我下updatawindow有什么作用,好像有没有都有用呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
发表于 2011-12-7 21:50:37 | 显示全部楼层
如果窗口更新的区域不为空,UpdateWindow函数通过发送一个WM_PAINT消息来更新指定窗口的客户区。函数绕过应用程序的消息队列,直接发送WM_PAINT消息给指定窗口的窗口过程,如果更新区域为空,则不发送消息。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 18:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表