這是繼上次那個選項卡界面切換的一個問題,因為最終沒能有適合的方案,決定嘗試超類化,因為它是唯一處理WM_CREATE的方法.
上代碼
#pragma once
#include "framework.h"
HINSTANCE UIHINST
HWND = hwnd;
WNDPROC EXTTOLD;
typedef struct tagEXT
{//
HWND = hWnd; // handle of owner window
BOOL x1; //默認是185
BOOL y1; //默認是170
BOOL x2;
BOOL y2;
HBRUSH style;
UINT styleU;
WNDPROC lpfnWndProc;
int cbClsExtra;
int cbWndExtra;
HINSTANCE hInstance;
HICON hIcon;
HCURSOR hCursor;
LPCWSTR lpszMenuName;
LPCWSTR lpszClassName;
} EXT_PARAM;
//static wchar_t m_strInput[0xfffff] = L""; //宣告輸入框變數
#define IDS_TEXT1 0x200
#define IDB_BT1 0x199
LRESULT CALLBACK EXTUIProc(hwnd hwnd, UINT message, WPARAM wParam, LPARAM lParam) //str=wcs,必須轉化成Unicode
{
// RECT rc;
// char prompt[40];
//int len = wcslen(m_strInput);
switch (message)
{
case WM_CREATE:
{
MessageBox(0,L"當你看到這個訊息,就證明視窗創建成功了",L"超類化",0);
break;
}
break;
default:
break;
}
return CallWindowProc(EXTUIProc, hwnd, message, wParam, lParam);
}
int EXTUI(
HWND hWnd, // handle of owner window
BOOL x1,
BOOL y1,
BOOL x2,
BOOL y2
//LPWSTR in,
)
{
WNDCLASSEX extwindow;
::GetClassInfoEx(hInstance, L"extwin", &extwindow);
// 保存原控制元件默認訊息處理函式
EXTTOLD = extwindow.lpfnWndProc;
// 設定替換的訊息處理函式
extwindow.lpfnWndProc = EXTUIProc;
// 指定新的視窗類名字
extwindow.lpszClassName = L"extwindow";
// 設定結構體大小
extwindow.cbSize = sizeof(WNDCLASSEX);
// 注冊新資訊
RegisterClassEx(&extwindow);
//WNDCLASSEX wndclass = { 0 };
static HFONT hFont;
UIHINST = hInstance;
CON_PARAM btn = { 0 };
btn.hWnd = hWnd;
hwnd = CreateWindowEx(NULL, extwindow.lpszClassName, L"EXTUI",
WS_CHILD , x1, y1, x2, y2, hwnd, NULL, hInstance, 0);
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hWnd);
// return 0;
return -1;
}
之后在主視窗
EXTUI(x,y,rcx,rcy);//顯示
如上代碼,原本想實作一個子視窗.
不過如下圖

這個子視窗完全沒被創建!
綜上代碼,是出了什么錯誤呢?
uj5u.com熱心網友回復:
BOOL x1; //默認是185BOOL y1; //默認是170
BOOL x2;
BOOL y2;
為什么是 BOOL?
uj5u.com熱心網友回復:
hwnd = CreateWindowEx(NULL, extwindow.lpszClassName, L"EXTUI",WS_CHILD , x1, y1, x2, y2, hwnd hParentWnd, NULL, hInstance, 0);
uj5u.com熱心網友回復:
// Win32Tab.cpp : Defines the entry point for the application.//
#include "stdafx.h"
#include "resource.h"
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
//
#define PAGES 3
HWND g_pages[PAGES];
int g_PageNow=0;
HWND g_btn1;
HWND g_btn2;
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WIN32TAB, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WIN32TAB);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
#define MainWidth 100
//
BOOL CALLBACK PageProc1(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
HDC hdc;
HBRUSH br;
// POINT pt;
// char prompt[40];
//
switch (uMsg)
{
case WM_INITDIALOG:
GetClientRect(hwndDlg,&rc);
rc.left += MainWidth;
rc.right += MainWidth;
MoveWindow(hwndDlg,rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top,TRUE);
return TRUE;
case WM_ERASEBKGND:
hdc=(HDC)wParam;
GetClientRect(hwndDlg,&rc);
br=CreateSolidBrush(RGB(60,60,255));
FillRect(hdc,&rc,br);
DeleteObject(br);
return TRUE;//not break;
}
return FALSE;
}
BOOL CALLBACK PageProc2(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
HDC hdc;
HBRUSH br;
// POINT pt;
// char prompt[40];
//
switch (uMsg)
{
case WM_INITDIALOG:
GetClientRect(hwndDlg,&rc);
rc.left += MainWidth;
rc.right += MainWidth;
MoveWindow(hwndDlg,rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top,TRUE);
return TRUE;
case WM_ERASEBKGND:
hdc=(HDC)wParam;
GetClientRect(hwndDlg,&rc);
br=CreateSolidBrush(RGB(255,60,60));
FillRect(hdc,&rc,br);
DeleteObject(br);
return TRUE;//not break;
}
return FALSE;
}
BOOL CALLBACK PageProc3(HWND hwndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
RECT rc;
HDC hdc;
HBRUSH br;
// POINT pt;
// char prompt[40];
//
switch (uMsg)
{
case WM_INITDIALOG:
GetClientRect(hwndDlg,&rc);
rc.left += MainWidth;
rc.right += MainWidth;
MoveWindow(hwndDlg,rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top,TRUE);
return TRUE;
case WM_ERASEBKGND:
hdc=(HDC)wParam;
GetClientRect(hwndDlg,&rc);
br=CreateSolidBrush(RGB(60,255,60));
FillRect(hdc,&rc,br);
DeleteObject(br);
return TRUE;//not break;
}
return FALSE;
}
HWND CreateBT(HWND parentWnd,int x,int y,int wid,int hei,UINT ID)
{
HWND hBt=0;
hBt = CreateWindow("BUTTON",
NULL,
WS_CHILD|WS_TABSTOP|WS_VISIBLE,//|BS_PUSHBUTTON|WS_SIZEBOX|WS_GROUP,
x,y,wid,hei,
parentWnd, (HMENU)ID,//
(HINSTANCE) GetWindowLong(parentWnd, GWL_HINSTANCE),
NULL);
if(ID==IDC_BUTTON) SetWindowText(hBt,"頁面切換");
if(ID==IDC_BUTTON1) SetWindowText(hBt,"X");
//
return hBt;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WIN32TAB);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = 0;//(LPCSTR)IDC_WIN32TAB;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
RECT rc;
int edgeX;
int edgeY;
hWnd = CreateWindow(szWindowClass, "Win32Tab", WS_SYSMENU,//WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
#if 1
//創建無邊框無標題欄表單區域
GetWindowRect(hWnd,&rc);//
OffsetRect(&rc,-rc.left,-rc.top);
edgeX=GetSystemMetrics(SM_CXDLGFRAME);// 3
edgeY=GetSystemMetrics(SM_CYDLGFRAME);// 3
rc.top=GetSystemMetrics(SM_CYCAPTION);// 21
HRGN wndRgn;
wndRgn=CreateRectRgn(rc.left+edgeX,rc.top+edgeY,rc.right-edgeX,rc.bottom-edgeY);
SetWindowRgn(hWnd,wndRgn,1);
#endif
//
g_btn1=CreateBT(hWnd,10, 160, 80, 24,IDC_BUTTON);// pages
g_btn2=CreateBT(hWnd,82, 4, 14,14,IDC_BUTTON1);// close
//
g_pages[0]=CreateDialog(hInstance, MAKEINTRESOURCE(IDD_PROPPAGE1), hWnd, PageProc1);
g_pages[1]=CreateDialog(hInstance, MAKEINTRESOURCE(IDD_PROPPAGE2), hWnd, PageProc2);
g_pages[2]=CreateDialog(hInstance, MAKEINTRESOURCE(IDD_PROPPAGE3), hWnd, PageProc3);
ShowWindow(g_pages[0],SW_SHOW);
//
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
HBRUSH br;
RECT rc;
int wmId, wmEvent;
UINT hit;
PAINTSTRUCT ps;
switch (message)
{
case WM_ERASEBKGND:
hdc=(HDC)wParam;
GetClientRect(hWnd,&rc);
rc.right=MainWidth;
br=CreateSolidBrush(RGB(240,240,0));
FillRect(hdc,&rc,br);
DeleteObject(br);
return TRUE;//not break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
if(wmId==IDC_BUTTON)
{
ShowWindow(g_pages[g_PageNow],SW_HIDE);
g_PageNow ++;
g_PageNow %=PAGES;
ShowWindow(g_pages[g_PageNow],SW_SHOW);
}
if(wmId==IDC_BUTTON1)
{
PostQuitMessage(0);
}
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
SetBkMode(hdc,TRANSPARENT);
TextOut(hdc,4,4,"Win32Tab",8);
EndPaint(hWnd, &ps);
break;
case WM_NCHITTEST://
hit=DefWindowProc(hWnd,message,wParam,lParam);
if(hit== HTCLIENT) hit=HTCAPTION;
return hit;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
允許在使用函式的時候輸入數字,問題不出在這里,經過反復去掉代碼排查確定和類注冊關系最大uj5u.com熱心網友回復:
打錯了,這個是hWnduj5u.com熱心網友回復:
如果是做兩個按鈕,如界面1,界面2,可以通過一樣的方法實作?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/159740.html
標籤:界面
