使用QT框架创建窗口程序
在main函数中创建窗口程序
在qt中的其他类别里新建qmake项目,项目中包含一个文件FirstQt.pro
添加文件命名为main.cpp
代码示例
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include <QApplication> #include <QWidget>
int main(int argc, char **argv) { QApplication app(argc, argv);
QWidget w; w.setWindowTitle("Title"); w.show();
app.exec(); return 0; }
|
FirstQt.pro
1 2 3 4 5
| #FirstQt.pro QT += widgets
SOURCES += \ main.cpp
|
指针的方式调用类
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| #include <QApplication> #include <QWidget>
int main(int argc, char **argv) { QApplication app(argc, argv);
QWidget *w=new QWidget; w->setWindowTitle("Title"); w->show();
app.exec(); return 0; }
|
FirstQt.pro
1 2 3 4 5
| #FirstQt.pro QT += widgets
SOURCES += \ main.cpp
|
使用继承的方式调用Qt类
使用MFC框架创建窗口程序
MFC库文件配置方法
1).”配置属性”—>”常规”—>”项目默认值”, 改为”在静态库中使用 MFC”;
在main函数中调用MFC类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| #include <afxwin.h>
int main() { CWinApp theApp;
CFrameWnd* Frame = new CFrameWnd(); printf("sfas\n"); theApp.m_pMainWnd = Frame; Frame->Create(NULL, _T("最简单的窗口")); Frame->ShowWindow(SW_SHOW); theApp.Run(); return 0; }
|
使用继承的方式调用MFC中的类创建窗口程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| #include <afxwin.h>
class MyApp : public CWinApp{
public:
BOOL InitInstance()
{
CFrameWnd* Frame = new CFrameWnd();
m_pMainWnd = Frame;
Frame->Create(NULL, _T("最简单的窗口"));
Frame->ShowWindow(SW_SHOW);
return true;
}
};
int main() { MyApp theApp; theApp.InitInstance(); CFrameWnd* Frame = new CFrameWnd(); theApp.Run();
return 0; }
|
使用新建全局对象的方式创建窗口
(2).”配置属性”—>”链接器”—>”系统”—>”子系统”, 改为”窗口 (/SUBSYSTEM:WINDOWS)”;
入口点为WinMain
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include <afxwin.h>
class MyApp : public CWinApp{
public:
BOOL InitInstance()
{
CFrameWnd* Frame = new CFrameWnd();
m_pMainWnd = Frame;
Frame->Create(NULL, _T("最简单的窗口"));
Frame->ShowWindow(SW_SHOW);
return true;
}
}; MyApp theApp;
|
模仿MFC框架
模仿MFC隐藏入口函数WinMain实现隐藏main函数
Inherit.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| #include "Base.h" #include <iostream>
class Inherit :Base { public: int a = 1; virtual int BaseFun() { printf("inhert\n"); return 0; } };
Inherit in;
|
Base.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| #pragma once
class Base { public: Base(); ~Base();
virtual int BaseFun();
};
extern Base* g_pBase;
|
Base.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| #include "Base.h" #include <iostream>
Base* g_pBase = NULL;
Base::Base() { g_pBase = this; printf("Base\n");
}
Base::~Base() {
}
int Base::BaseFun() { printf("BaseFun\n"); return 0; }
|
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| #include "Base.h" #include <iostream>
extern "C" int __stdcall AfxMain(); extern "C" int __stdcall main(int argc, char* argv[]);
int AfxMain() { Base* pBase = g_pBase; pBase->BaseFun(); printf("AfxMain\n"); return 0; }
int main(int argc, char* argv[]) {
return AfxMain(); }
|
模仿MFC封装Win32程序
模仿Qt框架
模仿QT