C++ MFC有进程没有界面
LRESULT CALLBACK WinSunPrnc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrcvInstance,
    LPSTR IpCmdLine,
    int nCmdShow
    )
{
    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_ERROR);//图标a
    wndcls.hInstance = hInstance;//
    wndcls.lpfnWndProc = WinSunPrnc;//窗口过程实例
    wndcls.lpszClassName = "First MFC";//类名化;
    wndcls.lpszMenuName = NULL;//菜单的名字
    wndcls.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wndcls);

    HWND hwnd;
    hwnd = CreateWindow("First MFC 2015","吉林大学", WS_OVERLAPPEDWINDOW, 0, 100, 600, 800, 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 WinSunPrnc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
    switch (uMsg)
    {
    case WM_CHAR:
        char szChar[20];
        sprintf(szChar, "char is %d", wParam);
        MessageBox(hwnd, szChar, TEXT("First MFC 2015"), 0);
        break;
    case WM_LBUTTONDOWN:
        MessageBox(hwnd, TEXT("muse clicked"), TEXT("mause"), 0);
        HDC hdc;
        hdc = GetDC(hwnd);
        TextOut(hdc, 0, 50, TEXT("its amazing"), strlen("its amazing"));
        ReleaseDC(hwnd, hdc);
        break;
    case WM_PAINT:
        HDC hDC;
        PAINTSTRUCT ps;
        hDC = BeginPaint(hwnd, &ps);
        TextOut(hDC, 0, 0, TEXT("翟羽翔"), strlen("翟羽翔"));
        EndPaint(hwnd, &ps);
        break;
    case WM_CLOSE:
        if (IDYES == MessageBox(hwnd, TEXT("是否真的结束?"), TEXT("First MFC"), MB_YESNO))
        {
            DestroyWindow(hwnd);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
}

运行之后只有进程没有界面是为什么。。

c mfc

6个回答

你的窗口回调函数在哪里?
WinSunPrnc在哪里?

sf1995
sf1995 我更新了,这个是看了孙鑫老师的视频我基本属于照抄,但是在vs2013里编译链接都能通过,任务管理器里也有WIN32.EXE的进程。但是就是没有界面
5 年多之前 回复
sf1995
sf1995 稍等
5 年多之前 回复

你代码中指定的类名和你创建窗口指定的类名不一致,你创建窗口的函数CreateWindow应该会失败。
wndcls.lpszClassName =** "First MFC";//类名化;**
。。。。
hwnd = CreateWindow(**"First MFC 2015",**

你用BCompare去比较下看看

用BCompary比比看

#include <Windows.h>                     //Windows程序需要包含Windows的头文件
#include <stdio.h>

//可以通过MSDN查看回调函数的声明方式
LRESULT CALLBACK WinQinProc(HWND hwnd,
       UINT uMsg,
       WPARAM wParam,
       LPARAM lParam
);




//WinMain.通过查看MSDN找到声明方式
int __stdcall WinMain( HINSTANCE hInstance,
   HINSTANCE hPrevInstance,
   LPSTR lpCmdLine,
   int nCmdShow
)
{
 //设计一个窗口类
 WNDCLASS wndcls;
 wndcls.cbClsExtra = 0;
 wndcls.cbWndExtra = 0;
 wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);     //白色的刷子
 wndcls.hCursor = LoadCursor(NULL, IDC_CROSS);      //十字形光标。因为标准的,所以第一个参数为NULL
 wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION);   //窗口上面是应用程序的图标
 wndcls.hInstance = hInstance;
 wndcls.lpfnWndProc = WinQinProc;                             //窗口过程函数。回调函数
 wndcls.lpszClassName = "qinwg";                                //类名
 wndcls.lpszMenuName = NULL;                                   //没有菜单
 wndcls.style = CS_HREDRAW | CS_VREDRAW;          //CS:类的样式---水平重绘、垂直重绘

 //注册窗口
 RegisterClass(&wndcls);

 //创建窗口
 HWND hwnd;
 hwnd = CreateWindow("qinwg", "qinwg's Program",WS_OVERLAPPEDWINDOW,
  50,50,600,400, NULL,NULL,hInstance,NULL);

 //显示窗口
 ShowWindow(hwnd, SW_SHOWNORMAL);
 UpdateWindow(hwnd);

 //消息循环机制
 MSG msg;
 //GetMessage取到WM_QUIT消息时,返回值为0,退出循环
 while (GetMessage(&msg, NULL, NULL, NULL))    //第二个参数为NULL,表示接受所有窗口消息
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }

 return 0;
}


//窗口过程函数实现过程
LRESULT CALLBACK WinQinProc(HWND hwnd,
       UINT uMsg,
       WPARAM wParam,
       LPARAM lParam
)
{
 HDC hDC;                              //Device Context的句柄
 switch(uMsg)                       //针对不同消息处理过程
 {
 case WM_PAINT:                  //窗口重绘时,触发的消息
  PAINTSTRUCT ps;
  hDC=BeginPaint(hwnd, &ps);              //BeginPaint/EndPaint只能用在WM_PAINT消息中
  TextOut(hDC, 50, 50, "This is first MFC Program!", strlen( "This is first MFC Program!"));
  EndPaint(hwnd, &ps);
  break;
 case WM_CHAR:                 //按下键盘字母键响应消息
  char cArry[20];
  memset(cArry, 0, 20);
  sprintf(cArry, "char is %d", wParam);
  MessageBox(hwnd, cArry, "WM_CHAR", MB_OKCANCEL);
  break;
 case WM_LBUTTONDOWN:
  MessageBox(hwnd, "LeftMouse Click", "WM_LBUTTONDOWN", MB_OKCANCEL);
  hDC = GetDC(hwnd);
  TextOut(hDC, 50, 100, "MFC Program!", strlen( "MFC Program!"));
  ReleaseDC(hwnd,hDC);
  break;
 case WM_CLOSE:
  if (IDOK == MessageBox(hwnd, "是否真的要退出?", "提示", MB_OKCANCEL))
  {
   DestroyWindow(hwnd);                     //销毁窗口,同时发送WM_DESTROY消息
  }
  break;
 case WM_DESTROY:
  PostQuitMessage(0);                            //发送WM_QUIT消息
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);            //对待不关心的消息,采用默认方式处理
 }

 return 0;
}

sf1995
sf1995 前辈你好,,我比较了我俩的代码,没有出入啊。。为什么呢?
5 年多之前 回复

我也在搞这个代码,搞得好苦啊!

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐