#define VC_EXTRALEAN
#define _WIN32_WINNT 0x0501
#define WINVER 0x0501
#undef UNICODE
#include <windows.h>
#include <commctrl.h>
#pragma comment(lib,"comctl32.lib")
#define PACKAGE_CLASS "MyClass"
#define PACKAGE_STRING "MyString"
typedef struct we_csg_s {
int nDone;
HINSTANCE hInstance;
HWND hWndMain;
HWND hWndRebar1;
HWND hWndTBar1;
HWND hWndTBar2;
HWND hWndSBar1;
} we_csg_t;
we_csg_t *hWECSG = NULL;
int wnd_main_init();
void wnd_main_done();
int main_init( HINSTANCE hInstance ) {
INITCOMMONCONTROLSEX ctrls;
// get memory for globals
hWECSG = (we_csg_t*)malloc( sizeof(we_csg_t) );
if( hWECSG == NULL ) return 0;
memset( hWECSG, 0, sizeof(we_csg_t) );
hWECSG->hInstance = hInstance;
ctrls.dwSize = sizeof(INITCOMMONCONTROLSEX);
ctrls.dwICC = ICC_BAR_CLASSES|ICC_COOL_CLASSES;
InitCommonControlsEx( &ctrls );
// init main window
if( !wnd_main_init() ) return 0;
return 1;
}
void main_done() {
wnd_main_done();
if( hWECSG != NULL ) {
free( hWECSG );
hWECSG = NULL;
}
}
void main_do() {
MSG msg;
while( !hWECSG->nDone ) {
if(GetMessage( &msg, NULL, 0, 0 )) {
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}
}
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
if( !main_init( hInstance ) ) {
main_done();
return 1;
}
main_do();
main_done();
return 0;
}
LRESULT wnd_main_proc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) {
switch( uMsg ) {
case WM_CREATE:
break;
case WM_QUIT:
SendMessage( hWnd, WM_DESTROY, 0, 0 );
break;
case WM_DESTROY:
hWECSG->nDone = 1;
break;
case WM_SIZE:
SendMessage( hWECSG->hWndRebar1, uMsg, 0, 0 );
SendMessage( hWECSG->hWndSBar1, uMsg, 0, 0 );
break;
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}
int wnd_main_createStatusbar() {
DWORD dwStyle;
dwStyle = WS_BORDER|WS_CHILD|WS_VISIBLE;
hWECSG->hWndSBar1 = CreateWindowEx( WS_EX_TOOLWINDOW, STATUSCLASSNAME, "", dwStyle, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, hWECSG->hWndMain, NULL, hWECSG->hInstance, NULL );
if( hWECSG->hWndSBar1 == NULL ) {
MessageBox( NULL, "failed to create status bar control!", "wnd_main_init", MB_OK );
return 0;
}
return 1;
}
int wnd_main_createRebar() {
REBARINFO ri;
DWORD dwStyle;
dwStyle = WS_CHILD|WS_VISIBLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN|RBS_VARHEIGHT|CCS_NODIVIDER;
hWECSG->hWndRebar1 = CreateWindowEx(WS_EX_TOOLWINDOW, REBARCLASSNAME, NULL, dwStyle, 0, 0, 0, 0, hWECSG->hWndMain, NULL, hWECSG->hInstance, NULL );
if( hWECSG->hWndRebar1 == NULL ) {
MessageBox( NULL, "failed to create rebar control!", "wnd_main_init", MB_OK );
return 0;
}
memset( &ri, 0, sizeof(REBARINFO) );
ri.cbSize = sizeof(REBARINFO);
SendMessage( hWECSG->hWndRebar1, RB_SETBARINFO, 0, (LPARAM)&ri );
return 1;
}
void wnd_main_addRebarItem( char *lpText, HWND hChild ) {
// don't use GetWindowRect, use button's size and padding instead
DWORD buttonsize = SendMessage(hChild, TB_GETBUTTONSIZE, 0,0);
DWORD padding = SendMessage(hChild, TB_GETPADDING, 0,0);
REBARBANDINFO rbi;
//RECT rc;
//GetWindowRect( hChild, &rc );
memset( &rbi, 0, sizeof(REBARBANDINFO) );
rbi.cbSize = sizeof(REBARBANDINFO);
rbi.fMask = RBBIM_CHILD|RBBIM_CHILDSIZE|RBBIM_STYLE|RBBIM_TEXT;
rbi.fStyle = RBBS_GRIPPERALWAYS|RBBS_USECHEVRON|RBBS_CHILDEDGE;
rbi.hwndChild = hChild;
rbi.cxMinChild = 0;
rbi.cyMinChild = HIWORD(buttonsize) + HIWORD(padding); // sum the two
rbi.cx = 100;
rbi.lpText = lpText;
if( !SendMessage( hWECSG->hWndRebar1, RB_INSERTBAND, (WPARAM)-1, (LPARAM)&rbi ) ) {
MessageBox( NULL, "insert band to rebar failed!", "wnd_main_init", MB_OK );
}
}
int wnd_main_createTBar1() {
DWORD dwStyle;
TBBUTTON tbb[] = {
{ I_IMAGENONE, 1, TBSTATE_ENABLED, BTNS_BUTTON, 0, 0, 0 },
{ I_IMAGENONE, 2, TBSTATE_ENABLED, BTNS_BUTTON, 0, 0, 0 }
};
// must use CCS_NODIVIDER here too to get rid of double 3D line at top
// must also specify CCS_NOPARENTALIGN and CCS_NORESIZE to prevent toolbar from aligning to the top
dwStyle = WS_CHILD | WS_VISIBLE | CCS_NOPARENTALIGN | CCS_NORESIZE |CCS_NODIVIDER;
hWECSG->hWndTBar1 = CreateWindowEx(0, TOOLBARCLASSNAME, "", dwStyle, 0, 0, 0, 0, hWECSG->hWndMain, NULL, hWECSG->hInstance, NULL );
if( hWECSG->hWndTBar1 == NULL ) {
MessageBox( NULL, "failed to create toolbar1!", "wnd_main_init", MB_OK );
}
SendMessage( hWECSG->hWndTBar1, TB_BUTTONSTRUCTSIZE, (WPARAM)(int)sizeof(TBBUTTON), 0 );
SendMessage( hWECSG->hWndTBar1, TB_ADDBUTTONS, (WPARAM)2, (LPARAM)&tbb );
SendMessage( hWECSG->hWndTBar1, TB_AUTOSIZE, 0, 0 );
wnd_main_addRebarItem( "Toolbar1", hWECSG->hWndTBar1 );
return 1;
}
int wnd_main_createTBar2() {
DWORD dwStyle;
TBBUTTON tbb[] = {
{ I_IMAGENONE, 3, TBSTATE_ENABLED, BTNS_BUTTON, 0, 0, 0 },
{ I_IMAGENONE, 4, TBSTATE_ENABLED, BTNS_BUTTON, 0, 0, 0 }
};
// must use CCS_NODIVIDER here too to get rid of double 3D line at top
// must also specify CCS_NOPARENTALIGN and CCS_NORESIZE to prevent toolbar from aligning to the top
dwStyle = WS_CHILD | WS_VISIBLE | CCS_NOPARENTALIGN | CCS_NORESIZE|CCS_NODIVIDER;
hWECSG->hWndTBar2 = CreateWindowEx( 0, TOOLBARCLASSNAME, "", dwStyle, 0, 0, 0, 0, hWECSG->hWndMain, NULL, hWECSG->hInstance, NULL );
if( hWECSG->hWndTBar2 == NULL ) {
MessageBox( NULL, "failed to create toolbar2!", "wnd_main_init", MB_OK );
}
SendMessage( hWECSG->hWndTBar2, TB_BUTTONSTRUCTSIZE, (WPARAM)(int)sizeof(TBBUTTON), 0 );
SendMessage( hWECSG->hWndTBar2, TB_ADDBUTTONS, (WPARAM)2, (LPARAM)&tbb );
SendMessage( hWECSG->hWndTBar2, TB_AUTOSIZE, 0, 0 );
wnd_main_addRebarItem( "Toolbar2", hWECSG->hWndTBar2 );
return 1;
}
int wnd_main_init() {
WNDCLASSEX wcx;
DWORD dwStyle, dwExStyle;
// set up window class and register it
memset( &wcx, 0, sizeof(WNDCLASSEX) );
wcx.cbSize = sizeof(WNDCLASSEX);
wcx.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
wcx.lpfnWndProc = (WNDPROC)wnd_main_proc;
wcx.hInstance = hWECSG->hInstance;
wcx.hbrBackground = (HBRUSH)COLOR_APPWORKSPACE+1;
wcx.lpszClassName = PACKAGE_CLASS;
if( !RegisterClassEx( &wcx ) ) {
return 0;
}
// create the application window
dwExStyle = WS_EX_APPWINDOW;
dwStyle = WS_OVERLAPPEDWINDOW;
hWECSG->hWndMain = CreateWindowEx(
dwExStyle, PACKAGE_CLASS, PACKAGE_STRING, dwStyle,
CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
NULL, NULL, hWECSG->hInstance, NULL );
if( hWECSG == NULL ) {
return 0;
}
// set up toolbars etc...
wnd_main_createStatusbar();
wnd_main_createRebar();
wnd_main_createTBar1();
wnd_main_createTBar2();
// update and show app wnd
UpdateWindow( hWECSG->hWndMain );
ShowWindow( hWECSG->hWndMain, SW_SHOW );
return 1;
}
void wnd_main_done() {
if( hWECSG->hWndMain ) {
DestroyWindow( hWECSG->hWndMain );
hWECSG->hWndMain = NULL;
}
UnregisterClass( PACKAGE_CLASS, hWECSG->hInstance );
}
- 1
- 2
- 3
前往页