| Kavan |
10-24-2011 06:44 PM |
So, you guys want to post any code for the things you're making with C++?
I'll set the ball rolling with a text editor I'm trying to piece together. It's my first one. :p
PHP Code:
#include <Windows.h>
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#define ID_EDIT 1001
#define ID_BUTTON 1002
#define ID_OPEN 9001
#define ID_SAVE 9002
#define ID_EXIT 9003
#define ID_COPY 9004
static TCHAR szWindowClass[] = _T("win32_test");
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
{
HMENU hMenu, hSubMenu;
HICON hIcon, hIconSm;
hMenu = CreateMenu();
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_OPEN, _T("Open"));
AppendMenu(hSubMenu, MF_STRING, ID_SAVE, _T("Save"));
AppendMenu(hSubMenu, MF_STRING, ID_EXIT, _T("Exit"));
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, _T("File"));
hSubMenu = CreatePopupMenu();
AppendMenu(hSubMenu, MF_STRING, ID_COPY, _T("Copy"));
AppendMenu(hMenu, MF_STRING | MF_POPUP, (UINT)hSubMenu, _T("Edit"));
SetMenu(hWnd, hMenu);
HWND hEdit = CreateWindowEx(
WS_EX_CLIENTEDGE,
_T("EDIT"),
_T(""),
WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_AUTOVSCROLL,
0, 0,
100, 100,
hWnd,
(HMENU)ID_EDIT,
GetModuleHandle(NULL),
NULL);
HGDIOBJ hfDefault = GetStockObject(DEFAULT_GUI_FONT);
SendMessage(hEdit, WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(false, 0));
HWND hButton = CreateWindowEx(
NULL,
_T("BUTTON"),
_T("Button that does something"),
WS_BORDER | WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0, 0,
100, 30,
hWnd,
(HMENU)ID_BUTTON,
GetModuleHandle(NULL),
NULL);
}
break;
case WM_SIZE:
{
HWND hEdit, hButton;
RECT rcClient;
GetClientRect(hWnd, &rcClient);
hEdit = GetDlgItem(hWnd, ID_EDIT);
SetWindowPos(hEdit, NULL, 0, 0, rcClient.right, rcClient.bottom - 50, SWP_NOZORDER);
hButton = GetDlgItem(hWnd, ID_BUTTON);
SetWindowPos(hButton, NULL, 10, rcClient.bottom - 40, 200, 30, SWP_NOZORDER);
}
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_EXIT:
PostQuitMessage(0);
break;
case ID_COPY:
MessageBox(NULL, _T("This popup button does absolutely nothing as of yet."), _T("Notice"), NULL);
break;
case ID_OPEN:
{
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFilter = _T("Text Files (*.txt)\0*.txt\0All files (*.*)\0*.*\0");
ofn.lpstrFile = _T("");
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = _T("txt");
if(GetOpenFileName(&ofn))
{
// Going to do this later...
}
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
if(!RegisterClassEx(&wcex))
{
MessageBox(NULL, _T("Call to RegisterClassEx failed!"), _T("Kavan's Text Editor"), NULL);
return 1;
}
HWND hWnd = CreateWindow(
szWindowClass,
_T("Kavan's Text Editor"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL,
NULL,
hInstance,
NULL);
if(!hWnd)
{
MessageBox(NULL, _T("Call to CreateWindow failed"), _T("Kavan's Text Editor"), NULL);
return 1;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
Some feedback from more knowledgeable beings would be appreciated, as I'm not exactly sure how to open a text file and place the text in it onto my window.
|