c ++构建多个Windows应用程序 [英] c++ building multiple windows app

查看:58
本文介绍了c ++构建多个Windows应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两个窗口在纯c ++ / win32中构建一个应用程序,用户可以随时切换到该窗口。我写了这段代码:

i'm trying to build an app in pure c++/win32 with two windows which a user can switch to whenever he wants. i wrote this code:

#include <windows.h>
#include <windowsx.h>

HWND mainWnd, subWnd;
HINSTANCE hInst;

HWND btnShowSubWindow;

LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK SubWndProc(HWND, UINT, WPARAM, LPARAM);

void CreateMainWnd(){
	WNDCLASSEX wc; /* A properties struct of our window */
	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = MainWndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInst;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "MainWindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return;
	}
	
	mainWnd = CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"Main window",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInst,NULL);

	if(mainWnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return;
	}
}

void CreateSubWnd(){
	WNDCLASSEX wc; /* A properties struct of our window */
	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = SubWndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInst;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "SubWindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return;
	}
	
	subWnd = CreateWindowEx(WS_EX_CLIENTEDGE,wc.lpszClassName,"Secondary window",WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		640, /* width */
		480, /* height */
		NULL,NULL,hInst,NULL);

	if(subWnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return;
	}
}

/* This is where all the input to the window goes to */
LRESULT CALLBACK MainWndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		case WM_CREATE:{
			btnShowSubWindow = CreateWindow("button", "Show 2nd window", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
											50,50,150,20,
											hwnd,(HMENU)1000, hInst, NULL);
			break;
		}
		case WM_COMMAND:{
			switch(LOWORD(wParam)){
				case 1000:{
					ShowWindow(mainWnd, SW_HIDE);
					if(subWnd!=NULL)
						ShowWindow(subWnd, SW_SHOW);
					else
						ShowWindow(hwnd, SW_SHOW);
					break;
				}
			}
			break;
		}
		case WM_CLOSE:{
			if (MessageBox(hwnd, "Really quit?", "My application", MB_OKCANCEL) == IDOK)
   			{
        		DestroyWindow(hwnd);
    		}
			break;
		}
		/* Upon destruction, tell the main thread to stop */
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

LRESULT CALLBACK SubWndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
	switch(Message) {
		case WM_CLOSE:{
			CloseWindow(hwnd);
			ShowWindow(mainWnd, SW_SHOW);
			break;
		}
		case WM_DESTROY:{
			return 1;
			break;
		}
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	MSG msg; /* A temporary location for all messages */
	hInst = hInstance;
	CreateMainWnd();
	CreateSubWnd();
	ShowWindow(mainWnd, nCmdShow);
	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produce unreasonably high CPU usage
	*/
	while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&msg); /* Translate key codes to chars if present */
		DispatchMessage(&msg); /* Send it to WndProc */
	}
	return msg.wParam;
}

当应用程序运行时,我只能切换到第二个窗口一次,之后 - 主窗口隐藏自身并且没有任何反应。应用程序仍在运行,第二个窗口未显示且未被销毁。我做错了什么?

when the app is running i can switch to second window only once, after that - main window hides itself and nothing happens. app still running, second window isn't showing and it's not destroyed. what am i doing wrong?

p.s。请不要问我为什么不使用更新的框架,不建议在MFC或其他任何地方重写应用程序。

p.s. please don't ask why i'm not using more recent frameworks, do not suggest to rewrite the app in MFC or whatever.

推荐答案

你好fmastyaev。

Hi fmastyaev.

我建议你在
MSDN常规Windows桌面开发问题论坛
,更适合此类主题。

I suggest you to ask this question in the MSDN General Windows Desktop Development Issues Forum, which is more appropriate for this kind of topics.

再见。


这篇关于c ++构建多个Windows应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆