windows 编程模板

win32 消息
#include <Windows.h>

#include <stdio.h>
#include <string.h>

// 用于在 win32 中获取控制台
HANDLE g_hOutput = NULL;

// 全局保存实例对象
HINSTANCE g_hInstance = NULL;


LRESULT CALLBACK WnProc(
	HWND hwnd,
	UINT msg,
	WPARAM wparam,
	LPARAM lparam)
{
	switch (msg)
	{
	case WM_CREATE:
	{
		break;
	}
	case WM_CLOSE:
	{
		DestroyWindow(hwnd);
		PostQuitMessage(0);
	}
	default:
		break;
	}

	return DefWindowProc(hwnd, msg, wparam, lparam);
}

int WINAPI WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	PSTR szCmdLine,
	int nCmdShow)
{
	g_hInstance = hInstance;

	// 在 win32 中获取控制台
	AllocConsole();
	g_hOutput = GetStdHandle(STD_OUTPUT_HANDLE);


	WNDCLASS wndclass;
	ZeroMemory(&wndclass, sizeof(WNDCLASS));

	wndclass.lpfnWndProc = WnProc;
	wndclass.lpszClassName = "MyWndClass";

	if (!RegisterClass(&wndclass))
	{
		MessageBox(NULL, TEXT("regist windwos class failed!"), "failed", MB_ICONERROR);
		return 0;
	}

    HWND hwnd = CreateWindow(
        wndclass.lpszClassName,
        "window name",
        // WS_OVERLAPPEDWINDOW,
        WS_EX_TOOLWINDOW |
            WS_EX_NOACTIVATE |
            WS_EX_TRANSPARENT |
            WS_EX_LAYERED |
            WS_EX_TOPMOST,
        CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
        NULL,
        NULL,
        hInstance,
        NULL);

	ShowWindow(hwnd, SW_NORMAL);
	UpdateWindow(hwnd);

	MSG msg;
	while (GetMessage(&msg, NULL, 0, 0))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	return msg.wParam;
}
MFC 对话框模板
#include <afxwin.h>
#include "resource.h"

class CMyDlg : public CDialog
{
    DECLARE_MESSAGE_MAP()
public:
    CMyDlg() : CDialog(IDD_DIALOG1) {};

    virtual BOOL OnInitDialog();
    virtual void DoDataExchange(CDataExchange* pDX);

};
BEGIN_MESSAGE_MAP(CMyDlg, CDialog)

END_MESSAGE_MAP()
BOOL CMyDlg::OnInitDialog()
{

    return TRUE;
}
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{

}


class CMyWinApp : public CWinApp
{
public:
    virtual BOOL InitInstance();
};
BOOL CMyWinApp::InitInstance()
{
    CMyDlg myDlg;
    this->m_pMainWnd = &myDlg;
    myDlg.DoModal();
    return FALSE;
}

CMyWinApp theApp;

 

posted @ 2023-05-19 20:31  某某人8265  阅读(24)  评论(0编辑  收藏  举报