 |
| Author |
Message |
Revant

|
Posted: Visual C++ Language, Button Subclassing |
Top |
Hello
I have written this code for button subclassing. But in the client area no buttons are bieng painter for some reason. Please can you have a look. Thanks
#include <windows.h> #include <stdafx.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); LRESULT CALLBACK ButtonProc(HWND, UINT, WPARAM, LPARAM);
WNDPROC OldButton ; int iFocus = 0; static TCHAR szAppName[] = TEXT("Buttons");
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {
HWND hwnd; MSG msg; WNDCLASS wndclass; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hInstance = hInstance; wndclass.lpfnWndProc = WndProc; wndclass.lpszClassName = szAppName; wndclass.lpszMenuName = NULL; wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS; RegisterClass(&wndclass);
hwnd = CreateWindow(szAppName, TEXT("BUTTON"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, iCmdShow); UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
return msg.wParam; }
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { static HWND hwndButton ; static HWND hwndRect; int i; static int cxChar, cyChar; static int cxSize, cySize; static HINSTANCE hInst;
switch(message) { case WM_CREATE:
hInst = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE); cxChar = LOWORD(GetDialogBaseUnits()); cyChar = HIWORD(GetDialogBaseUnits());
hwndRect = CreateWindow(szAppName, TEXT("Static"), WS_CHILD | WS_VISIBLE | SS_WHITERECT, 0, 0, 0, 0, hwnd, (HMENU)6, hInst, NULL);
for(i = 0; i < 5; i++) { hwndButton = CreateWindow(szAppName, TEXT("Buttons"), WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON, 0, 0, 0, 0, hwnd, (HMENU)i, hInst, NULL); OldButton = (WNDPROC) SetWindowLong(hwndButton , GWL_WNDPROC, (LONG)ButtonProc); }
SetFocus(hwnd);
return 0;
case WM_SIZE:
cxSize = LOWORD(lParam); cySize = HIWORD(lParam);
MoveWindow(hwndRect, 0, 0, cxSize / 2, cySize, TRUE);
for( i = 0; i < 5; i++) { MoveWindow(hwndButton , 1 / 4 * cxSize , 1 / 6 * (i + 1) * cySize, 10 * cxChar, 7 / 4 * cyChar, TRUE); }
return 0;
case WM_SETFOCUS:
SetFocus(hwndButton[iFocus]); return 0;
case WM_DESTROY:
PostQuitMessage(0); return 0;
} return DefWindowProc(hwnd, message, wParam, lParam); }
LRESULT CALLBACK ButtonProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { int id = GetWindowLong(hwnd, GWL_ID);
switch(message) { case WM_KEYDOWN: if( wParam == VK_TAB) { iFocus = (id + 1) % 6; SetFocus(GetParent(hwnd)); } break;
case WM_SETFOCUS:
iFocus = id; break;
} return CallWindowProc(OldButton[id], hwnd, message, wParam, lParam); }
Visual C++5
|
| |
|
| |
 |
Sarath.

|
Posted: Visual C++ Language, Button Subclassing |
Top |
I think you are missing something with the CreateWindow API for the button.
"Buttons" is not a registered window class. Either you have to register it before using or you have to use the available registered class "BUTTON" for your use.
I think that is the issue.
|
| |
|
| |
 |
Bite Qiu - MSFT

|
Posted: Visual C++ Language, Button Subclassing |
Top |
hi, Revant,
It is true that nothing display on the window, i tried to change the value of "lpClassName" to L("BUTTON") instead of szAppName, then the buttons can display, i am not sure if this change fit your case, but anyway you can check it out :)
hwndButton = CreateWindow(L"BUTTON",
L"Buttons",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
0,
0,
0,
0,
hwnd,
(HMENU)i,
hInst,
NULL);
thanks
Bite
|
| |
|
| |
 |
| |
|