Sign in to follow this  
Thehuffermsu

win32 controls

Recommended Posts

Thehuffermsu    122
In an effort to learn win32 programming with C++ I decided to try to impliment a battleship game I'd written in C#. The problem I'm having is I'd like to create my board of 100 buttons at runtime instead of making a dialog resource and placing them by hand. I started out placing them directly on my main window but decided to try placing them on a modeless dialog inside the window. What'd I'd like to do is create a modeless dialog from a resource, and size it to equal the size of the client area of the main window and place my controls on the dialog, but im not sure thats possible?

Share this post


Link to post
Share on other sites
yadango    567
if i recall right you most certainly can make a modeless dialog box a child window and resize it from the parent's WM_SIZE handler. modal is a different story, but modeless you should be fine.

Share this post


Link to post
Share on other sites
yadango    567
basic.cpp

#include<windows.h>
#include "crap.h"

HINSTANCE basead = 0;
LPCTSTR title = TEXT("Fundamental Window");
LPCTSTR cname = TEXT("Fundamental Window");

LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam);
INT_PTR CALLBACK TestDialogProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam);

int WINAPI WinMain(HINSTANCE handle, HINSTANCE, LPSTR, int)
{
basead = handle;

WNDCLASS wc;
wc.style = CS_DBLCLKS;
wc.lpfnWndProc = MainWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = basead;
wc.hIcon = 0;
wc.hCursor = LoadCursor(0, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = title;

if(!RegisterClass(&wc))
return MessageBox(0, TEXT("Could not register main window."), TEXT("Error"), MB_ICONSTOP);

DWORD style = WS_OVERLAPPEDWINDOW;
int x = CW_USEDEFAULT; int w = CW_USEDEFAULT;
int y = CW_USEDEFAULT; int h = CW_USEDEFAULT;

HWND window = CreateWindowEx(0, cname, title, style, x, y, w, h, 0, 0, basead, 0);
if(!window) return MessageBox(0, TEXT("Could not create main window."), TEXT("Error"), MB_ICONSTOP);

ShowWindow(window, SW_SHOW);
UpdateWindow(window);

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

return (int)msg.wParam;
}

LRESULT CALLBACK MainWndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
static HWND dialog = 0;

switch(message) {
case(WM_CREATE) : {
dialog = ::CreateDialogW(basead, MAKEINTRESOURCE(ID_TESTDIALOG), window, TestDialogProc);
ShowWindow(dialog, SW_SHOW);
break;
}
case(WM_SIZE) : {
RECT rect;
GetClientRect(window, &rect);
if(dialog) MoveWindow(dialog, 0, 0, rect.right - rect.left, rect.bottom - rect.top, TRUE);
break;
}
case(WM_DESTROY) : {
PostQuitMessage(0);
break;
}
}
return DefWindowProc(window, message, wparam, lparam);
}

INT_PTR CALLBACK TestDialogProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
{
return FALSE;
}



crap.h

#include<windows.h>

#define ID_TESTDIALOG 9




crap.rc

#include "crap.h"

ID_TESTDIALOG DIALOGEX 0, 0, 187, 95
STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,130,7,50,14
PUSHBUTTON "Cancel",IDCANCEL,130,24,50,14
END

Share this post


Link to post
Share on other sites
Thehuffermsu    122
Thanks yadango, after looking at your simple resource file im pondering if it wouldn't just be simpler to hand write the .rc file with all the buttons placed and then have handles to use then to dynamically add them in code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this