C++编程 windows窗口程序和矩阵类程序

题目一:设计一个windows程序使窗体总在最前面,并且保持窗体颜色动态变化。 这个是中等难度的,嘿嘿,高手帮我解决下吧!题目二:设计一个矩阵类,可以实现构造矩阵,设置矩阵元素,提取元素,提取行,列信息,可以进行矩阵加,减,乘,转置,求逆运算,并设计一个GUI,对该矩阵类的功能进行测试。 这个是较难的,如果不能全部设计出,部分程序也行,谢谢高手了以上两题,做出一道也没问题我的邮箱:490892191@qq.com高手:谢谢了O(∩_∩)O~现在只要第二题了。。。 是建个工程。。。 MFC工程 EXE那个。。。
2026年09月22日 16:20
有3个网友回答
网友(1):

#include
#include
#include

using namespace std;

//m*n 阶矩阵
class M
{
public:
M(int m, int n);
M(M& m);
virtual ~M();
int m;
int n;
private:
int *a;
friend istream& operator>>( istream& in, M& m );
friend ostream& operator<<( ostream& out, M& m );
friend M& operator+(M& a, M& b);
};

M::M(int m=1, int n=1) : m(m), n(n)
{
a = new int[n*m];
}

//拷贝构造
M::M(M& m) : m(m.m), n(m.n)
{
a = new int[m.n*m.m];
memcpy(a, m.a, m.n*m.m*sizeof(int));
}

M::~M()
{
delete a;
}

//输入矩阵
istream& operator>>( istream& in, M& m )
{
cout<<"input "< for( int i=0; i {
cout<<"input line "< for( int j=0; j {
in >> (*(m.a+i*m.n+j));
}
}
return in;
}

//输出矩阵
ostream& operator<<( ostream& out, M& m )
{
for( int i=0; i {
for( int j=0; j {
out< }
out< }

return out;
}

//矩阵加法
M& operator+(M& a, M& b)
{
if( !( a.m==b.m && a.n==b.n ) )
{
cout<<"Error: can not +"< }

M m(a);

for( int i=0; i {
for( int j=0; j {
*(m.a+i*m.n+j) += *(b.a+i*m.n+j);
}
}
M* x = new M(m);
return *x;
}

int main(int argc, char *argv[])
{
M a(2, 3), b(2, 3);
cin>>a>>b;
M c = a + b;
cout<
return 0;
}

网友(2):

就会第一题,第二题的GUI没有研究,以下是C++程序代码:
#include

HDC
hdc;
PAINTSTRUCT
ps;
RECT
rect;
LRESULT
CALLBACK
WndProc
(HWND,
UINT,
WPARAM,
LPARAM)
;
int
WINAPI
WinMain
(HINSTANCE
hInstance,
HINSTANCE
hPrevInstance,
PSTR
szCmdLine,
int
nCmdShow)
{
static
TCHAR
szAppName
[]="applicaiton";
HWND
hwnd;
WNDCLASS
wndcls;
MSG
msg;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WndProc;
wndcls.lpszMenuName=NULL;
wndcls.lpszClassName=szAppName;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndcls))
{
MessageBox(NULL,"Error","error",MB_ICONERROR);
return
0;
}
hwnd=CreateWindowEx(WS_EX_TOPMOST,
szAppName,
"windows窗口程序Demo",
WS_OVERLAPPEDWINDOW,
0,0,
400,300,
NULL,NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);
DWORD
start_time;
while(true)
{
hdc=GetDC(hwnd);
start_time=GetTickCount();
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GetClientRect
(hwnd,
&rect)
;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
while(GetTickCount()-start_time<250)
{}
ReleaseDC(hwnd,hdc);
}
return
msg.wParam;
}
LRESULT
CALLBACK
WndProc
(HWND
hwnd,
UINT
msg,
WPARAM
wapram,
LPARAM
lapram)
{
switch(msg)
{
case
WM_PAINT:
{
hdc
=
BeginPaint
(hwnd,
&ps)
;
GetClientRect
(hwnd,
&rect)
;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
EndPaint
(hwnd,
&ps)
;
return
0;
}
break;
case
WM_DESTROY:
{
PostQuitMessage
(0)
;
return
0
;
}
break;
}
return
DefWindowProc
(hwnd,
msg,
wapram,
lapram)
;
}
注意:最后在菜单栏选择工程->设置->链接,或者快捷键
alt+F7,调出对话框,将工程选项的subsystem:console的console改为windows,嘿嘿!!!
搞定,运行即可。哈哈
由于这个程序使用的是GDI函数直接调用,没有优化,比较占资源,刚开始反映可能会有些迟钝。

网友(3):

就会第一题,第二题的GUI没有研究,以下是C++程序代码:
#include

HDC hdc;
PAINTSTRUCT ps;
RECT rect;

LRESULT CALLBACK WndProc (HWND,
UINT,
WPARAM,
LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int nCmdShow)
{
static TCHAR szAppName []="applicaiton";
HWND hwnd;
WNDCLASS wndcls;
MSG msg;
wndcls.cbClsExtra=0;
wndcls.cbWndExtra=0;
wndcls.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndcls.hCursor=LoadCursor(NULL,IDC_ARROW);
wndcls.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndcls.hInstance=hInstance;
wndcls.lpfnWndProc=WndProc;
wndcls.lpszMenuName=NULL;
wndcls.lpszClassName=szAppName;
wndcls.style=CS_HREDRAW|CS_VREDRAW;
if(!RegisterClass(&wndcls))
{
MessageBox(NULL,"Error","error",MB_ICONERROR);
return 0;
}
hwnd=CreateWindowEx(WS_EX_TOPMOST,
szAppName,
"windows窗口程序Demo",
WS_OVERLAPPEDWINDOW,
0,0,
400,300,
NULL,NULL,
hInstance,
NULL);
ShowWindow(hwnd,nCmdShow);
UpdateWindow(hwnd);

DWORD start_time;
while(true)
{
hdc=GetDC(hwnd);
start_time=GetTickCount();

if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{ if(msg.message==WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
GetClientRect (hwnd, &rect) ;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
while(GetTickCount()-start_time<250)
{}
ReleaseDC(hwnd,hdc);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT msg, WPARAM wapram, LPARAM lapram)
{
switch(msg)
{

case WM_PAINT:
{
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
FillRect(hdc,&rect,CreateSolidBrush(RGB(rand()%255,rand()%255,rand()%255)));
EndPaint (hwnd, &ps) ;
return 0;
}
break;
case WM_DESTROY:
{
PostQuitMessage (0) ;
return 0 ;
}
break;
}
return DefWindowProc (hwnd, msg, wapram, lapram) ;
}
注意:最后在菜单栏选择工程->设置->链接,或者快捷键 alt+F7,调出对话框,将工程选项的subsystem:console的console改为windows,嘿嘿!!!
搞定,运行即可。哈哈
由于这个程序使用的是GDI函数直接调用,没有优化,比较占资源,刚开始反映可能会有些迟钝。