首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

VC++自定义窗口,编译经过,但窗口没有弹出来,进程里可以找的到

2013-08-09 
VC++自定义窗口,编译通过,但窗口没有弹出来,进程里可以找的到。今天写抄代码时发现两个问题:一个是我写的时

VC++自定义窗口,编译通过,但窗口没有弹出来,进程里可以找的到。
今天写抄代码时发现两个问题:
一个是我写的时候发现VC环境没有给我联想,比如我写完 "对象."的时候,本来会弹出这个对象的公有属性和方法列表的,但它什么也没提示(我用的是win7系统)。。
另一个问题就是自定义窗口的问题,我按孙鑫老师的教程写的,编译通过,在进程里也可以找的到,但窗口没有弹出来。
这是怎么回事啊?


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

//窗口过程函数声明
LRESULT CALLBACK WindowSunProc(
HWND hwnd,//窗口句柄
UINT uMsg,//信息类型
WPARAM wParam,//第一个信息参数
LPARAM lParam//第二个信息参数
);

int WINAPI WinMain(
HINSTANCE hInstance,    //当前实例句柄
HINSTANCE hPrevInstance,//前一个实例句柄
LPSTR lpCmdLine,        //命令行
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);     
wndcls.hInstance=hInstance;     
wndcls.lpfnWndProc=WindowSunProc;      
wndcls.lpszClassName="MyFirstWin";     
wndcls.lpszMenuName=NULL;   
wndcls.style=CS_HREDRAW|CS_VREDRAW;     
//注册窗口类
RegisterClass(&wndcls);
//创建窗口类
HWND hwnd;
hwnd=CreateWindow("MyFirstWin","MyFirstWin",WS_OVERLAPPEDWINDOW,
0,0,600,400,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 WindowSunProc(
HWND hwnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam
)
{return 0;
}


[解决办法]
你看看你的消息循环,直接就return了,没循环了,窗口咋个显示
[解决办法]
我给楼主一份源码好了

// 32win.cpp : Defines the entry point for the application.


//

#include "stdafx.h"
#include "resource.h"

#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:
ATOMMyRegisterClass(HINSTANCE hInstance);
BOOLInitInstance(HINSTANCE, int);
LRESULT CALLBACKWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACKAbout(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_MY32WIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

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

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

// 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(NULL,IDI_ERROR);
wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName= NULL;
wcex.lpszClassName= "hello world";
wcex.hIconSm= NULL;

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("hello world", 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;
}


[解决办法]
引用:
mstlq谢谢你的代码,可是我换掉代码时编译提示这个错误:
Cannot open include file: 'stdafx.h': No such file or directory
我初学VC++ 能给我说明一下吗?


预编译。。包含这个头文件就好了。。。  

热点排行