Skip to content

Instantly share code, notes, and snippets.

@tuldok89
Created April 7, 2014 14:20
Show Gist options
  • Save tuldok89/10021197 to your computer and use it in GitHub Desktop.
Save tuldok89/10021197 to your computer and use it in GitHub Desktop.
Win32 C/C++ Sample Code
#include <windows.h>
#include <tchar.h>
#include "MainWindow.h"
int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
MainWindow *winMain = new MainWindow(hInstance);
if (!winMain->Run(nCmdShow))
{
delete winMain;
return 1;
}
while (GetMessage(&msg, NULL, 0, 0))
{
// translate virtual-key messages into character messages
TranslateMessage(&msg);
// Send message to WindowProcedure
DispatchMessage(&msg);
}
delete winMain;
return msg.wParam;
}
#include <windows.h>
#include "MainWindow.h"
const TCHAR* MainWindow::m_szClassName = _T("DrawLite");
HINSTANCE MainWindow::m_hInstance = nullptr;
MainWindow::MainWindow(HINSTANCE hInstance)
{
m_hInstance = hInstance;
m_wndClass.cbSize = sizeof(WNDCLASSEX);
m_wndClass.style = CS_DBLCLKS;
m_wndClass.lpfnWndProc = MainWndProc;
m_wndClass.cbClsExtra = 0;
m_wndClass.cbWndExtra = 0;
m_wndClass.hInstance = hInstance;
m_wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
m_wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
m_wndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW);
m_wndClass.lpszMenuName = NULL;
m_wndClass.lpszClassName = m_szClassName;
m_wndClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
}
MainWindow::~MainWindow()
{
}
LRESULT CALLBACK MainWindow::MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
TCHAR str[256];
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
POINT pt;
pt.x = LOWORD(lParam);
pt.y = HIWORD(lParam);
wsprintf(str, _T("Coordinates are \nX=%i and Y=%i"), pt.x, pt.y);
MessageBox(hwnd, str, _T("Left Button Clicked"), MB_OK);
break;
case WM_CHAR:
wsprintf(str, _T("Pressed %c key"), wParam);
MessageBox(hwnd, str, _T("Key Pressed"), MB_OK);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
bool MainWindow::Run(int nCmdShow)
{
if (!RegisterClassEx(&m_wndClass))
return false;
m_hwnd = CreateWindowEx(
0,
m_szClassName,
_T("Draw Lite"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
500,
400,
NULL,
NULL,
m_hInstance,
NULL);
if (!m_hwnd)
return false;
ShowWindow(m_hwnd, nCmdShow);
return true;
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <windows.h>
#include <tchar.h>
class MainWindow
{
public:
MainWindow(HINSTANCE hInstance);
~MainWindow();
static LRESULT CALLBACK MainWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
bool Run(int nCmdShow);
private:
WNDCLASSEX m_wndClass;
static HINSTANCE m_hInstance;
HWND m_hwnd;
static const TCHAR* m_szClassName;
void leftMouseClick(HWND hwd, LPARAM lParam);
};
#endif // MAINWINDOW_H
@jonathanforhan
Copy link

jonathanforhan commented Aug 5, 2022

Awesome!!!

if anyone else uses this remember to change it to windows in properties -> linker -> system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment