Win32控制台应用程序是我最早接触的程序吧。从写完第一个"Hello World!"起,我就想着是否能够画一个像Windows对话框一样的图形界面出来。可惜,那时年纪还小,隐约感觉这个东西太高端,就没敢尝试。后来学完C++,知道了MFC,呵呵,一直就用它做各种界面了。不过呢,心里头还萦绕着一个梦想——我还是要自己画界面。可惜,那时候太懒了。终于,拖到自己都感觉自己老了的时候,我想,还是完成以下那个小小的梦想吧。于是,Internet上就出现了一个到处查东西,翻东西的人。。
忙了两三个小时,终于弄明白了一些问题(以前都是一知半解,得过且过的。)。main和WinMain到底什么区别?能否用main实现WinMain呢?网友们给了很给力的答案——当然能。后面会附上几个参考地址,感兴趣的同志们可以去逛逛啊。
废话说了一堆,好了,下面贴代码吧。这是一个很初级的程序,仅仅是验证了一个思想而已,画了一个丑不拉几的窗口出来。当然,如果能够画出这个窗口,那么控件什么的也可以自己画了,毕竟这些东西差不太多吧。。。
啊,shit!代码还有些地方没有说明白,刚才自己试了一下发现忘记了几个地方。
首先,该项目得是多字符集的哦,Unicode编码太麻烦了。。
其次,忘记把声明拷贝过来了。。现在,重新贴代码。。
运行结果:一个很丑的窗口
// BeautifulMFC.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
int WINAPI MyWinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow);
BOOL InitApplication(HINSTANCE hinstance);
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow);
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
LPTSTR GetCmdLine(int argc,char *argv[]);
int main(int argc,char *argv[])
{
HINSTANCE hinstance = GetModuleHandle(NULL);
LPSTR lpCmdLine = GetCmdLine(argc,argv);
int nCmdShow = SW_SHOW;
MyWinMain(hinstance, 0, lpCmdLine, nCmdShow);
return 0;
}
int WINAPI MyWinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
if (!InitApplication(hinstance))
{
return FALSE;
}
if (!InitInstance(hinstance, nCmdShow))
{
return FALSE;
}
BOOL fGotMessage;
while ((fGotMessage = GetMessage(&msg, (HWND) NULL, 0, 0)) != 0 && fGotMessage != -1)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
UNREFERENCED_PARAMETER(lpCmdLine);
}
BOOL InitApplication(HINSTANCE hinstance)
{
WNDCLASS wcx;
// Fill in the window class structure with parameters
// that describe the main window.
wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes
wcx.lpfnWndProc = MainWndProc; // points to window procedure
wcx.cbClsExtra = 0; // no extra class memory
wcx.cbWndExtra = 0; // no extra window memory
wcx.hInstance = hinstance; // handle to instance
wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION); // predefined app. icon
wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // predefined arrow
wcx.hbrBackground = (HBRUSH)GetStockObject( WHITE_BRUSH); // white background brush
wcx.lpszMenuName = NULL;//"MainMenu"; // name of menu resource
wcx.lpszClassName = "MainWClass"; // name of window class
// Register the window class.
return RegisterClass(&wcx);
}
BOOL InitInstance(HINSTANCE hinstance, int nCmdShow)
{
HWND hwnd;
// Create the main window.
hwnd = CreateWindow(
"MainWClass", // name of window class
"Sample", // title-bar string
WS_OVERLAPPEDWINDOW, // top-level window
CW_USEDEFAULT, // default horizontal position
CW_USEDEFAULT, // default vertical position
CW_USEDEFAULT, // default width
CW_USEDEFAULT, // default height
(HWND) NULL, // no owner window
(HMENU) NULL, // use class menu
hinstance, // handle to application instance
(LPVOID) NULL); // no window-creation data
if (!hwnd)
{
printf("创建错误!!!\n");
return FALSE;
}
// Show the window and send a WM_PAINT message to the window
// procedure.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
return TRUE;
}
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_KEYDOWN:
break;
case WM_CLOSE:
DestroyWindow(hWnd);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
}
return NULL;
}
LPTSTR GetCmdLine(int argc,char *argv[])
{
int i=0;
int length=0;
char * cmdline;
if(argc<2)
return TEXT("");
for(i=1; i<argc; i++)
{
length=length + strlen(argv[i]);
}
cmdline = (char *)malloc(sizeof(char)*(length + argc -1));
strcpy(cmdline,argv[1]);
if(argc>2)
{
for(i=2;i<argc;i++)
{
strcat(cmdline," ");
strcat(cmdline,argv[i]);
}
}
return TEXT(cmdline);
}