不用Vc,用c能创建window程序吗???

linqiu6 2003-01-14 12:10:25
以下是我的一段实现,但却不成功,请大家看一下!!!

#include <windows.h>

LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
char szAppName[]="Window";

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
if (!hPrevInstance)
{
wndclass.style = 0;
wndclass.lpfnWndProc = (WNDPROC) WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon((HINSTANCE) NULL,IDI_APPLICATION);
wndclass.hCursor = LoadCursor((HINSTANCE) NULL,IDC_ARROW);
//wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = "MainMenu";
wndclass.lpszClassName = "MainWndClass";
if (!::RegisterClass(&wndclass))
return false;
}

hwnd=::CreateWindow("MainWndClass", "Sample",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT, (HWND) NULL,
(HMENU) NULL, hInstance, (LPVOID) NULL);
if(!hwnd)
return false;
::ShowWindow(hwnd, nCmdShow);
::UpdateWindow(hwnd);
while (::GetMessage(&msg, (HWND) NULL, 0, 0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return msg.wParam;
}
...全文
32 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
fingerfox 2003-01-14
  • 打赏
  • 举报
回复
just a sample use c
fingerfox 2003-01-14
  • 打赏
  • 举报
回复
#include <windows.h>
#include "sysmets.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static TCHAR szAppName[]=TEXT("HelloWin");
HWND hwnd;
MSG msg;
WNDCLASS wndClass;
wndClass.style=CS_HREDRAW|CS_VREDRAW;
wndClass.lpfnWndProc=WndProc;
wndClass.cbClsExtra=0;
wndClass.cbWndExtra=0;
wndClass.hInstance=hInstance;
wndClass.hIcon=LoadIcon(NULL,IDI_ASTERISK);
wndClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.lpszMenuName=NULL;
wndClass.lpszClassName=szAppName;
if(!RegisterClass(&wndClass))
{
MessageBox(NULL,TEXT("Run Programe Environment Error"),szAppName,MB_ICONERROR);
return 0;
}
hwnd=CreateWindow(szAppName,TEXT("Get System Metrics No.2"),
WS_OVERLAPPEDWINDOW|WS_VSCROLL,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 POINT aptFigure[10]={10,70,50,70,50,10,90,10,90,50,30,50,30,90,70,90,70,30,10,30};
static int cxChar,cxCaps,cyChar,cxClient,cyClient,iVscrollPos;
int i,y;
TCHAR szBuffer[10];
TEXTMETRIC tm;
HDC hdc;
PAINTSTRUCT ps;
// POINT apt[10];
// RECT rect;
switch (message)
{
case WM_CREATE:
// PlaySound(TEXT("sbounce.wav"),NULL,SND_FILENAME|SND_ASYNC);
// return 0;
hdc=GetDC(hwnd);
GetTextMetrics(hdc,&tm);
cxChar=tm.tmAveCharWidth;
cxCaps=(tm.tmPitchAndFamily&1?3:2)*cxChar/2;
cyChar=tm.tmHeight+tm.tmExternalLeading;
ReleaseDC(hwnd,hdc);
SetScrollRange(hwnd,SB_VERT,0,NUMLINES-1,FALSE);
SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
return 0;
case WM_SIZE:
cyClient = HIWORD(lParam);
cxClient = LOWORD(lParam);
return 0;
case WM_VSCROLL:
switch(LOWORD(wParam))
{
case SB_LINEUP:
iVscrollPos-=1;
break;
case SB_LINEDOWN:
iVscrollPos+=1;
break;
case SB_PAGEUP:
iVscrollPos-=cyClient/cyChar;
break;
case SB_PAGEDOWN:
iVscrollPos+=cyClient/cyChar;
break;
case SB_THUMBPOSITION:
iVscrollPos = HIWORD(wParam);
break;
default:
break;
}
iVscrollPos=max(0,min(iVscrollPos,NUMLINES-1));
if(iVscrollPos!=GetScrollPos(hwnd,SB_VERT))
{
SetScrollPos(hwnd,SB_VERT,iVscrollPos,TRUE);
InvalidateRect(hwnd,NULL,TRUE);
}
return 0;
case WM_PAINT:
// HDC hAllDC;
// hAllDC=GetDC(NULL);
// TextOut(hAllDC,20,20,"OutDC",5);
// ReleaseDC(NULL,hAllDC);
//the above 5 lines can textout sth. out of the window about the application
hdc=BeginPaint(hwnd,&ps);
/* SelectObject(hdc,GetStockObject(GRAY_BRUSH));
for(i=0;i<10;i++)
{
apt[i].x=cxClient*aptFigure[i].x/200;
apt[i].y=cyClient*aptFigure[i].y/100;
}
SetPolyFillMode(hdc,ALTERNATE);
Polygon(hdc,apt,10);
for(i=0;i<10;i++)
apt[i].x+=cxClient/2;
SetPolyFillMode(hdc,WINDING);
Polygon(hdc,apt,10);*/

for( i=0;i<NUMLINES;i++ )
{
y=cyChar * (i-iVscrollPos);
TextOut(hdc,0,y,sysmetrics[i].szLabel,lstrlen(sysmetrics[i].szLabel));
TextOut(hdc,22*cxCaps,y,sysmetrics[i].szDesc,lstrlen(sysmetrics[i].szDesc));
SetTextAlign(hdc,TA_RIGHT|TA_TOP);
TextOut(hdc,22*cxCaps+40*cxChar,y,szBuffer,wsprintf(szBuffer,TEXT("%5d"),GetSystemMetrics(sysmetrics[i].iIndex)));
SetTextAlign(hdc,TA_LEFT|TA_TOP);
}
// GetClientRect(hwnd,&rect);
// DrawText(hdc,TEXT("HELLO WINDOWS98 !"),-1,&rect,DT_SINGLELINE|DT_CENTER|DT_VCENTER);
EndPaint(hwnd,&ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}


return DefWindowProc(hwnd,message,wParam,lParam);
}
arvid_gs 2003-01-14
  • 打赏
  • 举报
回复
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);里面还要处理
WM_CRETAE
fingerfox 2003-01-14
  • 打赏
  • 举报
回复
wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
linqiu6 2003-01-14
  • 打赏
  • 举报
回复
: fingerfox(foxfinger)
按你的做法,我再编译这个文件时与user32.lib和gdi.lib进行链接,去除外部的符号没有定义的问题,然后生成了exe

但是为什么我在cmd中执行这个exe,没有出现什么窗口,好象什么事也没发生一样????双鸡它也一样
fingerfox 2003-01-14
  • 打赏
  • 举报
回复
建一个工程选择WIN32 APPLICATION
zdleek 2003-01-14
  • 打赏
  • 举报
回复
Win32 SDK
zdleek 2003-01-14
  • 打赏
  • 举报
回复
<<Windows 程序设计>>
ixMind 2003-01-14
  • 打赏
  • 举报
回复
连接到相关的库?
linqiu6 2003-01-14
  • 打赏
  • 举报
回复
但是我编译时,就提示有很多外部的符号没有定义,如TranslateMessage等

请问能给出相关的编译吗???

15,979

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 界面
社区管理员
  • 界面
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧