你需要按 Win32 程序的框架来,不可能在 console 来显示其它窗体吧。
// Win32MultiPlatformApp.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "Win32MultiPlatformApp.h"
#define MAX_LOADSTRING 100
// 全局变量:
HINSTANCE g_hInst; // 当前实例
#ifdef SHELL_AYGSHELL
HWND g_hWndMenuBar; // 菜单栏句柄
#else // SHELL_AYGSHELL
HWND g_hWndCommandBar; // 命令栏句柄
#endif // SHELL_AYGSHELL
// 此代码模块中包含的函数的前向声明:
ATOM MyRegisterClass(HINSTANCE, LPTSTR);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
#ifndef WIN32_PLATFORM_WFSP
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
#endif // !WIN32_PLATFORM_WFSP
#ifdef _UNICODE
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
#else
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
#endif
{
MSG msg;
// 执行应用程序初始化:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
#ifndef WIN32_PLATFORM_WFSP
HACCEL hAccelTable;
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_WIN32MULTIPLATFORMAPP));
#endif // !WIN32_PLATFORM_WFSP
// 主消息循环:
while (GetMessage(&msg, NULL, 0, 0))
{
#ifndef WIN32_PLATFORM_WFSP
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
#endif // !WIN32_PLATFORM_WFSP
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// 函数: MyRegisterClass()
//
// 目的: 注册窗口类。
//
// 注释:
//
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass)
{
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_WIN32MULTIPLATFORMAPP));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
//
// 函数: InitInstance(HINSTANCE, int)
//
// 目的: 保存实例句柄并创建主窗口
//
// 注释:
//
// 在此函数中,我们在全局变量中保存实例句柄并
// 创建和显示主程序窗口。
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
TCHAR szTitle[MAX_LOADSTRING]; // 标题栏文本
TCHAR szWindowClass[MAX_LOADSTRING]; // 主窗口类名
g_hInst = hInstance; // 将实例句柄存储在全局变量中
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
// 在应用程序初始化期间,应调用一次 SHInitExtraControls 以初始化
// 所有设备特定控件,例如,CAPEDIT 和 SIPPREF。
// SHInitExtraControls();
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32MULTIPLATFORMAPP, szWindowClass, MAX_LOADSTRING);
#if defined(WIN32_PLATFORM_PSPC) || defined(WIN32_PLATFORM_WFSP)
//如果它已经在运行,则将焦点置于窗口上,然后退出
hWnd = FindWindow(szWindowClass, szTitle);
if (hWnd)
{
// 将焦点置于最前面的子窗口
// “| 0x00000001”用于将所有附属窗口置于前台并
// 激活这些窗口。
SetForegroundWindow((HWND)((ULONG) hWnd | 0x00000001));
return 0;
}
#endif // WIN32_PLATFORM_PSPC || WIN32_PLATFORM_WFSP
if (!MyRegisterClass(hInstance, TEXT("szWindowClass")))
{
return FALSE;
}
hWnd = CreateWindow(szWindowClass, _T("szTitle"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
#ifdef WIN32_PLATFORM_PSPC
// 使用 CW_USEDEFAULT 创建主窗口时,将不会考虑菜单栏的高度(如果创建了一个
// 菜单栏)。因此,我们要在创建窗口后调整其大小
// 如果菜单栏存在
if (g_hWndMenuBar)
{
RECT rc;
RECT rcMenuBar;
GetWindowRect(hWnd, &rc);
GetWindowRect(g_hWndMenuBar, &rcMenuBar);
rc.bottom -= (rcMenuBar.bottom - rcMenuBar.top);
MoveWindow(hWnd, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, FALSE);
}
#endif // WIN32_PLATFORM_PSPC
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
#ifndef SHELL_AYGSHELL
if (g_hWndCommandBar)
{
// CommandBar_Show(g_hWndCommandBar, TRUE);
}
#endif // !SHELL_AYGSHELL
return TRUE;
}
//
// 函数: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 目的: 处理主窗口的消息。
//
// WM_COMMAND - 处理应用程序菜单
// WM_PAINT - 绘制主窗口
// WM_DESTROY - 发送退出消息并返回
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
static SHACTIVATEINFO s_sai;
#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// 分析菜单选择:
switch (wmId)
{
#ifndef WIN32_PLATFORM_WFSP
case IDM_HELP_ABOUT:
DialogBox(g_hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, About);
break;
#endif // !WIN32_PLATFORM_WFSP
#ifndef SHELL_AYGSHELL
case IDM_FILE_EXIT:
DestroyWindow(hWnd);
break;
#endif // !SHELL_AYGSHELL
#ifdef WIN32_PLATFORM_WFSP
case IDM_OK:
DestroyWindow(hWnd);
break;
#endif // WIN32_PLATFORM_WFSP
#ifdef WIN32_PLATFORM_PSPC
case IDM_OK:
SendMessage (hWnd, WM_CLOSE, 0, 0);
break;
#endif // WIN32_PLATFORM_PSPC
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_CREATE:
#ifndef SHELL_AYGSHELL
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
SHMENUBARINFO mbi;
memset(&mbi, 0, sizeof(SHMENUBARINFO));
mbi.cbSize = sizeof(SHMENUBARINFO);
mbi.hwndParent = hWnd;
mbi.nToolBarId = IDR_MENU;
mbi.hInstRes = g_hInst;
if (!SHCreateMenuBar(&mbi))
{
g_hWndMenuBar = NULL;
}
else
{
g_hWndMenuBar = mbi.hwndMB;
}
#ifndef WIN32_PLATFORM_WFSP
// 初始化外壳程序激活信息结构
memset(&s_sai, 0, sizeof (s_sai));
s_sai.cbSize = sizeof (s_sai);
#endif // !WIN32_PLATFORM_WFSP
#endif // SHELL_AYGSHELL
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: 在此添加任意绘图代码...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
#ifndef SHELL_AYGSHELL
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
CommandBar_Destroy(g_hWndMenuBar);
#endif // SHELL_AYGSHELL
PostQuitMessage(0);
break;
#if defined(SHELL_AYGSHELL) && !defined(WIN32_PLATFORM_WFSP)
case WM_ACTIVATE:
// 向外壳程序通知我们的激活消息
SHHandleWMActivate(hWnd, wParam, lParam, &s_sai, FALSE);
break;
case WM_SETTINGCHANGE:
SHHandleWMSettingChange(hWnd, wParam, lParam, &s_sai);
break;
#endif // SHELL_AYGSHELL && !WIN32_PLATFORM_WFSP
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
#ifndef WIN32_PLATFORM_WFSP
// “关于”框的消息处理程序。
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
#ifndef SHELL_AYGSHELL
RECT rectChild, rectParent;
int DlgWidth, DlgHeight; // 以像素为单位的对话框宽度和高度
int NewPosX, NewPosY;
// 设法使“关于”对话框居中显示
if (GetWindowRect(hDlg, &rectChild))
{
GetClientRect(GetParent(hDlg), &rectParent);
DlgWidth = rectChild.right - rectChild.left;
DlgHeight = rectChild.bottom - rectChild.top ;
NewPosX = (rectParent.right - rectParent.left - DlgWidth) / 2;
NewPosY = (rectParent.bottom - rectParent.top - DlgHeight) / 2;
// 如果“关于”框比实际屏幕大
if (NewPosX < 0) NewPosX = 0;
if (NewPosY < 0) NewPosY = 0;
SetWindowPos(hDlg, 0, NewPosX, NewPosY,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
{
// 创建一个“完成”按钮并调整其大小。
SHINITDLGINFO shidi;
shidi.dwMask = SHIDIM_FLAGS;
shidi.dwFlags = SHIDIF_DONEBUTTON | SHIDIF_SIPDOWN | SHIDIF_SIZEDLGFULLSCREEN | SHIDIF_EMPTYMENU;
shidi.hDlg = hDlg;
SHInitDialog(&shidi);
}
#endif // SHELL_AYGSHELL
return (INT_PTR)TRUE;
case WM_COMMAND:
#ifndef SHELL_AYGSHELL
if ((LOWORD(wParam) == IDOK) || (LOWORD(wParam) == IDCANCEL))
#endif // !SHELL_AYGSHELL
#ifdef SHELL_AYGSHELL
if (LOWORD(wParam) == IDOK)
#endif
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
case WM_CLOSE:
EndDialog(hDlg, message);
return (INT_PTR)TRUE;
}
return (INT_PTR)FALSE;
}
#endif // !WIN32_PLATFORM_WFSP