BusyMonkey

no one can stop you, but yourself

控制台下创建窗口和WinMain入口创建窗口简单程序

控制台创建窗口(同时有控制台窗口和新创建的窗口)(项目类型:控制台应用程序)

#include "windows.h"  
#include "TCHAR.h"  

LRESULT CALLBACK WindowProc(
	HWND hwnd,
	UINT uMsg,
	WPARAM wParam,
	LPARAM lParam
	);


int _tmain(int argc, _TCHAR* argv[])
{
	HINSTANCE hInstance;
	hInstance = GetModuleHandle(NULL);
	WNDCLASS Draw;
	Draw.cbClsExtra = 0;
	Draw.cbWndExtra = 0;
	Draw.hCursor = LoadCursor(hInstance, IDC_ARROW);;
	Draw.hIcon = LoadIcon(hInstance, IDI_APPLICATION);;
	Draw.lpszMenuName = NULL;
	Draw.style = CS_HREDRAW | CS_VREDRAW;
	Draw.hbrBackground = (HBRUSH)COLOR_WINDOW;
	Draw.lpfnWndProc = WindowProc;
	Draw.lpszClassName = _T("DDraw");
	Draw.hInstance = hInstance;


	RegisterClass(&Draw);

	HWND hwnd = CreateWindow(
		_T("DDraw"),           //上面注册的类名,要完全一致    
		L"绘制",  //窗口标题文字    
		WS_OVERLAPPEDWINDOW, //窗口外观样式    
		38,             //窗口相对于父级的X坐标    
		20,             //窗口相对于父级的Y坐标    
		640,                //窗口的宽度    
		480,                //窗口的高度    
		NULL,               //没有父窗口,为NULL    
		NULL,               //没有菜单,为NULL    
		hInstance,          //当前应用程序的实例句柄    
		NULL);              //没有附加数据,为NULL    

	// 显示窗口    
	ShowWindow(hwnd, SW_SHOW);

	// 更新窗口    
	UpdateWindow(hwnd);

	// 消息循环    
	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
}

// 消息处理函数的实现  
LRESULT CALLBACK WindowProc(
	_In_  HWND hwnd,
	_In_  UINT uMsg,
	_In_  WPARAM wParam,
	_In_  LPARAM lParam
	)
{
	switch (uMsg)
	{
	case WM_DESTROY:
	{
					   PostQuitMessage(0);
					   return 0;
	}
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


WinMain入口下创建窗口(只有新创建的窗口)(项目类型:windows应用程序)

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

LRESULT CALLBACK WindowProc(
	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 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 = WindowProc;
	wndcls.lpszClassName = _T("Hello");
	wndcls.lpszMenuName = NULL;
	wndcls.style = CS_HREDRAW | CS_VREDRAW;
	RegisterClass(&wndcls);

	HWND hwnd;
	hwnd = CreateWindow(_T("Hello"), L"World", 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 WindowProc(
	  HWND hwnd,
	  UINT uMsg,
	  WPARAM wParam,
	  LPARAM lParam
)
{
	switch (uMsg)
	{
	case WM_DESTROY:
	{
					   PostQuitMessage(0);
					   return 0;
	}
	}
	return DefWindowProc(hwnd, uMsg, wParam, lParam);
}


阅读更多

扫码向博主提问

去开通我的Chat快问

dopamy_busymonkey

没人能阻挡你,除了你自己
  • 擅长领域:
  • java
  • 图像处理
  • js
  • ios
  • python
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dopamy_BusyMonkey/article/details/46801481
文章标签: C++
个人分类: C++
上一篇DirectShow VS2013 控制台下捕捉摄像头并且显示
下一篇C++中各种类型的成员变量的初始化方法
想对作者说点什么? 我来说一句

WINAPI WinMain

WINAPI WinMain用法

lichji2016 lichji2016

2016-04-13 23:19:22

阅读数:177

没有更多推荐了,返回首页

关闭
关闭