#include <windows.h>
#include <tchar.h>
#if defined __MINGW_H
#define _WIN32_IE 0x0400
#endif
#include <commctrl.h>
#include <string>
#include <vector>
typedef std::basic_string<TCHAR> ustring;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
int OnCreate(const HWND,CREATESTRUCT*);
void OnSize(const HWND hwnd,int,int,UINT);
void AddToolbarButton(const HWND,TBBUTTON&,int,const ustring&,
BYTE style=TBSTYLE_BUTTON,DWORD_PTR data=0,int cmd=-1,
BYTE state=TBSTATE_ENABLED);
HWND CreateToolbar(const HWND,const HINSTANCE,DWORD,const RECT&,const int);
inline int ErrMsg(const ustring&);
void StartCommonControls(DWORD);
enum {
IDC_TOOLBAR=200,
};
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR pStr,int nCmd)
{
ustring classname=_T("SIMPLEWND");
WNDCLASSEX wcx={0};
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.lpfnWndProc = WndProc;
wcx.hInstance = hInst;
wcx.hIcon = reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION,
IMAGE_ICON,0,0,LR_SHARED));
wcx.hCursor = reinterpret_cast<HCURSOR>(LoadImage(0,IDC_ARROW,
IMAGE_CURSOR,0,0,LR_SHARED));
wcx.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BTNFACE+1);
wcx.lpszClassName = classname.c_str();
if (!RegisterClassEx(&wcx))
{
ErrMsg(_T("Failed to register wnd class"));
return -1;
}
int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
int desktopheight=GetSystemMetrics(SM_CYSCREEN);
HWND hwnd=CreateWindowEx(0,
classname.c_str(),
_T("Common Controls - Toolbar"),
WS_OVERLAPPEDWINDOW,
desktopwidth/4,
desktopheight/4,
desktopwidth/2,
desktopheight/2,
0,
0,
hInst,
0);
if (!hwnd)
{
ErrMsg(_T("Failed to create wnd"));
return -1;
}
ShowWindow(hwnd,nCmd);
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg,0,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
{
return OnCreate(hwnd,reinterpret_cast<CREATESTRUCT*>(lParam));
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
case WM_SIZE:
{
OnSize(hwnd,LOWORD(lParam),HIWORD(lParam),static_cast<UINT>(wParam));
return 0;
}
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
}
int OnCreate(const HWND hwnd,CREATESTRUCT *cs)
{
RECT rc={0,0,0,0};
StartCommonControls(ICC_BAR_CLASSES);
HWND hToolbar=CreateToolbar(hwnd,cs->hInstance,TBSTYLE_FLAT|CCS_ADJUSTABLE|
CCS_NODIVIDER,rc,IDC_TOOLBAR);
SetWindowLongPtr(hwnd,GWLP_USERDATA,reinterpret_cast<LONG_PTR>(hToolbar));
TBADDBITMAP tbAddBmp={0};
std::vector<TBBUTTON> tbb(17);
SendMessage(hToolbar,TB_BUTTONSTRUCTSIZE,
static_cast<WPARAM>(sizeof(TBBUTTON)),0);
tbAddBmp.hInst=HINST_COMMCTRL;
tbAddBmp.nID=IDB_STD_SMALL_COLOR;
SendMessage(hToolbar,TB_ADDBITMAP,0,reinterpret_cast<WPARAM>(&tbAddBmp));
AddToolbarButton(hToolbar,tbb[0],STD_FILENEW,_T("New"));
AddToolbarButton(hToolbar,tbb[1],STD_FILEOPEN,_T("Open"));
AddToolbarButton(hToolbar,tbb[2],STD_FILESAVE,_T("Save"));
AddToolbarButton(hToolbar,tbb[3],STD_PRINTPRE,_T("PrintPre"));
AddToolbarButton(hToolbar,tbb[4],STD_PRINT,_T("Print"));
AddToolbarButton(hToolbar,tbb[5],0,_T(""),TBSTYLE_SEP);
AddToolbarButton(hToolbar,tbb[6],STD_DELETE,_T("Delete"));
AddToolbarButton(hToolbar,tbb[7],STD_COPY,_T("Copy"));
AddToolbarButton(hToolbar,tbb[8],STD_CUT,_T("Cut"));
AddToolbarButton(hToolbar,tbb[9],STD_PASTE,_T("Paste"));
AddToolbarButton(hToolbar,tbb[10],STD_REDOW,_T("Redo"));
AddToolbarButton(hToolbar,tbb[11],STD_UNDO,_T("Undo"));
AddToolbarButton(hToolbar,tbb[12],0,_T(""),TBSTYLE_SEP);
AddToolbarButton(hToolbar,tbb[13],STD_PROPERTIES,_T("Props"));
AddToolbarButton(hToolbar,tbb[14],STD_FIND,_T("Find"));
AddToolbarButton(hToolbar,tbb[15],STD_REPLACE,_T("Replace"));
AddToolbarButton(hToolbar,tbb[16],STD_HELP,_T("Help"));
SendMessage(hToolbar,TB_ADDBUTTONS,tbb.size(),
reinterpret_cast<LPARAM>(&tbb[0]));
return 0;
}
void OnSize(const HWND hwnd,int cx,int cy,UINT flags)
{
HWND hToolbar=reinterpret_cast<HWND>(static_cast<LONG_PTR>
(GetWindowLongPtr(hwnd,GWLP_USERDATA)));
MoveWindow(hToolbar,0,0,cx,0,TRUE);
}
void AddToolbarButton(const HWND hTb,TBBUTTON& tbb,int bmp,const ustring& txt,
BYTE style,DWORD_PTR data,int cmd,BYTE state)
{
tbb.iBitmap=bmp;
tbb.idCommand=cmd;
tbb.fsState=state;
tbb.fsStyle=style;
tbb.dwData=data;
tbb.iString=SendMessage(hTb,TB_ADDSTRING,0,
reinterpret_cast<LPARAM>(txt.c_str()));
}
HWND CreateToolbar(const HWND hParent,const HINSTANCE hInst,DWORD dwStyle,
const RECT& rc,const int id)
{
dwStyle|=WS_CHILD|WS_VISIBLE;
return CreateWindowEx(0,
TOOLBARCLASSNAME,
0,
dwStyle,
rc.left,
rc.top,
rc.right,
rc.bottom,
hParent,
reinterpret_cast<HMENU>(static_cast<INT_PTR>(id)),
hInst,
0);
}
inline int ErrMsg(const ustring& s)
{
return MessageBox(0,s.c_str(),_T("ERROR"),MB_OK|MB_ICONEXCLAMATION);
}
void StartCommonControls(DWORD flags)
{
INITCOMMONCONTROLSEX iccx;
iccx.dwSize=sizeof(INITCOMMONCONTROLSEX);
iccx.dwICC=flags;
InitCommonControlsEx(&iccx);
}
Comments (0)
You don't have permission to comment on this page.