用纯C 语言怎样写出对话框嵌套程序?不用MFC
展开全部
MFC是相对比较简单的了,MFC中集成做界面需要的一些函数,而且连WinMain等最基本的函数都写在了类库里,让你可以直接写界面代码而不需要考虑C++中的一些基本元素,所以说是很方便的,如果你想学,推荐你看孙鑫老师的mfc视频教程,20课学完你必有小成。
不用MFC的话,就只能用Win32 Application建工程了,但你写一个界面相当于是把MFC类库中的东西重复一些而已。如下是一段不用MFC的界面,但在MFC的类库中完全可以找到这些代码:
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT wMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(0);
wndcls.hCursor = LoadCursor(NULL,0);
wndcls.hIcon = LoadIcon(NULL,IDI_ERROR);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = "xss";
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WinSunProc;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindow("xss","mfc第一课",WS_OVERLAPPEDWINDOW,0,0,600,600,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 WinSunProc(
HWND hwnd,
UINT wMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(wMsg)
{
case WM_CHAR:
char szchar[20];
sprintf(szchar,"char is %d",wParam);
MessageBox(hwnd,szchar,"xss",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"nihao","xss",0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,0,50,"123456",6);
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hdc1;
PAINTSTRUCT ps;
hdc1 = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的关闭?","xss",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,wMsg,wParam,lParam);
}
}
不用MFC的话,就只能用Win32 Application建工程了,但你写一个界面相当于是把MFC类库中的东西重复一些而已。如下是一段不用MFC的界面,但在MFC的类库中完全可以找到这些代码:
#include <windows.h>
#include <stdio.h>
LRESULT CALLBACK WinSunProc(
HWND hwnd,
UINT wMsg,
WPARAM wParam,
LPARAM lParam
);
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd
)
{
WNDCLASS wndcls;
wndcls.cbClsExtra = 0;
wndcls.cbWndExtra = 0;
wndcls.hbrBackground = (HBRUSH)GetStockObject(0);
wndcls.hCursor = LoadCursor(NULL,0);
wndcls.hIcon = LoadIcon(NULL,IDI_ERROR);
wndcls.lpszMenuName = NULL;
wndcls.lpszClassName = "xss";
wndcls.hInstance = hInstance;
wndcls.lpfnWndProc = WinSunProc;
wndcls.style = CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wndcls);
HWND hwnd;
hwnd = CreateWindow("xss","mfc第一课",WS_OVERLAPPEDWINDOW,0,0,600,600,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 WinSunProc(
HWND hwnd,
UINT wMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(wMsg)
{
case WM_CHAR:
char szchar[20];
sprintf(szchar,"char is %d",wParam);
MessageBox(hwnd,szchar,"xss",0);
break;
case WM_LBUTTONDOWN:
MessageBox(hwnd,"nihao","xss",0);
HDC hdc;
hdc = GetDC(hwnd);
TextOut(hdc,0,50,"123456",6);
ReleaseDC(hwnd,hdc);
break;
case WM_PAINT:
HDC hdc1;
PAINTSTRUCT ps;
hdc1 = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
if(IDYES==MessageBox(hwnd,"是否真的关闭?","xss",MB_YESNO))
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd,wMsg,wParam,lParam);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询