Windows程序设计之控制台程序的定时器使用方法

一、函数原型

VOID CALLBACK TimerProc(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
);

UINT_PTR SetTimer(
  HWND hWnd,              // handle to window
  UINT_PTR nIDEvent,      // timer identifier
  UINT uElapse,           // time-out value
  TIMERPROC lpTimerFunc   // timer procedure
);

Continue reading

Windows程序设计之创建、打开、读写文件方法介绍

一、创建或打开文件

HANDLE CreateFile(
  LPCTSTR lpFileName,                         // file name
  DWORD dwDesiredAccess,                      // access mode
  DWORD dwShareMode,                          // share mode
  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  DWORD dwCreationDisposition,                // how to create
  DWORD dwFlagsAndAttributes,                 // file attributes
  HANDLE hTemplateFile                        // handle to template file
);

Continue reading

C++程序设计之QEventLoop实现进入和退出事件循环

QEventLoop类为我们提供了一种进入和退出事件循环的方法。在任何时候,你都可以创建一个QEventLoop实例,并通过调用exec()来启动一个事件循环。在循环期间,主动调用exit()可以使exec()强制返回。

void Worker::run()
{
    QEventLoop loop;
    QTimer *timer = new QTimer;
    connect(timer, &QTimer::timeout, this, [&]()
    {
        static int count = 0;
        qDebug() << QDateTime::currentDateTime().toString();
        if(++count == 10) loop.quit();
    },
    Qt::DirectConnection);
    timer->start(1000);
    loop.exec();
}

C++程序设计之Boost序列化与反序列化自定义数据结构

Boost Serialization的主要概念是归档。归档的文件是相当于序列化的C++对象的一个字节流。对象可以通过序列化添加到归档文件,反过来也可从归档文件中加载。

一、自定义数据结构

class DateTime
{
public:
    DateTime(int y=0, int m=0, int d=0)
    {year = y; month = m; day = d;}
    
    void Display(void)
    {cout << year << "-" << month << "-" << day << endl;}
    
    template<class Archive>
    friend void serialize(Archive &ar, DateTime &dt, const unsigned int version);
    
private:
    int year;
    int month;
    int day;
};

Continue reading

C++程序设计之Qt自定义数据类型的序列化与反序列化

一、自定义数据类型

class Data_Record
{
public:
    Data_Record(int id = 0, QString name = "", int age = 0, QString addr = "");
    void display();
    friend QDataStream &operator<<(QDataStream &stream, const Data_Record &data); // 序列化
    friend QDataStream &operator>>(QDataStream &stream, Data_Record &data);       // 反序列化

private:
    int ID;
    QString name;
    int age;
    QString addr;
};

Data_Record::Data_Record(int ID, QString name, int age, QString addr)
{
    this->ID = ID;
    this->name = name;
    this->age = age;
    this->addr = addr;
}

void Data_Record::display()
{
    qDebug() << ID << name << age << addr;
}

QDataStream &operator<<(QDataStream &stream, const Data_Record &data)
{
    stream << data.ID << data.name << data.age << data.addr;
    return stream;
}

QDataStream &operator>>(QDataStream &stream, Data_Record &data)
{
    stream >> data.ID >> data.name >> data.age >> data.addr;

    return stream;
}

Continue reading

C++程序设计之Qt基本数据类型的序列化与反序列化

一、Qt基本数据类型的序列化

    QFile file1("QText.dat");
    if(!file1.open(QIODevice::WriteOnly | QIODevice::Text)) return;
    QDataStream out(&file1);

    QString str1 = "中华人民共和国";
    int iData1 = 10000;
    double dData1 = 3.14;
    QVector<int> vec1 = {0,1,2,3,4,5,6,7,8,9};

    out << str1 << iData1 << dData1 << vec1;
    file1.close();

Continue reading

Windows程序设计之第一个基本窗口程序示例

#include <Windows.h>
#include <tchar.h>


// 窗口处理函数
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0); // 发送WM_QUIT消息,结束程序
        break;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam); // 系统默认窗口处理函数
}

INT WINAPI _tWinMain(
    _In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPTSTR lpCmdLine,
    _In_ INT nShowCmd)
{
    WNDCLASS wc;
    HWND hWnd;
    MSG msg;

    // 注册窗口类
    ZeroMemory(&wc, sizeof(wc));
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = TEXT("BasicWindow");

    RegisterClass(&wc);
    
    // 创建窗口
    hWnd = CreateWindow(TEXT("BasicWindow"), TEXT("BasicWindow"),
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
 
    // 显示窗口
    ShowWindow(hWnd, nShowCmd);
    UpdateWindow(hWnd);

    // 消息循环
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

Windows程序设计之基本常用函数和类介绍

一、窗口处理函数

LRESULT CALLBACK WindowProc(
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

二、窗口类

typedef struct _WNDCLASS { 
    UINT       style; 
    WNDPROC    lpfnWndProc; 
    int        cbClsExtra; 
    int        cbWndExtra; 
    HINSTANCE  hInstance; 
    HICON      hIcon; 
    HCURSOR    hCursor; 
    HBRUSH     hbrBackground; 
    LPCTSTR    lpszMenuName; 
    LPCTSTR    lpszClassName; 
} WNDCLASS, *PWNDCLASS; 

ATOM RegisterClass(
  CONST WNDCLASS *lpWndClass  // class data
);

Continue reading