不调用任何与窗口相关的函数
Not calling any window-related functions
我的程序不需要任何窗口(它只需要进行文件I/O和一些计算),我正在努力使它尽可能小。我一开始就有这一切:
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("");
RegisterClass(&wc);
HWND hwnd = CreateWindow(TEXT(""),TEXT(""),0,0,0,0,0,HWND_MESSAGE,NULL,hInstance,NULL);
我把它拿出来了,所以现在我的程序只是WinMain和有用的东西,没有与窗口相关的调用。我还取消了user32.lib的链接。它似乎运行得很好,但这种糟糕的做法/会导致任何不可预见的问题吗?
最好从"Win32控制台应用程序"开始,Visual Studio向导将在默认情况下为您提供一个什么都不做的基本应用程序:
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
正如您所看到的,Windows应用程序根本不需要来执行任何操作。这同样适用于GUI应用程序,如果这确实是您所需要的(基本区别是GUI项目不会在控制台窗口中启动)。尽管项目向导一开始会不一致地给你一堆混乱的代码,但你可以安全地删除所有内容。一直到:
#include "stdafx.h"
int APIENTRY _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
return 0;
}
stdafx.h
标头是一个预编译标头,它基本上与#include <windows.h>
等效。但是,由于预编译头的工作方式,您必须首先包含stdafx.h
。这完全是一个不同的话题,已经在这个网站上广泛报道了。
不确定您删除了什么和留下了什么。如果有什么东西被遗留下来,那么将来可能会引起骚乱。
查看此简单代码以启动Winmain
#include <stdio.h>
#include <Windows.h>
#pragma comment(linker,"/SUBSYSTEM:WINDOWS")
int _stdcall WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
return 0;
}
Win32窗口的最低要求。http://www.codeproject.com/Articles/215690/Minimal-WinApi-Window
#include <windows.h>
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg = {0};
WNDCLASS wc = {0};
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);
wc.lpszClassName = L"minwindowsapp";
if( !RegisterClass(&wc) )
return 1;
if( !CreateWindow(wc.lpszClassName,
L"Minimal Windows Application",
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
0,0,640,480,0,0,hInstance,NULL))
return 2;
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
DispatchMessage( &msg );
return 0;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
更多阅读:
http://msdn.microsoft.com/en-us/library/bb384843.aspx
http://groups.engin.umd.umich.edu/CIS/course.des/cis577/lecture/win.htm
- 如何在窗口中使用注入的 dll 中的参数调用函数
- 在函数调用期间暂时禁用窗口
- 如何从 Gtk::窗口调用派生对象的析构函数
- 延迟函数使打开的框架窗口冻结,直到指定的时间过去
- 有没有办法一次运行 3 个创建窗口函数?
- 每次qt窗口获得焦点时调用函数
- 在多个函数中使用全局glfw窗口
- 渲染窗口无法跨多个函数工作
- 窗口上信号处理程序的异步安全写入函数
- Wow64DisableWow64FsRedirection() 函数在窗口 10 的 Visual Studio 中编
- 无法使用 CreateWindowEx 函数创建窗口
- 您如何在与打开窗口的功能的不同函数中使用Draw函数
- 从MEX函数中清除MATLAB命令窗口
- 如何在调用函数时将对象绘制到 sfml 窗口上
- 构造函数字符串参数未设置窗口标题
- 如何调用OnEraseBkgnd()在另一个函数中重新绘制窗口?(MFC)
- 加载 OpenGL > 1.1 函数窗口
- 是否有任何窗口函数可以使用域名获取所有OU(组织单位)名称
- 在Qt中关闭子对话窗口时的调用函数
- 在MS Visual Studio 2010监视窗口中评估模板函数