不调用任何与窗口相关的函数

Not calling any window-related functions

本文关键字:函数 窗口 调用 任何      更新时间:2023-10-16

我的程序不需要任何窗口(它只需要进行文件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